#define IMGUI_DISABLE_INCLUDE_IMCONFIG_H
#define IMGUI_HAS_DOCK 1

#include <imgui.h>
#include <reshade.hpp>
#include <fstream>
#include <string>

// Core game dll functions declarations
typedef void (*SetBoolFn)(bool,bool);
typedef void (*SetIntFn)(int);
typedef float (*GetFloatFn)();

static HMODULE fixLib = nullptr;
static SetBoolFn SetFixEnabled = nullptr;
static SetBoolFn SetFOVFixEnabled = nullptr;
static SetBoolFn SetCinematicsFOVFixEnabled = nullptr;
static SetBoolFn SetCinematicsFTFixEnabled = nullptr;
static SetIntFn SetFOV = nullptr;
static SetIntFn SetCinematicsFOV = nullptr;
static GetFloatFn GetFOVIn = nullptr;
static GetFloatFn GetFOVOut = nullptr;
static GetFloatFn GetCinematicsFOVIn = nullptr;
static GetFloatFn GetCinematicsCompensadedFOV = nullptr;
static GetFloatFn GetCinematicsFOVOut = nullptr;

// Plugin variables for checkboxes and sliders
static bool fix_enabled = false;
static bool fov_fix_enabled = false;
static bool cinematics_fov_fix_enabled = false;
static bool cinematics_DOF_fix_enabled = false;
static bool cinematics_FT_fix_enabled = false;
static bool cinematics_FG_fix_enabled = false;
static int worldFOVvalue = 0;
static int cinematicsFOVvalue = 0;
static bool popup_Informations = false;

// Plugin settings
const char* SETTINGS_FILE = "PluginSettings.ini";
const char* GENERAL_FIX_SETTING = "GeneralFIX=";
const char* WORLD_FOV_FIX_SETTING = "WorldFOVFIX=";
const char* CINEMATICS_FOV_FIX_SETTING = "CinematicsFOVFIX=";
const char* CINEMATICS_FT_FIX_SETTING = "CinematicsFTFIX=";
const char* WORLD_FOV_SETTING = "WorldFOV=";
const char* CINEMATICS_FOV_SETTING = "CinematicsFOV=";
const char* FIX_VERSION = "1.0.3";
const char* FIX_INFORMATIONS = "This fix allows to:\r\n - Modify FOV in game\r\n - Modify Cinematics FOV.\r\n - Disable depth of field in cutscenes.\r\n - Unlock cinematics frametime.\r\n - Enable frame generation with cutscenes.\r\n\r\nThe game has a narrow FOV in cinematics for displays with a ratio above 21/9.\r\nCinematics FOV will be compensated for ultrawide displays.\r\nCinematics FOV won't affect world or menus FOVs.\r\nDepth of field fix should affect all the game.";
const char* DONATION_URL = "https://buymeacoffee.com/k4sh44";

// Load and unload game core dll functions /!\ necessary
static void LoadFixDLL()
{
    if (fixLib) return;
    fixLib = LoadLibraryA("IndianaJonesTGCCore.dll");

    if (!fixLib) {
        MessageBoxA(nullptr, "Impossible to load game core dll", "Erreur", MB_OK);
        return;
    }
    SetFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetFixEnabled");
    SetFOVFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetFOVFixEnabled");
    SetCinematicsFOVFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetCinematicsFOVFixEnabled");
    SetCinematicsFTFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetCinematicsFTFixEnabled");
    SetFOV = (SetIntFn)GetProcAddress(fixLib, "SetFOV");
    SetCinematicsFOV = (SetIntFn)GetProcAddress(fixLib, "SetCinematicsFOV");
    GetFOVIn = (GetFloatFn)GetProcAddress(fixLib, "GetFOVIn");
    GetFOVOut = (GetFloatFn)GetProcAddress(fixLib, "GetFOVOut");
    GetCinematicsFOVIn = (GetFloatFn)GetProcAddress(fixLib, "GetCinematicsFOVIn");
    GetCinematicsCompensadedFOV = (GetFloatFn)GetProcAddress(fixLib, "GetCinematicsCompensadedFOV");
    GetCinematicsFOVOut = (GetFloatFn)GetProcAddress(fixLib, "GetCinematicsFOVOut");

    // Apply initial plugin values
    if (SetFOV) SetFOV(worldFOVvalue);
    if (SetCinematicsFOV) SetCinematicsFOV(cinematicsFOVvalue);
    if (SetFOVFixEnabled) SetFOVFixEnabled(fov_fix_enabled, true);
    if (SetCinematicsFOVFixEnabled) SetCinematicsFOVFixEnabled(cinematics_fov_fix_enabled, true);
    if (SetCinematicsFTFixEnabled) SetCinematicsFTFixEnabled(cinematics_FT_fix_enabled, true);
    if (SetFixEnabled) SetFixEnabled(fix_enabled, true);
}

// Addon functions
static void SaveSettings()
{
    std::ofstream file(SETTINGS_FILE);
    if (file.is_open())
    {
        file << GENERAL_FIX_SETTING << (fix_enabled ? "1" : "0") << "\n";
        file << WORLD_FOV_FIX_SETTING << (fov_fix_enabled ? "1" : "0") << "\n";
        file << CINEMATICS_FOV_FIX_SETTING << (cinematics_fov_fix_enabled ? "1" : "0") << "\n";
        file << CINEMATICS_FT_FIX_SETTING << (cinematics_FT_fix_enabled ? "1" : "0") << "\n";
        file << WORLD_FOV_SETTING << worldFOVvalue << "\n";
        file << CINEMATICS_FOV_SETTING << cinematicsFOVvalue << "\n";
        file.close();
    }
}

