#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 void (*SetFloatFn)(float);
typedef float (*GetFloatFn)();

static HMODULE fixLib = nullptr;
static SetBoolFn SetFixEnabled = nullptr;
static SetBoolFn SetFOVFixEnabled = nullptr;
static SetBoolFn SetPhotoFOVFixEnabled = nullptr;
static SetBoolFn SetCinematicsARFixEnabled = nullptr;
static SetBoolFn SetAspectRatioFixEnabled = nullptr;
static SetBoolFn SetDOFFixEnabled = nullptr;
static SetBoolFn SetCAFixEnabled = nullptr;
static SetBoolFn SetBDFixEnabled = nullptr;
static SetIntFn SetFOV = nullptr;
static SetIntFn SetPhotoFOV = nullptr;
static SetFloatFn SetAspect = nullptr;
static GetFloatFn GetFOVIn = nullptr;
static GetFloatFn GetFOVOut = nullptr;

// Plugin variables for checkboxes and sliders
static bool fov_fix_enabled = false;
static bool photo_fov_fix_enabled = false;
static bool cinematics_ar_fix_enabled = false;
static bool aspect_ratio_fix_enabled = false;
static bool DOF_fix_enabled = false;
static bool CA_fix_enabled = false;
static bool BD_fix_enabled = false;
static bool fix_enabled = false;
static int worldFOVvalue = 0;
static int photoFOVvalue = 0;
static bool popup_Informations = false;

// Screen informations
static int screenWidth = GetSystemMetrics(SM_CXSCREEN);
static int screenHeight = GetSystemMetrics(SM_CYSCREEN);
static float aspectRatiovalue = (float)screenWidth / screenHeight;

// Plugin settings
const char* SETTINGS_FILE = "PluginSettings.ini";
const char* GENERAL_FIX_SETTING = "GeneralFIX=";
const char* WORLD_FOV_FIX_SETTING = "WorldFOVFIX=";
const char* PHOTO_FOV_FIX_SETTING = "PhotoFOVFIX=";
const char* CINEMATICS_AR_FIX_SETTING = "CinematicsARFIX=";
const char* ASPECT_FIX_SETTING = "AspectFIX=";
const char* DOF_FIX_SETTING = "DOFFIX=";
const char* CA_FIX_SETTING = "CAFIX=";
const char* BD_FIX_SETTING = "BDFIX=";
const char* WORLD_FOV_SETTING = "WorldFOV=";
const char* PHOTO_FOV_SETTING = "PhotoFOV=";
const char* ASPECT_SETTING = "Aspect=";
const char* FIX_VERSION = "1.0.6";
const char* FIX_INFORMATIONS = "This fix allows to :\r\n - Change FOV in game and photo mode.\r\n - Remove cinematics black borders.\r\n - Override game aspect ratio limitation.\r\n - Controls in game and cinematics depth of field, chromatic aberrations and barrel distortion.";
const char* DONATION_URL = "https://buymeacoffee.com/k4sh44";

