#pragma once
#include <windows.h> 
#include "../game_state.h"
#include "../Module.h"
#include "../ModuleCategory.h"
#include "../../config/CheckboxValue.h"
#include "../../il2cpp/Il2CppObjectInstance.h"
#include "../../il2cpp/unity.h"
#include "../../gui/menu/menu.h"

class WorldModule : Module
{
public:
    WorldModule() : Module("世界", Exploit)
    {
    }

    ActionRowValue* unlock_doors = conf(new ActionRowValue([&]
        {
            unlock_doors_impl();
        }, "解锁门"));
    bool queue_unlock_doors = false;

    void unlock_doors_impl()
    {
        if (!game_state::is_in_raid) return;
        queue_unlock_doors = true;
    }

private:
     int unlock_doors_key = 0;
    bool unlock_doors_key_pressed = false; 
    int* waiting_for_key_ptr = nullptr;

     std::string get_key_name(int key_code)
    {
        if (key_code == 0) return "NONE";
        if (key_code > 0 && key_code < 165) return menu::KeyNames[key_code];
        return "Key " + std::to_string(key_code);
    }


    void draw_keybind_button(const char* label, int& key_var)
    {
        ImGui::Text("%s", label);

        float btn_width = 85.0f;
        ImGui::SameLine(ImGui::GetWindowWidth() - btn_width - 20.0f);

        std::string btn_text;
        if (waiting_for_key_ptr == &key_var) {
            btn_text = "[ ... ]";
        }
        else {
            btn_text = "[ " + get_key_name(key_var) + " ]";
        }

        ImGui::PushID(label);
        if (ImGui::Button(btn_text.c_str(), ImVec2(btn_width, 0))) {
            waiting_for_key_ptr = &key_var;
        }
        ImGui::PopID();

         if (waiting_for_key_ptr == &key_var) {
            for (int i = 3; i < 256; i++) {
                if (GetAsyncKeyState(i) & 0x8000) {
                    if (i == VK_ESCAPE) {
                        key_var = 0;
                    }
                    else {
                        key_var = i;
                    }
                    waiting_for_key_ptr = nullptr;
                    break;
                }
            }
        }
    }

public:
     void draw_menu_extra() override
    {
         ImGui::Dummy(ImVec2(0, 8.0f));
         ImGui::Separator();
         ImGui::Dummy(ImVec2(0, 5.0f));
         ImGui::TextColored(ImVec4(0.15f, 0.85f, 0.15f, 1.00f), "快捷键绑定");
         ImGui::Dummy(ImVec2(0, 5.0f));

         draw_keybind_button("解锁门快捷键", unlock_doors_key);
    }

    void draw_overlay(ImDrawList* draw_list) override
    {
    }

    void application_update() override
    {
         if (unlock_doors_key != 0 && waiting_for_key_ptr == nullptr)
        {
            if (GetAsyncKeyState(unlock_doors_key) & 0x8000)
            {
                if (!unlock_doors_key_pressed)
                {
                    unlock_doors_impl(); 
                    unlock_doors_key_pressed = true;
                }
            }
            else
            {
                unlock_doors_key_pressed = false;
            }
        }
    }

    void gameworld_update(const Il2CppClass* game_world_class, Il2CppObjectInstance game_world_instance,
        Il2CppObjectInstance main_player) override
    {
        if (queue_unlock_doors)
        {
            queue_unlock_doors = false;

            Il2CppObject* door_system_type = il2utils::get_system_type(game_world_class->image, "EFT.Interactive",
                "Door");
            size_t found_doors = 0;
            Il2CppObject** doors = unity::find_objects_of_type(door_system_type, &found_doors);
            for (size_t i = 0; i < found_doors; ++i)
            {
                Il2CppObjectInstance door_instance(doors[i]);
                 door_instance.get_method<void(*)(Il2CppObject*, int)>("set_DoorState", 1)(
                    door_instance.get_instance(), 2);
                if (globals::verbose) logger::info("Unlocked Doors");
            }
        }
    }

    void init() override
    {
    }
};