#include <shade/platform/ports.h>
#include <shade/platform/drivers/vga_text_mode.h>
#include <shade/platform/interrupts/idt.h>
#include <shade/platform/interrupts/isr.h>
#include <shade/platform/interrupts/pic.h>
#include <shade/platform/drivers/keyboard.h>
#include <shade/platform/ports.h>
#include <shade/version.h>
#include <printf.h>
#include <shade/kmsg.h>

// Welcome to Shade!
// shadeOS kernel version 0.2.2
// Running on shade-development
// shadeOS willow, build b4e5b2e, compiled blah

void kernel_welcome() {
    kprintf("Welcome to \x1b[0;36mShade\x1b[0m!\n");
    kprintf("shadeOS kernel version \x1b[0;33m%s\x1b[0m\n", SHADE_OS_KERNEL_VERSION);
    kprintf("Running on \x1b[0;33m%s\x1b[0m\n", SHADE_OS_KERNEL);
    kprintf("shadeOS %s (%s), compiled %s\n", SHADE_OS_CODENAME, SHADE_OS_BUILD, SHADE_OS_COMPILE_DATE);
}

// kMain
void kmain() {
    // Initialize screen buffer
	struct Char* buffer = (struct Char*) 0xb8000;
    init_state();
    
    clear_all(); // Clear screen
    set_cursor_pos(0, 0); // Set cursor position
    print_set_color(PRINT_COLOR_WHITE, PRINT_COLOR_BLACK); // Set print color

    // welcome messages
    kmsg_ok("Initialized display successfully\n");
    kernel_welcome();

    kprintf("Copyright (c) e3team 2022. All rights reserved.\n");
    kprintf("This program is provided \"as-is\" and no express or implied warranty is provided.\n");
    kprintf("The full license can be found at /sys/LICENCE on this system or ./LICENCE in the source tree.\n");

    pic_remap(0x20, 0x28);
    fill_isr(); // ISR must be filled before interrupts are enabled
    idt_assemble();
    kmsg_ok("Enabled interrupts");

    init_keyboard();

    idt_disable_interrupts();
    outb(0x21,0xfd);
    outb(0xa1,0xff);
    idt_enable_interrupts();

    kmsg_ok("Enabled keyboard");

    __asm__ __volatile__ ("int $2");

    for (;;) {}
}