// Load and unload game core dll functions /!\ necessary
static void LoadFixDLL()
{
    if (fixLib) return;
    fixLib = LoadLibraryA("UnchartedLOTCCore.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");
    SetPhotoFOVFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetPhotoFOVFixEnabled");
    SetCinematicsARFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetCinematicsARFixEnabled");
    SetAspectRatioFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetAspectRatioFixEnabled");
    SetDOFFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetDOFFixEnabled");
    SetCAFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetCAFixEnabled");
    SetBDFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetBDFixEnabled");
    SetFOV = (SetIntFn)GetProcAddress(fixLib, "SetFOV");
    SetPhotoFOV = (SetIntFn)GetProcAddress(fixLib, "SetPhotoFOV");
    GetFOVIn = (GetFloatFn)GetProcAddress(fixLib, "GetFOVIn");
    GetFOVOut = (GetFloatFn)GetProcAddress(fixLib, "GetFOVOut");;
    SetAspect = (SetFloatFn)GetProcAddress(fixLib, "SetAspect");

    // Appliquer les valeurs initiales chargées
    if (SetFOV) SetFOV(worldFOVvalue);
    if (SetPhotoFOV) SetPhotoFOV(photoFOVvalue);
    if (SetAspect) SetAspect(aspectRatiovalue);
    if (SetFOVFixEnabled) SetFOVFixEnabled(fov_fix_enabled, true);
    if (SetPhotoFOVFixEnabled) SetPhotoFOVFixEnabled(photo_fov_fix_enabled, true);
    if (SetCinematicsARFixEnabled) SetCinematicsARFixEnabled(cinematics_ar_fix_enabled, true);
    if (SetAspectRatioFixEnabled) SetAspectRatioFixEnabled(aspect_ratio_fix_enabled, true);
    if (SetDOFFixEnabled) SetDOFFixEnabled(DOF_fix_enabled, true);
    if (SetCAFixEnabled) SetCAFixEnabled(CA_fix_enabled, true);
    if (SetBDFixEnabled) SetBDFixEnabled(BD_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 << PHOTO_FOV_FIX_SETTING << (photo_fov_fix_enabled ? "1" : "0") << "\n";
        file << CINEMATICS_AR_FIX_SETTING << (cinematics_ar_fix_enabled ? "1" : "0") << "\n";        
        file << ASPECT_FIX_SETTING << (aspect_ratio_fix_enabled ? "1" : "0") << "\n";
        file << DOF_FIX_SETTING << (DOF_fix_enabled ? "1" : "0") << "\n";
        file << CA_FIX_SETTING << (CA_fix_enabled ? "1" : "0") << "\n";
        file << BD_FIX_SETTING << (BD_fix_enabled ? "1" : "0") << "\n";
        file << WORLD_FOV_SETTING << worldFOVvalue << "\n";
        file << PHOTO_FOV_SETTING << photoFOVvalue << "\n";
        file << ASPECT_SETTING << aspectRatiovalue << "\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(PHOTO_FOV_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(PHOTO_FOV_FIX_SETTING));
                photo_fov_fix_enabled = (val == "1" || val == "true");
            }            
            else if (line.find(CINEMATICS_AR_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(CINEMATICS_AR_FIX_SETTING));
                cinematics_ar_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(ASPECT_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(ASPECT_FIX_SETTING));
                aspect_ratio_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(DOF_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(DOF_FIX_SETTING));
                DOF_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(CA_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(CA_FIX_SETTING));
                CA_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(BD_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(BD_FIX_SETTING));
                BD_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(PHOTO_FOV_SETTING) == 0)
                photoFOVvalue = std::stoi(line.substr(strlen(PHOTO_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 = border
    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);
    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("PhotoFOVHeader", ImVec2(220, 0), false);
    if (ImGui::CollapsingHeader("Photo mode additional FOV", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("", &photoFOVvalue, -20, 50)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetPhotoFOV) SetPhotoFOV(photoFOVvalue);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    ImGui::SetCursorPos(ImVec2(10, 240));
    ImGui::BeginChild("AspectHeader", ImVec2(220, 0), false);
    if (ImGui::CollapsingHeader("Game aspect ratio", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        if (ImGui::InputFloat("", &aspectRatiovalue, 0.0f, 0.0f, "% .2f")) {
            if (SetAspect) SetAspect(aspectRatiovalue);
            SaveSettings();
        }
        ImGui::SetNextItemWidth(150.0f);
    }
    ImGui::EndChild();
    // Individual fixes
    ImGui::SetCursorPos(ImVec2(240, 60));
    ImGui::BeginChild("IndividualFixesHeader", ImVec2(250, 0), false);
    if (ImGui::CollapsingHeader("Individual fixes", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30)); // FOV
        if (ImGui::Checkbox("Enable FOV fix", &fov_fix_enabled)) {
            if (SetFOVFixEnabled) SetFOVFixEnabled(fov_fix_enabled, false);
            SaveSettings();
        }
        ImGui::SetCursorPos(ImVec2(5, 55)); // Photo FOV
        if (ImGui::Checkbox("Photo FOV fix", &photo_fov_fix_enabled)) {
            if (SetPhotoFOVFixEnabled) SetPhotoFOVFixEnabled(photo_fov_fix_enabled, false);
            SaveSettings();
        }
        ImGui::SetCursorPos(ImVec2(5, 80)); // Cinematics Aspect
        if (ImGui::Checkbox("Cinematics aspect ratio Fix", &cinematics_ar_fix_enabled)) {
            if (SetCinematicsARFixEnabled) SetCinematicsARFixEnabled(cinematics_ar_fix_enabled, false);
            SaveSettings();
        }
        ImGui::SetCursorPos(ImVec2(5, 105)); // Aspect ratio
        if (ImGui::Checkbox("Aspect ratio Fix", &aspect_ratio_fix_enabled)) {
            if (SetAspectRatioFixEnabled) SetAspectRatioFixEnabled(aspect_ratio_fix_enabled, false);
            SaveSettings();
        }
        ImGui::SetCursorPos(ImVec2(5, 130)); // DOF
        if (ImGui::Checkbox("Depth of field Fix", &DOF_fix_enabled)) {
            if (SetDOFFixEnabled) SetDOFFixEnabled(DOF_fix_enabled, false);
            SaveSettings();
        }
        ImGui::SetCursorPos(ImVec2(5, 155)); // DOF
        if (ImGui::Checkbox("Chromatic aberrations Fix", &CA_fix_enabled)) {
            if (SetCAFixEnabled) SetCAFixEnabled(CA_fix_enabled, false);
            SaveSettings();
        }
        ImGui::SetCursorPos(ImVec2(5, 180)); // Chromatic aberrations
        if (ImGui::Checkbox("Barrel distortion Fix", &BD_fix_enabled)) {
            if (SetBDFixEnabled) SetBDFixEnabled(BD_fix_enabled, false);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // Fix status
    ImGui::SetCursorPos(ImVec2(10, 300));
    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::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("Uncharted LOTC", &on_overlay_draw);
        reshade::register_event<reshade::addon_event::init_effect_runtime>(
            [](reshade::api::effect_runtime* runtime) {
                    LoadFixDLL();
            });

        break;
    case DLL_PROCESS_DETACH:
        reshade::unregister_addon(hinstDLL);
        break;
    }
    return TRUE;
}