// configs.h
#include <filesystem>
#include <fstream>
#include <vector>
#include <string>
#include "../utils/settings.hpp"

namespace fs = std::filesystem;

class ConfigManager {
private:
    std::string configPath = "C:\\XW\\";
    std::vector<std::string> availableConfigs;

public:
    ConfigManager() {
        if (!fs::exists(configPath)) {
            fs::create_directories(configPath);
        }
    }

    void RefreshConfigList() {
        availableConfigs.clear();
        if (fs::exists(configPath)) {
            for (const auto& entry : fs::directory_iterator(configPath)) {
                if (entry.path().extension() == ".cfg") {
                    availableConfigs.push_back(entry.path().stem().string());
                }
            }
        }
    }

    bool SaveConfig(const std::string& name) {
        std::string fullPath = configPath + name + ".cfg";
        std::ofstream file(fullPath);

        if (!file.is_open()) {
            return false;
        }

        file << "[Aimbot]\n";
        file << "enabled=" << var->c_aimbot.enabled << "\n";
        file << "memoryaim=" << var->c_aimbot.memory_or_mouse << "\n";
        file << "smoothsize=" << var->c_aimbot.smooth << "\n";
        file << "fovsize=" << var->c_aimbot.fov << "\n";
        file << "hitbox=" << var->c_aimbot.aim_bone << "\n";
        file << "aimkey=" << var->c_aimbot.keybind << "\n";
        file << "targetPriority=" << var->c_aimbot.aim_priority << "\n";
        file << "prediction=" << var->c_aimbot.prediction << "\n";
        file << "drawfov=" << var->c_aimbot.draw_fov << "\n";
        file << "vischeck=" << var->c_aimbot.visible_check << "\n";

        file << "\n[ESP]\n";
        file << "box=" << var->c_esp.player_box << "\n";
        file << "boxOutlined=" << settings::esp.boxOutlined << "\n";
        file << "snapline=" << var->c_esp.snaplines << "\n";
        file << "distance=" << var->c_esp.distance << "\n";
        file << "username=" << var->c_esp.username << "\n";
        file << "weaponesp=" << var->c_esp.weapon << "\n";
        file << "platform=" << var->c_esp.platform << "\n";
        file << "rank=" << var->c_esp.rank << "\n";
        file << "skeletonesp=" << var->c_esp.skeleton_esp << "\n";
        file << "espdis=" << var->c_esp.esp_distance << "\n";
        file << "textsize=" << var->c_esp.text_size << "\n";

        file << "\n[Colors]\n";
        file << "visible_r=" << var->c_esp.box_vis_col[0] << "\n";
        file << "visible_g=" << var->c_esp.box_vis_col[1] << "\n";
        file << "visible_b=" << var->c_esp.box_vis_col[2] << "\n";
        file << "visible_a=" << var->c_esp.box_vis_col[3] << "\n";
        file << "notVisible_r=" << var->c_esp.box_invis_col[0] << "\n";
        file << "notVisible_g=" << var->c_esp.box_invis_col[1] << "\n";
        file << "notVisible_b=" << var->c_esp.box_invis_col[2] << "\n";
        file << "notVisible_a=" << var->c_esp.box_invis_col[3] << "\n";
        file << "skeleton_visible_r=" << var->c_esp.skel_vis_col[0] << "\n";
        file << "skeleton_visible_g=" << var->c_esp.skel_vis_col[1] << "\n";
        file << "skeleton_visible_b=" << var->c_esp.skel_vis_col[2] << "\n";
        file << "skeleton_visible_a=" << var->c_esp.skel_vis_col[3] << "\n";
        file << "skeleton_invisible_r=" << var->c_esp.skel_invis_col[0] << "\n";
        file << "skeleton_invisible_g=" << var->c_esp.skel_invis_col[1] << "\n";
        file << "skeleton_invisible_b=" << var->c_esp.skel_invis_col[2] << "\n";
        file << "skeleton_invisible_a=" << var->c_esp.skel_invis_col[3] << "\n";

        file << "\n[UI]\n";
        file << "bullet_tp="<< var->c_exploits.bullet_teleport << "\n";
        file << "no_spread="<< var->c_exploits.no_spread << "\n";
        file << "ads_in_air="<< var->c_exploits.ads_in_air << "\n";
        file << "car_gravity_mod="<< var->c_exploits.car_gravity_mod << "\n";
        file << "car_fly="<< var->c_exploits.car_fly << "\n";
        file << "car_speed_mod="<< var->c_exploits.car_speed_mod << "\n";
        file << "car_fly_value="<< settings::exploits.carflyvalue << "\n";
        file << "car_speed_value="<< settings::exploits.carspeedvalue << "\n";
        file << "bullet_speed="<< settings::exploits.speedycuh << "\n";
        file << "bullet_speed_value="<< settings::exploits.speedycuhspeed << "\n";
        file << "vsync=" << var->c_settings.vsync << "\n";


        file.close();
        RefreshConfigList();
        return true;
    }

