use crate::MEMORY_SIZE;

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EmulatorState {
    pub a: u8,
    pub b: u8,
    pub c: u8,
    pub d: u8,
    pub e: u8,
    pub h: u8,
    pub l: u8,

    pub cc: ConditionCodes,

    /// Stack pointer
    pub sp: u16,
    /// Memory pointer
    pub pc: u16,
    /// Enable interrupts
    pub ei: bool,
    /// Memory map
    pub memory: [u8; MEMORY_SIZE],
}

impl EmulatorState {
    // Read the next byte from memory pointed at by PC
    pub fn next_byte(&mut self) -> u8 {
        let value = self.memory[self.pc as usize];
        (self.pc, ..) = self.pc.overflowing_add(1);
        value
    }

    // Read the next 16-bit word from memory pointed at by PC, in little endian order
    pub fn next_word(&mut self) -> u16 {
        let value = self.read_word(self.pc);
        (self.pc, ..) = self.pc.overflowing_add(2);
        value
    }

    // Read a 16-bit word at a specific address
    pub fn read_word(&mut self, address: u16) -> u16 {
        u16::from_le_bytes([
            self.memory[address as usize],
            self.memory[address.overflowing_add(1).0 as usize],
        ])
    }

    // Write a 16-bit word at a specific address
    pub fn write_word(&mut self, address: u16, value: u16) {
        let [low, high] = u16::to_le_bytes(value);
        self.memory[address as usize] = low;
        self.memory[address.overflowing_add(1).0 as usize] = high;
    }
}

impl Default for EmulatorState {
    fn default() -> Self {
        Self {
            a: 0,
            b: 0,
            c: 0,
            d: 0,
            e: 0,
            h: 0,
            l: 0,
            sp: 0,
            cc: ConditionCodes {
                z: true,
                s: true,
                p: true,
                c: true,
            },
            pc: 0,
            ei: false,
            memory: [0; MEMORY_SIZE],
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ConditionCodes {
    /// Zero (Z), set if the result is zero.
    pub z: bool,
    /// Sign (S), set if the result is negative.
    pub s: bool,
    /// Parity (P), set if the number of 1 bits in the result is even.
    pub p: bool,
    /// Carry (C), set if the last addition operation resulted in a carry or if the last subtraction operation required a borrow.
    pub c: bool,
    // Auxiliary carry (AC or H), used for binary-coded decimal arithmetic (BCD).
    // Can't test this so I won't implement it
    // ac: bool,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Register {
    B,
    C,
    D,
    E,
    H,
    L,
    M,
    A,
    SP,
}

/// Returns a Register enum based on the input number 0..7 in the order B,C,D,E,H,L,M,A
#[track_caller]
pub fn register_from_num(b: u8) -> Register {
    match b {
        0 => Register::B,
        1 => Register::C,
        2 => Register::D,
        3 => Register::E,
        4 => Register::H,
        5 => Register::L,
        6 => Register::M,
        7 => Register::A,
        _ => panic!("'{b}' cannot be converted to register enum"),
    }
}

pub fn get_register(register: Register, state: &EmulatorState) -> u8 {
    match register {
        Register::B => state.b,
        Register::C => state.c,
        Register::D => state.d,
        Register::E => state.e,
        Register::H => state.h,
        Register::L => state.l,
        Register::A => state.a,
        Register::M => state.memory[u16::from_le_bytes([state.l, state.h]) as usize],
        Register::SP => unreachable!(),
    }
}

pub fn set_register(register: Register, value: u8, state: &mut EmulatorState) {
    match register {
        Register::B => state.b = value,
        Register::C => state.c = value,
        Register::D => state.d = value,
        Register::E => state.e = value,
        Register::H => state.h = value,
        Register::L => state.l = value,
        Register::A => state.a = value,
        Register::M => state.memory[u16::from_le_bytes([state.l, state.h]) as usize] = value,
        Register::SP => panic!("Cannot set 'SP' through set_register()"),
    };
}

pub fn get_register_pair(register: Register, state: &mut EmulatorState) -> u16 {
    match register {
        Register::B => u16::from_le_bytes([state.c, state.b]),
        Register::D => u16::from_le_bytes([state.e, state.d]),
        Register::H => u16::from_le_bytes([state.l, state.h]),
        Register::A => {
            // the PSW looks like this: SZ0A0P1C
            let flags: u8 = u8::from(state.cc.s) << 7 // bit 7
                | u8::from(state.cc.z) << 6 // bit 6
                //| u8::from(state.cc.a) << 4 // bit 4
                | u8::from(state.cc.p) << 2 // bit 2
                | 0x02 // bit 1
                | u8::from(state.cc.c); // bit 0

            u16::from_le_bytes([flags, state.a])
        }
        Register::SP => state.sp,
        _ => unreachable!(),
    }
}

pub fn set_register_pair(register: Register, value: u16, state: &mut EmulatorState) {
    let arr = value.to_le_bytes();
    let high = arr[1];
    let low = arr[0];
    match register {
        Register::B => {
            state.b = high;
            state.c = low;
        }
        Register::D => {
            state.d = high;
            state.e = low;
        }
        Register::H => {
            state.h = high;
            state.l = low;
        }
        Register::A => {
            state.a = high;
            // the PSW looks like this: SZ0A0P1C
            state.cc.s = low & 0b1000_0000 > 0;
            state.cc.z = low & 0b0100_0000 > 0;
            debug_assert!(low & 0b0010_0000 == 0, "malformed PSW");
            //state.cc.a = low & 0b0001_0000 > 0;
            debug_assert!(low & 0b0000_1000 == 0, "malformed PSW");
            state.cc.p = low & 0b0000_0100 > 0;
            debug_assert!(low & 0b0000_0010 > 0, "malformed PSW");
            state.cc.c = low & 0b0000_0001 > 0;
        }
        Register::SP => state.sp = value,
        _ => unreachable!(),
    }
}

/// Print values of registers and flags to stdout
pub fn print_state(state: &EmulatorState) {
    // State
    println!("\nsp\tpc\tei");
    println!("{:#06x}\t{:#06x}\t{}", state.sp, state.pc, state.ei);

    // Registers
    println!("\nB\tC\tD\tE\tH\tL\tA");
    println!(
        "{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}",
        state.b, state.c, state.d, state.e, state.h, state.l, state.a
    );

    // Flags
    println!("\nz\ts\tp\tc");
    println!(
        "{}\t{}\t{}\t{}",
        state.cc.z, state.cc.s, state.cc.p, state.cc.c
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn read_word() {
        let mut state = EmulatorState::default();
        state.memory[0x10] = 0x34;
        state.memory[0x11] = 0x12;
        assert_eq!(state.read_word(0x10), 0x1234);
    }

    #[test]
    fn write_word() {
        let mut state = EmulatorState::default();
        state.write_word(0x10, 0x1234);
        assert_eq!(state.memory[0x10..=0x11], [0x34, 0x12]);
    }

    #[test]
    fn next_word_at_pc() {
        let mut state = EmulatorState::default();
        state.memory[0x10] = 0x34;
        state.memory[0x11] = 0x12;
        state.pc = 0x10;
        assert_eq!(state.next_word(), 0x1234);
    }
}
