use crate::{debug, serial_print};
use pic8259::ChainedPics;
use x86_64::registers::control::Cr2;
use x86_64::structures::idt::{
    InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode,
};
use x86_64::structures::paging::mapper::MapperFlushAll;
use x86_64::structures::paging::{
    FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
};

use super::gdt;
use crate::arch::x86_64::memory::{FRAME_ALLOCATOR, OFFSET_PAGE_TABLE};
use crate::{println_log, serial_println};
use spin::{Lazy, Mutex};

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_1ST_INDEX);
    }

    idt.general_protection_fault
        .set_handler_fn(general_protection_fault_handler);
    idt.page_fault.set_handler_fn(page_fault_handler);

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

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

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

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum InterruptIndex {
    Timer = PIC_1_OFFSET,
    Keyboard,
}

impl InterruptIndex {
    const fn as_u8(self) -> u8 {
        self as u8
    }

    fn _as_usize(self) -> usize {
        usize::from(self.as_u8())
    }
}

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

pub fn enable_pic() {
    unsafe {
        PICS.lock().initialize();
        PICS.lock().write_masks(0xfc, 0xff);
    }
}

pub fn disable_pic() {
    unsafe {
        PICS.lock().disable();
    }
}

extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
    serial_println!("Exception: Breakpoint\n{:#?}", stack_frame);
    println_log!("Exception: Breakpoint\n{:#?}", stack_frame);
}

extern "x86-interrupt" fn general_protection_fault_handler(
    stack_frame: InterruptStackFrame,
    _error_code: u64,
) {
    serial_println!("Exception: General Protection Fault\n{:#?}", stack_frame);
    panic!("Exception: General Protection Fault\n{:#?}", stack_frame);
}

extern "x86-interrupt" fn double_fault_handler(
    stack_frame: InterruptStackFrame,
    _error_code: u64,
) -> ! {
    serial_println!("Exception: Double Fault\n{:#?}", stack_frame);
    panic!("Exception: Double Fault\n{:#?}", stack_frame);
}

extern "x86-interrupt" fn keyboard_interrupt_handler(
    _stack_frame: InterruptStackFrame,
) {
    use pc_keyboard::{HandleControl, Keyboard, ScancodeSet1, layouts};
    // use pc_keyboard::DecodedKey;
    use spin::Mutex;
    use x86_64::instructions::port::Port;

    static KEYBOARD: Lazy<Mutex<Keyboard<layouts::Uk105Key, ScancodeSet1>>> =
        Lazy::new(|| {
            Mutex::new(Keyboard::new(
                ScancodeSet1::new(),
                layouts::Uk105Key,
                HandleControl::Ignore,
            ))
        });

    let _keyboard = KEYBOARD.lock();
    let mut port = Port::new(0x60);

    let scancode: u8 = unsafe { port.read() };
    crate::arch::x86_64::drivers::keyboard::add_scancode(scancode);

    unsafe {
        PICS.lock()
            .notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
    }
}

extern "x86-interrupt" fn timer_interrupt_handler(
    _stack_frame: InterruptStackFrame,
) {
    debug!("Timer Interrupt");
    unsafe {
        PICS.lock()
            .notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
    }
}

extern "x86-interrupt" fn page_fault_handler(
    _stack_frame: InterruptStackFrame,
    _error_code: PageFaultErrorCode,
) {
    // serial_println!("Exception: Page Fault");
    // serial_println!("Accessed Address: {:?}", Cr2::read());
    // serial_println!("Error Code: {:?}", _error_code);
    // serial_println!("{:#?}", _stack_frame);

    if let Some(frame_allocator) = FRAME_ALLOCATOR.get() {
        let mut f = frame_allocator.lock();

        let frame = f.allocate_frame().unwrap();
        let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
        let page: Page<Size4KiB> =
            Page::containing_address(Cr2::read().unwrap());

        unsafe {
            let mut mapper = OFFSET_PAGE_TABLE.get().unwrap().lock();

            match mapper.map_to(page, frame, flags, &mut *f) {
                Ok(_) => {}
                Err(why) => panic!("failed to map page: {:?}", why),
            }
        }
        MapperFlushAll::new().flush_all();
    } else {
        panic!("failed to get frame allocator");
    }
}
