use crate::structs::{get_register_pair, set_register, set_register_pair};
use crate::{get_register, EmulatorState, Register};

/// Sets the condition code flags according to `result`.
/// Does not set the carry flag and will always set the Z, S and P flags.
#[inline(always)]
fn set_cc(state: &mut EmulatorState, result: u8) {
    state.cc.z = result == 0;
    state.cc.s = result & 0x80 > 0;
    state.cc.p = result.count_ones() % 2 == 0;
}

/// Add values of `register` and `A`, add +1 if carry arg is set (either false or state.cc.c)
pub fn add_reg(register: Register, carry: bool, state: &mut EmulatorState) {
    add(get_register(register, state), carry, state);
}

/// Add values of input byte and `A`, add +1 if carry arg is set (either false or state.cc.c)
pub fn add(byte: u8, carry: bool, state: &mut EmulatorState) {
    let (a, first) = state.a.overflowing_add(byte);
    let (result, second) = a.overflowing_add(carry as u8);

    state.cc.c = first != second;
    set_cc(state, result);
    state.a = result;
}

pub fn sub_reg(register: Register, borrow: bool, state: &mut EmulatorState) {
    sub(get_register(register, state), borrow, state);
}

pub fn sub(byte: u8, borrow: bool, state: &mut EmulatorState) {
    let (a, first) = state.a.overflowing_sub(byte);
    let (result, second) = a.overflowing_sub(borrow as u8);

    state.cc.c = first != second;
    set_cc(state, result);
    state.a = result;
}

macro_rules! bitwise_op {
    ($name:ident, $reg_name:ident, $op:tt) => {
        pub fn $reg_name(register: Register, state: &mut EmulatorState) {
            $name(get_register(register, state), state);
        }

        pub fn $name(byte: u8, state: &mut EmulatorState) {
            let result = state.a $op byte;
            state.cc.c = false;
            set_cc(state, result);
            state.a = result;
        }
    }
}

bitwise_op!(and, and_reg, &);
bitwise_op!(or, or_reg, |);
bitwise_op!(xor, xor_reg, ^);

pub fn cmp_reg(register: Register, state: &mut EmulatorState) {
    cmp(get_register(register, state), state);
}

pub fn cmp(byte: u8, state: &mut EmulatorState) {
    let (result, carry) = state.a.overflowing_sub(byte);
    state.cc.c = carry;
    set_cc(state, result);
}

/// Double precision add - Add B&C, D&E or H&L to H&L
pub fn dad(register: Register, state: &mut EmulatorState) {
    let num = get_register_pair(register, state);
    let (result, overflow) = num.overflowing_add(u16::from_le_bytes([state.l, state.h]));

    // this is the only 16-bit arithmetic function that sets the other flags
    state.cc.z = result == 0;
    state.cc.s = result & 0x8000 > 0;
    state.cc.c = overflow;
    state.cc.p = result.count_ones() % 2 == 0;
    set_register_pair(register, result, state);
}

/// Increase register
pub fn inr(register: Register, state: &mut EmulatorState) {
    let (result, _) = get_register(register, state).overflowing_add(1);
    set_cc(state, result);
    set_register(register, result, state);
}

/// Decrease register
pub fn dcr(register: Register, state: &mut EmulatorState) {
    let (result, _) = get_register(register, state).overflowing_sub(1);
    set_cc(state, result);
    set_register(register, result, state);
}

/// Increase register pair
pub fn inx(register: Register, state: &mut EmulatorState) {
    let (result, _) = get_register_pair(register, state).overflowing_add(1);
    set_register_pair(register, result, state);
}

/// Decrease register pair
pub fn dcx(register: Register, state: &mut EmulatorState) {
    let (result, _) = get_register_pair(register, state).overflowing_sub(1);
    set_register_pair(register, result, state);
}

pub fn rlc(state: &mut EmulatorState) {
    let result = state.a.rotate_left(1);
    state.a = result;
    state.cc.c = result & 0x01 > 0;
}

pub fn rrc(state: &mut EmulatorState) {
    state.cc.c = state.a & 0x01 > 0;
    state.a = state.a.rotate_right(1);
}

pub fn ral(state: &mut EmulatorState) {
    let new_carry = state.a & 0x80 > 0;
    let result = state.a << 1;
    state.a = result | new_carry as u8;
    state.cc.c = new_carry;
}

pub fn rar(state: &mut EmulatorState) {
    let new_carry = state.a & 0x01 > 0;
    let result = state.a >> 1;
    state.a = result | (new_carry as u8) << 7;
    state.cc.c = new_carry;
}
