#include <stdint.h>
#include <stdio.h>

#include "sm83.h"
#include "mem.h"
#include "sm83dis.h"
#include "debug.h"
#include "gbscreen.h"
#include <SDL2/SDL.h>
#include <string.h>
#include "ui.h"
#include "ui_menu.h"
#include "ui_window.h"

uint32_t time = 0;
uint32_t t_states = 0;

namespace actions
{
    void exitMenu()
    {
        const uint8_t dt = sm83::step();
        debug::cont();
        gbscreen::refresh(dt);
    }

    int decZoom(int value)
    {
        gbscreen::decZoom();
        return 0;
    }

    int incZoom(int value)
    {
        gbscreen::incZoom();
        return 0;
    }

    int fullscreen(int value)
    {
        gbscreen::toggleFullscreen();
        return gbscreen::getFullscreen();
    }

    int showAnalyzer(int value)
    {
        //z80analyze::show();
        return 0;
    }
}

int main(int argc, char *argv[])
{
    if (argc < 2) { printf("ABORTING: No rom specified.\n"); exit(1); }

    const uint32_t clock = 4194304;
    const uint32_t update_freq = clock >> 3;

    SDL_Init(SDL_INIT_EVERYTHING);

    FILE *f = fopen(argv[1], "rb");
    if (!f) { printf("ABORTING: Rom not found.\n"); exit(1); }
    fseek(f, 0, SEEK_END);
    const int filesize = ftell(f);
    fseek(f, 0, SEEK_SET);
    uint8_t *buffer = (uint8_t*)malloc(filesize);
    fread(buffer, filesize, 1, f);
    fclose(f);

    mem::init(buffer, filesize);

    sm83dis::loadSymbols();
    sm83::reset();

    gbscreen::init(0);
    debug::init();

    ui::menu::init();
    ui::menu::setexitcallback(actions::exitMenu);

    int menu = ui::menu::addsubmenu("FILE");
    ui::menu::addoption(menu, "LOAD ROM", nullptr);
    ui::menu::addoption(menu, "SAVE ROM", nullptr);
    ui::menu::addseparator(menu);
    ui::menu::addoption(menu, "LOAD STATE", nullptr);
    ui::menu::addoption(menu, "SAVE STATE", nullptr);

    menu = ui::menu::addsubmenu("SCREEN");
    ui::menu::addoption(menu, "DEC ZOOM", actions::decZoom);
    ui::menu::addoption(menu, "INC ZOOM", actions::incZoom);
    ui::menu::addbooloption(menu, "FULLSCREEN", gbscreen::getFullscreen(), actions::fullscreen);

    menu = ui::menu::addsubmenu("EMULATION");
    ui::menu::addbooloption(menu, "STOP ON INVALID OP", sm83::getOption(SM83_OPTION_STOP_ON_INVALID), actions::decZoom);
    ui::menu::addoption(menu, "SHOW ANALYZER", actions::showAnalyzer);

    //zx_ula::sound_init();

    //debug::stop();

    bool should_exit = false;
    SDL_Event e;

    time = SDL_GetTicks();
    t_states = 0;

    while (!should_exit)
    {
        while (SDL_PollEvent(&e))
        {
            bool result = true;

            if (e.type == SDL_QUIT) { should_exit=true; break; }
            else if (e.type == SDL_MOUSEBUTTONDOWN) result = ui::window::sendEvent(e.button.windowID, &e);
            else if (e.type == SDL_MOUSEBUTTONUP) result = ui::window::sendEvent(e.button.windowID, &e);
            else if (e.type == SDL_MOUSEMOTION) result = ui::window::sendEvent(e.motion.windowID, &e);
            else if (e.type == SDL_WINDOWEVENT) result = ui::window::sendEvent(e.window.windowID, &e);
            else if (e.type == SDL_MOUSEWHEEL) result = ui::window::sendEvent(e.wheel.windowID, &e);
            else if (e.type == SDL_TEXTINPUT) result = ui::window::sendEvent(e.text.windowID, &e);
            else if (e.type == SDL_KEYDOWN) {
                if (e.key.keysym.scancode==SDL_SCANCODE_F5) {
                    if (debug::debugging()) {
                        debug::history::gototop();
                        const uint8_t dt = sm83::step();
                        debug::cont();
                        gbscreen::refresh(dt);
                    }
                } else if (e.key.keysym.scancode==SDL_SCANCODE_F8) {
                    if (!debug::debugging()) {
                        debug::stop();
                        gbscreen::redraw();
                    } else {
                        debug::show();
                    }
                } else if (e.key.keysym.scancode==SDL_SCANCODE_F10) {
                    if (debug::debugging()) {
                        debug::show();
                        debug::history::gototop();
                        const uint8_t dt = sm83::step();
                        debug::refresh();
                        gbscreen::refresh(dt);
                        gbscreen::redraw();
                        //z80analyze::refresh();
                    }
                } else if (e.key.keysym.scancode==SDL_SCANCODE_F11) {
                    if (debug::debugging()) {
                        debug::show();
                        debug::history::gototop();
                        const uint8_t dt = debug::next();
                        debug::refresh();
                        gbscreen::refresh(dt);
                        gbscreen::redraw();
                        //z80analyze::refresh();
                    }
                } else if (e.key.keysym.scancode==SDL_SCANCODE_F12) {
                    if (debug::debugging()) {
                        debug::show();
                        debug::history::gototop();
                        const uint8_t dt = debug::stepout();
                        debug::refresh();
                        gbscreen::refresh(dt);
                        gbscreen::redraw();
                        //z80analyze::refresh();
                    }
                }
                result = ui::window::sendEvent(e.key.windowID, &e);
            }
            else if (e.type == SDL_MOUSEBUTTONUP && e.button.button==1) ui::setClicked(true);

            if (!result)
                should_exit = true; break;
        }

        if (!debug::debugging() && !debug::paused()) {
            bool fastload=false;

            // En cada bucle fem 10 pasos de la CPU, sino s'ofega
            for (int i=0;i<20;++i) {
                if (debug::isbreak(sm83::getPC(), 9)) {
                    debug::stop();
                    gbscreen::redraw();
                    break;
                } else {
                    uint8_t dt = sm83::step();
                    t_states += dt;
                    //zx_ula::sound_update(dt);
                    gbscreen::refresh(dt);
                    if (debug::debugging()) break;
                }
            }

            if (t_states>=update_freq)
            {
                while (SDL_GetTicks()<(time+125)) {}
                t_states -= update_freq;
                time = SDL_GetTicks();
                //z80analyze::refresh();
            }
            //z80analyze::refresh(true);


        } else if (!debug::debugging() && debug::paused()) {
            gbscreen::redraw(false);
            ui::menu::show();
            gbscreen::present();
        }
        ui::setClicked(false);
    }

    return 0;
}
