use crate::{
    arch::{drivers::vga::WRITER, gdt},
    kernel_state::KERNEL_STATE,
    print, println,
    rhai_shell::KEYBUFF,
};
use cpuio::outb;
use pic8259::ChainedPics;
use spin::Lazy;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};

pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;

pub static PICS: spin::Mutex<ChainedPics> =
    spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });

/// Interrupt offsets.
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum InterruptIndex {
    Timer = PIC_1_OFFSET,
    Keyboard,
    // SecondInterrupt = PIC_2_OFFSET,
    Cmos = 0x70,
}

impl InterruptIndex {
    fn as_u8(self) -> u8 {
        self as u8
    }
    fn as_usize(self) -> usize {
        usize::from(self.as_u8())
    }
}

static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
    let mut idt = InterruptDescriptorTable::new();
    idt.breakpoint.set_handler_fn(breakpoint_handler);
    unsafe {
        idt.double_fault
            .set_handler_fn(double_fault_handler)
            .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
    }

    // This gives fast interrupts
    set_pit_frequency(1000);

    idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
    idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);

    idt[6].set_handler_fn(floppy_disk_interrupt_handler);

    idt
});

extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
    println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
}

extern "x86-interrupt" fn double_fault_handler(
    stack_frame: InterruptStackFrame,
    error_code: u64,
) -> ! {
    panic!(
        "EXCEPTION: Error code{}\nDOUBLE FAULT\n{:#?}",
        error_code, stack_frame
    );
}

extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
    kernel::tick();
    unsafe {
        PICS.lock()
            .notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
    }
}

extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
    use crate::keyboard::{
        CustomLayout, CustomScancodeSet, DecodedKey, DecodedKeyKind, HandleControl, KeyCode,
        Keyboard,
    };
    use spin::Mutex;
    use x86_64::instructions::port::Port;

    static KEYBOARD: Lazy<Mutex<Keyboard<CustomLayout, CustomScancodeSet>>> = Lazy::new(|| {
        Mutex::new(Keyboard::new(
            CustomLayout::new_us104key(),
            CustomScancodeSet::default(),
            HandleControl::Ignore,
        ))
    });

    let mut keyboard = KEYBOARD.lock();
    let mut port = Port::new(0x60);
    let scancode: u8 = unsafe { port.read() };
    if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
        if let Some(key) = keyboard.process_keyevent(key_event) {
            match key {
                DecodedKey {
                    kind: DecodedKeyKind::Unicode,
                    value: character,
                } => {
                    match character {
                        // Backspace
                        8 => {
                            WRITER.lock().backspace();
                            KEYBUFF.lock().push(8_u8 as _);
                            // print!(" ");
                            // WRITER.lock().backspace();
                        }

                        0x5E => KERNEL_STATE.lock().shutdown(),

                        _ => {
                            let mut buff = KEYBUFF.lock();

                            buff.push(character as u8 as char);

                            print!("{}", char::try_from(character).unwrap());
                        }
                    }
                }
                DecodedKey {
                    kind: DecodedKeyKind::RawKey,
                    value: key,
                } => match KeyCode::from(key) {
                    KeyCode::AltLeft => {}

                    KeyCode::AltRight => {}
                    _ => {
                        print!("{:?}", KeyCode::from(key))
                    }
                },
                // Register a keyboard interrupt handler for the given scancode.
            }
        }
    }
    unsafe {
        PICS.lock()
            .notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
    }
}

extern "x86-interrupt" fn floppy_disk_interrupt_handler(_stack_frame: InterruptStackFrame) {
    println!("EXCEPTION: FLOPPY DISK");
}

pub fn init_idt() {
    IDT.load();
}

fn set_pit_frequency(freq: u32) {
    let divisor: u16 = (1193180 / freq).try_into().unwrap();

    unsafe {
        outb(0x36, 0x43);

        outb((divisor & 0xFF) as u8, 0x40);
        outb((divisor >> 8) as u8, 0x40);
    }
}