static void LoadSettings()
{
    std::ifstream file(SETTINGS_FILE);
    if (file.is_open())
    {
        std::string line;
        while (std::getline(file, line))
        {
            if (line.find(GENERAL_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(GENERAL_FIX_SETTING));
                fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(WORLD_FOV_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(WORLD_FOV_FIX_SETTING));
                fov_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(CINEMATICS_FOV_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(CINEMATICS_FOV_FIX_SETTING));
                cinematics_fov_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(CINEMATICS_FT_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(CINEMATICS_FT_FIX_SETTING));
                cinematics_FT_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(WORLD_FOV_SETTING) == 0)
                worldFOVvalue = std::stoi(line.substr(strlen(WORLD_FOV_SETTING)));
            else if (line.find(CINEMATICS_FOV_SETTING) == 0)
                cinematicsFOVvalue = std::stoi(line.substr(strlen(CINEMATICS_FOV_SETTING)));
        }
        file.close();
    }
}

static void displayFixInformations() {
    // Fix version
    ImGui::Begin("Informations", &popup_Informations); //
    ImGui::SetCursorPos(ImVec2(10, 36));
    ImGui::Text("Version : %s", FIX_VERSION);
    ImGui::SetCursorPos(ImVec2(10, 76));
    ImGui::Text(FIX_INFORMATIONS);
    ImGui::End();
}

// Initialize ImGui widgets for Reshade
static void on_overlay_draw(reshade::api::effect_runtime* runtime)
{
    ImGui::SetNextWindowPos(ImVec2(100, 200), ImGuiCond_Once);
    ImGui::SetNextWindowSize(ImVec2(350, 150), ImGuiCond_Once);

    // Donation ?
    ImGui::SetCursorPos(ImVec2(10, 36));
    ImGui::Text("Like my work ?");
    ImGui::SetCursorPos(ImVec2(130, 33));
    if (ImGui::Button("consider donation"))
    {
        ShellExecuteA(NULL, "open", DONATION_URL, NULL, NULL, SW_SHOWNORMAL);
    }

    // Fix informations
    ImGui::SetCursorPos(ImVec2(270, 33));
    if (ImGui::Button("Fix informations"))
        popup_Informations = true;

    if (popup_Informations)
        displayFixInformations();

    // Generic fix
    ImGui::SetCursorPos(ImVec2(10, 60));
    ImGui::BeginChild("AllFixesHeader", ImVec2(220, 0), false); // true = bordure
    if (ImGui::CollapsingHeader("Enable fixes", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        if (ImGui::Checkbox("Fix enabled", &fix_enabled)) {
            if (SetFixEnabled) SetFixEnabled(fix_enabled, false);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // FOV adjustment
    ImGui::SetCursorPos(ImVec2(10, 120));
    ImGui::BeginChild("FOVHeader", ImVec2(220, 0), false); // true = bordure
    if (ImGui::CollapsingHeader("In game additional FOV", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("", &worldFOVvalue, -20, 50)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetFOV) SetFOV(worldFOVvalue);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    ImGui::SetCursorPos(ImVec2(10, 180));
    ImGui::BeginChild("CinematicsFOVHeader", ImVec2(220, 0), false); // true = bordure
    if (ImGui::CollapsingHeader("Cinematics additional FOV", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("", &cinematicsFOVvalue, -20, 50)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetCinematicsFOV) SetCinematicsFOV(cinematicsFOVvalue);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // Individual fixes
    ImGui::SetCursorPos(ImVec2(240, 60));
    ImGui::BeginChild("IndividualFixesHeader", ImVec2(250, 0), false); // true = bordure
    if (ImGui::CollapsingHeader("Individual fixes", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        if (ImGui::Checkbox("Enable FOV Fix", &fov_fix_enabled)) {
            if (SetFOVFixEnabled) SetFOVFixEnabled(fov_fix_enabled, false);
            SaveSettings();
        }

        ImGui::SetCursorPos(ImVec2(5, 55));
        if (ImGui::Checkbox("Cinematics FOV Fix", &cinematics_fov_fix_enabled)) {
            if (SetCinematicsFOVFixEnabled) SetCinematicsFOVFixEnabled(cinematics_fov_fix_enabled, false);
            SaveSettings();
        }

        ImGui::SetCursorPos(ImVec2(5, 80));
        if (ImGui::Checkbox("Cinematics frametime Fix", &cinematics_FT_fix_enabled)) {
            if (SetCinematicsFTFixEnabled) SetCinematicsFTFixEnabled(cinematics_FT_fix_enabled, false);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // Fix status
    ImGui::SetCursorPos(ImVec2(10, 240));
    ImGui::BeginChild("INFOSHeader", ImVec2(480, 80), true);
    if (ImGui::CollapsingHeader("Fix informations", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::Text("FOV In: %.2f, Out : %.2f", GetFOVIn(), GetFOVOut());
        ImGui::Text("Cinematics FOV In: %.2f, Compensated : %.2f, Out : %.2f", GetCinematicsFOVIn(), GetCinematicsCompensadedFOV(), GetCinematicsFOVOut());
    }
    ImGui::EndChild();
}

// Main dll intrance
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        if (!reshade::register_addon(hinstDLL))
            return FALSE;

        LoadSettings();
        reshade::register_overlay("Indiana Jones And The Great Circle", &on_overlay_draw);
        reshade::register_event<reshade::addon_event::init_effect_runtime>( // Will load asi core only once
            [](reshade::api::effect_runtime* runtime) {
                LoadFixDLL();
            });
        break;
    case DLL_PROCESS_DETACH:
        reshade::unregister_addon(hinstDLL);
        break;
    }
    return TRUE;
}