#include "menu.h"

#include "resources.h"
#include "../../module/ModuleManager.h"
#include "../../util/globals.h"
#include "../../util/utils.h"

namespace menu
{
    namespace
    {
        bool draw_imgui = false;
        float widthh;
        float heightt;
        float scale_factor;
        float font_size;
        float large_font_size;
        float hud_font_size;
        ImFont* poppins_regular;
        ImFont* poppins_regular_hud;
        ImFont* sfui_light;
        ImFont* sfui_bold;
        ImFont* icons;

        ModuleCategory selected_tab = Aimbot;
    }

    float get_hud_font_size()
    {
        return hud_font_size;
    }

    ImFont* get_hud_font()
    {
        return poppins_regular_hud;
    }

    float get_scale_factor()
    {
        return scale_factor;
    }

    float get_width()
    {
        return widthh;
    }

    float get_height()
    {
        return heightt;
    }

    bool is_active()
    {
        return draw_imgui == true;
    }

    void setup()
    {
        ImGuiIO& io = ImGui::GetIO(); 
        int horizontal = 0;
        int vertical = 0;
        utils::get_desktop_resolution(horizontal, vertical);
        widthh = static_cast<float>(horizontal);
        heightt = static_cast<float>(vertical);
        scale_factor = (static_cast<float>(horizontal) / 1920.0f + static_cast<float>(vertical) / 1080.0f) / 2.0f;
        font_size = 16 * scale_factor;
        large_font_size = 18 * scale_factor;
        hud_font_size = 256 * scale_factor;

         poppins_regular = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Deng.ttf", 15.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());

         ImFontConfig merge_config;
        merge_config.MergeMode = true;
        merge_config.PixelSnapH = true;

        io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Deng.ttf", font_size, NULL, io.Fonts->GetGlyphRangesChineseFull());
 
         poppins_regular_hud = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Deng.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());
        sfui_light = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Deng.ttf", 14.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());
        sfui_bold = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Deng.ttf", 13.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());
        icons = io.Fonts->AddFontFromMemoryCompressedTTF(icons_compressed_data, icons_compressed_size, font_size);
    }

    void draw()
    {

        if (selected_tab == Aimbot && !ModuleManager::get_instance()->has_module("自瞄")) {
            selected_tab = Visuals;
        }
         theme();
        ImGui::GetMouseCursor();
        ImGui::SetMouseCursor(ImGuiMouseCursor_Arrow);
        ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
        ImGui::GetIO().WantCaptureMouse = is_active();
        if (is_active())
        {
            constexpr float width = 880;
            constexpr float height = 550;
            ImGui::SetNextWindowSize(ImVec2(width * scale_factor, height * scale_factor), ImGuiCond_Once);
            ImGui::SetNextWindowBgAlpha(1.0f);
            if (ImGui::Begin(globals::get_display_title().c_str(), &draw_imgui,
                ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
                ImGuiWindowFlags_NoScrollbar))
            {
                 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(14.0f * scale_factor, 6.0f * scale_factor));
                ImGui::BeginTabBar("");

                auto moduleManager = ModuleManager::get_instance();

                if (moduleManager->has_module("自瞄")) {
                    imgui_addons::tab("自瞄", selected_tab == Aimbot, []
                        {
                            selected_tab = Aimbot;
                        });
                }
                imgui_addons::tab("视觉", selected_tab == Visuals, []
                    {
                        selected_tab = Visuals;
                    });
                imgui_addons::tab("功能", selected_tab == Exploit, []
                    {
                        selected_tab = Exploit;
                    });
                imgui_addons::tab("杂项", selected_tab == Misc, []
                    {
                        selected_tab = Misc;
                    });
                imgui_addons::tab("配置", selected_tab == Config, []
                    {
                        selected_tab = Config;
                    });
                ImGui::EndTabBar();
                ImGui::PopStyleVar();
                ImGui::Spacing();
                ImGui::Separator();
                ImGui::Spacing();

                bool overflow = false;
                ImVec2 window_pos = ImGui::GetWindowPos();
                ImVec2 window_size = ImGui::GetWindowSize();
                float window_min = window_pos.x + ImGui::GetCursorPosX();
                float window_max = window_pos.x + window_size.x;
                float window_space = window_max - window_min;
                const float colWidth = 0.5f * window_space - 4 * scale_factor;
                ImGui::BeginChild("##Child1", { colWidth, 0 });
                ModuleManager::get_instance()->apply([window_space, height, colWidth, &overflow](Module* m)
                    {
                        if (m->category != selected_tab) return;
                        ImGui::Dummy({ 0, 3 * scale_factor });
                        ImGui::Indent();
                        ImGui::PushFont(sfui_bold);
                        ImGui::Text(m->name.c_str());
                        ImGui::Dummy({ 0, 4 * scale_factor });
                        ImGui::PopFont();
                        ImGui::Unindent();
                        m->apply([](ConfigValue* c)
                            {
                                ImGui::Indent();
                                c->draw();
                                ImGui::Unindent();
                            });
                         ImGui::Indent();
                        m->draw_menu_extra();
                        ImGui::Unindent();
                         ImGui::Dummy({ 0, 0 });

                        if (ImGui::GetCursorPosY() >= 0.5f * height * scale_factor)
                        {
                            overflow = true;
                            ImGui::EndChild();
                            ImGui::SameLine();
                            ImGui::BeginChild("##Child2", { colWidth, 0 });
                        }
                    });
                if (!overflow)
                {
                    ImGui::EndChild();
                    ImGui::SameLine();
                    ImGui::BeginChild("##Child2", { colWidth, 0 });
                }
                ImGui::EndChild();
            }
            ImGui::End();
        }

         if (GetAsyncKeyState(VK_INSERT) & 1)
        {
            draw_imgui = !draw_imgui;
        }

         auto draw_list = ImGui::GetBackgroundDrawList();
        ModuleManager::get_instance()->apply([draw_list](Module* m)
            {
                m->draw_overlay(draw_list);
            });
    }
}