    bool LoadConfig(const std::string& name) {
        std::string fullPath = configPath + name + ".cfg";
        std::ifstream file(fullPath);

        if (!file.is_open()) {
            return false;
        }

        std::string line;
        std::string currentSection;

        while (std::getline(file, line)) {
            if (line.empty()) continue;

            if (line[0] == '[') {
                currentSection = line;
                continue;
            }

            size_t pos = line.find('=');
            if (pos != std::string::npos) {
                std::string key = line.substr(0, pos);
                std::string value = line.substr(pos + 1);

                if (currentSection == "[Aimbot]") {
                    if (key == "enabled") var->c_aimbot.enabled = (value == "1");
                    else if (key == "memoryaim") var->c_aimbot.memory_or_mouse = (value == "1");
                    else if (key == "smoothsize") var->c_aimbot.smooth = std::stoi(value);
                    else if (key == "fovsize") var->c_aimbot.fov = std::stoi(value);
                    else if (key == "hitbox") var->c_aimbot.aim_bone = std::stoi(value);
                    else if (key == "aimkey") var->c_aimbot.keybind = std::stoi(value);
                    else if (key == "targetPriority") var->c_aimbot.aim_priority = std::stoi(value);
                    else if (key == "prediction") var->c_aimbot.prediction = (value == "1");
                    else if (key == "drawfov") var->c_aimbot.draw_fov = (value == "1");
                    else if (key == "vischeck") var->c_aimbot.visible_check = (value == "1");
                }
                else if (currentSection == "[ESP]") {
                    if (key == "box") var->c_esp.player_box = (value == "1");
                    else if (key == "boxOutlined") settings::esp.boxOutlined = (value == "1");
                    else if (key == "snapline") var->c_esp.snaplines = (value == "1");
                    else if (key == "distance") var->c_esp.distance = (value == "1");
                    else if (key == "username") var->c_esp.username = (value == "1");
                    else if (key == "weaponesp") var->c_esp.weapon = (value == "1");
                    else if (key == "platform") var->c_esp.platform = (value == "1");
                    else if (key == "rank") var->c_esp.rank = (value == "1");
                    else if (key == "skeletonesp") var->c_esp.skeleton_esp = (value == "1");
                    else if (key == "espdis") var->c_esp.esp_distance = std::stoi(value);
                    else if (key == "textsize") var->c_esp.text_size = std::stoi(value);
                }
                else if (currentSection == "[Colors]") {
                    if (key == "visible_r") var->c_esp.box_vis_col[0] = std::stof(value);
                    else if (key == "visible_g") var->c_esp.box_vis_col[1] = std::stof(value);
                    else if (key == "visible_b") var->c_esp.box_vis_col[2] = std::stof(value);
                    else if (key == "visible_a") var->c_esp.box_vis_col[3] = std::stof(value);
                    else if (key == "notVisible_r") var->c_esp.box_invis_col[0] = std::stof(value);
                    else if (key == "notVisible_g") var->c_esp.box_invis_col[1] = std::stof(value);
                    else if (key == "notVisible_b") var->c_esp.box_invis_col[2] = std::stof(value);
                    else if (key == "notVisible_a") var->c_esp.box_invis_col[3] = std::stof(value);
                    else if (key == "skeleton_visible_r") var->c_esp.skel_vis_col[0] = std::stof(value);
                    else if (key == "skeleton_visible_g") var->c_esp.skel_vis_col[1] = std::stof(value);
                    else if (key == "skeleton_visible_b") var->c_esp.skel_vis_col[2] = std::stof(value);
                    else if (key == "skeleton_visible_a") var->c_esp.skel_vis_col[3] = std::stof(value);
                    else if (key == "skeleton_invisible_r") var->c_esp.skel_invis_col[0] = std::stof(value);
                    else if (key == "skeleton_invisible_g") var->c_esp.skel_invis_col[1] = std::stof(value);
                    else if (key == "skeleton_invisible_b") var->c_esp.skel_invis_col[2] = std::stof(value);
                    else if (key == "skeleton_invisible_a") var->c_esp.skel_invis_col[3] = std::stof(value);
                }
                else if (currentSection == "[UI]") {
                    if (key == "vsync") var->c_settings.vsync = (value == "1");
                    else if (key == "bullet_tp") var->c_exploits.bullet_teleport = (value == "1");
                    else if (key == "no_spread") var->c_exploits.no_spread = (value == "1");
                    else if (key == "ads_in_air") var->c_exploits.ads_in_air = (value == "1");
                    else if (key == "car_gravity_mod") var->c_exploits.car_gravity_mod = (value == "1");
                    else if (key == "car_fly") var->c_exploits.car_fly = (value == "1");
                    else if (key == "car_speed_mod") var->c_exploits.car_speed_mod = (value == "1");
                    else if (key == "car_fly_value") settings::exploits.carflyvalue = std::stoi(value);
                    else if (key == "car_speed_value") settings::exploits.carspeedvalue = std::stoi(value);
                    else if (key == "bullet_speed") settings::exploits.speedycuh = (value == "1");
                    else if (key == "bullet_speed_value") settings::exploits.speedycuhspeed = std::stoi(value);
                }
            }
        }

        file.close();
        return true;
    }

    bool DeleteConfig(const std::string& name) {
        std::string fullPath = configPath + name + ".cfg";
        if (fs::exists(fullPath)) {
            fs::remove(fullPath);
            RefreshConfigList();
            return true;
        }
        return false;
    }

    const std::vector<std::string>& GetConfigs() const {
        return availableConfigs;
    }
};

