#include "Keyboard.h"
#include "Client.h"
#include "Utils/FileUtils.h"
#include "Utils/AudioUtils.h"

#include "Mod/Interface.h"

#include "Datas/UserDatas.h"

void Hooks::Keyboard::callback(int key, bool held) {
    Client::keymaps[key] = held;

    if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('L') & 0x8000)) {
        Client::running = false;
    }

    if (key == 'U' && held) {
        Client::openConfigmanager = !Client::openConfigmanager;
    }

    bool held_ = held;
    bool cancel = false;
    for (Mod* mod : Client::modlist) {
        if (!mod->enabled && mod->onlyenabled) continue;
        
        mod->onKeyboard(key, &held_, &cancel);
    }

    if (Client::screenName != _("chat_screen")) {
        for (Mod* mod : Client::modlist) {
            for (int ckey : mod->keys) {
                if (key == ckey && (mod->onlyheld ? held : true) && mod != Client::findMod<Interface>(_("Interface"))) {
                    mod->enabled = !mod->enabled;
                    mod->enabled ? mod->onEnabled() : mod->onDisabled();

                    for (Mod* mod2 : Client::modlist) {
                        if (!mod2->enabled && mod2->onlyenabled) continue;

                        mod->enabled ? mod2->onAnyEnabled() : mod2->onAnyDisabled();
                    }
                }
            }
        }
    }

    if (Client::cancelInputs) return;
    if (Client::forceCancelinputs) return;

    if (!cancel) 
        keyboard(key, held_);
}
