#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 SetDialogFOVFixEnabled = nullptr;
static SetBoolFn SetWeaponFOVFixEnabled = nullptr;
static SetBoolFn SetHUDFixEnabled = nullptr;
static SetBoolFn SetPhotoModeFixEnabled = nullptr;
static SetIntFn SetDialogFOV = nullptr;
static SetIntFn SetWeaponFOV = nullptr;
static SetIntFn SetHUDX = nullptr;
static SetIntFn SetHUDY = nullptr;
static GetFloatFn GetDialogFOVIn = nullptr;
static GetFloatFn GetDialogFOVOut = nullptr;
static GetFloatFn GetWeaponFOVIn = nullptr;
static GetFloatFn GetWeaponFOVOut = nullptr;

// Plugin variables for checkboxes and sliders
static bool fix_enabled = false;
static bool dialog_fov_fix_enabled = false;
static bool weapon_fov_fix_enabled = false;
static bool HUD_fix_enabled = false;
static bool Photomode_fix_enabled = false;
static int dialogFOVvalue = 0;
static int weaponFOVvalue = 0;
static int HUDXvalue = 0;
static int HUDYvalue = 0;
static bool popup_Informations = false;

// Plugin settings
const char* SETTINGS_FILE = "PluginSettings.ini";
const char* GENERAL_FIX_SETTING = "GeneralFIX=";
const char* DIALOG_FOV_FIX_SETTING = "DialogFOVFIX=";
const char* WEAPON_FOV_FIX_SETTING = "WeaponFOVFIX=";
const char* HUD_FIX_SETTING = "HUDFIX=";
const char* PHOTOMODE_FIX_SETTING = "PhotomodeFIX=";
const char* DIALOG_FOV_SETTING = "DialogFOV=";
const char* WEAPON_FOV_SETTING = "WeaponFOV=";
const char* HUDX_SETTING = "HUDX=";
const char* HUDY_SETTING = "HUDY=";
const char* FIX_VERSION = "1.0.1";
const char* FIX_INFORMATIONS = "This fix allows to:\r\n - Control dialog and weapon FOV.\r\n - Control HUD safe zone.\r\n - Disable photomode black bars.";
const char* DONATION_URL = "https://buymeacoffee.com/k4sh44";

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

    if (!fixLib) {
        MessageBoxA(nullptr, "Impossible to load game core dll", "Erreur", MB_OK);
        return;
    }

    SetFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetFixEnabled");
    SetDialogFOVFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetDialogFOVFixEnabled");
    SetWeaponFOVFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetWeaponFOVFixEnabled");
    SetHUDFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetHUDFixEnabled");
    SetPhotoModeFixEnabled = (SetBoolFn)GetProcAddress(fixLib, "SetPhotoModeFixEnabled");
    SetDialogFOV = (SetIntFn)GetProcAddress(fixLib, "SetDialogFOV");
    SetWeaponFOV = (SetIntFn)GetProcAddress(fixLib, "SetWeaponFOV");
    SetHUDX = (SetIntFn)GetProcAddress(fixLib, "SetHUDX");
    SetHUDY = (SetIntFn)GetProcAddress(fixLib, "SetHUDY");
    GetDialogFOVIn = (GetFloatFn)GetProcAddress(fixLib, "GetDialogFOVIn");
    GetDialogFOVOut = (GetFloatFn)GetProcAddress(fixLib, "GetDialogFOVOut");
    GetWeaponFOVIn = (GetFloatFn)GetProcAddress(fixLib, "GetWeaponFOVIn");
    GetWeaponFOVOut = (GetFloatFn)GetProcAddress(fixLib, "GetWeaponFOVOut");

    // Apply initial settings loaded from ini file
    if (SetDialogFOV) SetDialogFOV(dialogFOVvalue);
    if (SetWeaponFOV) SetWeaponFOV(weaponFOVvalue);
    if (SetHUDX) SetHUDX(HUDXvalue);
    if (SetHUDY) SetHUDY(HUDYvalue);
    if (SetDialogFOVFixEnabled) SetDialogFOVFixEnabled(dialog_fov_fix_enabled, true);
    if (SetWeaponFOVFixEnabled) SetWeaponFOVFixEnabled(weapon_fov_fix_enabled, true);
    if (SetHUDFixEnabled) SetHUDFixEnabled(HUD_fix_enabled, true);
    if (SetPhotoModeFixEnabled) SetPhotoModeFixEnabled(Photomode_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 << DIALOG_FOV_FIX_SETTING << (dialog_fov_fix_enabled ? "1" : "0") << "\n";
        file << WEAPON_FOV_FIX_SETTING << (weapon_fov_fix_enabled ? "1" : "0") << "\n";
        file << HUD_FIX_SETTING << (HUD_fix_enabled ? "1" : "0") << "\n";
        file << PHOTOMODE_FIX_SETTING << (Photomode_fix_enabled ? "1" : "0") << "\n";
        file << DIALOG_FOV_SETTING << dialogFOVvalue << "\n";
        file << WEAPON_FOV_SETTING << weaponFOVvalue << "\n";
        file << HUDX_SETTING << HUDXvalue << "\n";
        file << HUDY_SETTING << HUDYvalue << "\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(DIALOG_FOV_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(DIALOG_FOV_FIX_SETTING));
                dialog_fov_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(WEAPON_FOV_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(WEAPON_FOV_FIX_SETTING));
                weapon_fov_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(HUD_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(HUD_FIX_SETTING));
                HUD_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(PHOTOMODE_FIX_SETTING) == 0)
            {
                std::string val = line.substr(strlen(PHOTOMODE_FIX_SETTING));
                Photomode_fix_enabled = (val == "1" || val == "true");
            }
            else if (line.find(DIALOG_FOV_SETTING) == 0)
                dialogFOVvalue = std::stoi(line.substr(strlen(DIALOG_FOV_SETTING)));
            else if (line.find(WEAPON_FOV_SETTING) == 0)
                weaponFOVvalue = std::stoi(line.substr(strlen(DIALOG_FOV_SETTING)));
            else if (line.find(HUDX_SETTING) == 0)
                HUDXvalue = std::stof(line.substr(strlen(HUDX_SETTING)));
            else if (line.find(HUDY_SETTING) == 0)
                HUDYvalue = std::stof(line.substr(strlen(HUDY_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();

    // Dialog FOV adjustment
    ImGui::SetCursorPos(ImVec2(10, 120));
    ImGui::BeginChild("DialogFOVHeader", ImVec2(220, 0), false); // true = bordure
    if (ImGui::CollapsingHeader("Dialog additional FOV", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("##DialogFOVSlider", &dialogFOVvalue, 0, 70)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetDialogFOV) SetDialogFOV(dialogFOVvalue);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // Weapon FOV adjustment
    ImGui::SetCursorPos(ImVec2(10, 180));
    ImGui::BeginChild("WeaponFOVHeader", ImVec2(220, 0), false);
    if (ImGui::CollapsingHeader("Weapon additional FOV", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("##WeaponFOVSlider", &weaponFOVvalue, 0, 50)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetWeaponFOV) SetWeaponFOV(weaponFOVvalue);
            SaveSettings();
        }
    }

    ImGui::EndChild();
    // Safe zone X axis
    ImGui::SetCursorPos(ImVec2(10, 240));
    ImGui::BeginChild("HUDXHeader", ImVec2(220, 0), false);
    if (ImGui::CollapsingHeader("HUD width scaling", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("##HUDXSlider", &HUDXvalue, 0, 80)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetHUDX) SetHUDX(HUDXvalue);
            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("Dialog FOV Fix", &dialog_fov_fix_enabled)) {
            if (SetDialogFOVFixEnabled) SetDialogFOVFixEnabled(dialog_fov_fix_enabled, false);
            SaveSettings();
        }

        ImGui::SetCursorPos(ImVec2(5, 55));
        if (ImGui::Checkbox("Weapon FOV Fix", &weapon_fov_fix_enabled)) {
            if (SetWeaponFOVFixEnabled) SetWeaponFOVFixEnabled(weapon_fov_fix_enabled, false);
            SaveSettings();
        }

        ImGui::SetCursorPos(ImVec2(5, 80));
        if (ImGui::Checkbox("HUD safe zone fix", &HUD_fix_enabled)) {
            if (SetHUDFixEnabled) SetHUDFixEnabled(HUD_fix_enabled, false);
            SaveSettings();
        }

        ImGui::SetCursorPos(ImVec2(5, 105));
        if (ImGui::Checkbox("Photomode aspect fix", &Photomode_fix_enabled)) {
            if (SetPhotoModeFixEnabled) SetPhotoModeFixEnabled(Photomode_fix_enabled, false);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // Safe zone Y axis
    ImGui::SetCursorPos(ImVec2(240, 240));
    ImGui::BeginChild("HUDYHeader", ImVec2(220, 0), false); // true = border
    if (ImGui::CollapsingHeader("HUD height scaling", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::SetNextItemWidth(150.0f);
        if (ImGui::SliderInt("##HUDYSlider", &HUDYvalue, 0, 80)) {}
        if (ImGui::IsItemDeactivatedAfterEdit()) {
            if (SetHUDY) SetHUDY(HUDYvalue);
            SaveSettings();
        }
    }
    ImGui::EndChild();

    // Fix status
    ImGui::SetCursorPos(ImVec2(10, 320));
    ImGui::BeginChild("INFOSHeader", ImVec2(480, 80), true); // true = border
    if (ImGui::CollapsingHeader("Fix informations", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::SetCursorPos(ImVec2(5, 30));
        ImGui::Text("Dialog FOV In: %.2f, Out : %.2f", GetDialogFOVIn(), GetDialogFOVOut());
        ImGui::Text("Weapon FOV In: %.2f, Out : %.2f", GetWeaponFOVIn(), GetWeaponFOVOut());
    }
    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("Starfield", &on_overlay_draw);
        reshade::register_event<reshade::addon_event::init_effect_runtime>(
            [](reshade::api::effect_runtime* runtime) {
                LoadFixDLL();
            });
        break;
    case DLL_PROCESS_DETACH:
        fixLib = nullptr;
        reshade::unregister_addon(hinstDLL);
        break;
    }
    return TRUE;
}