#include "ConfigManager.h"
#include <Globals.hxx>
#include <direct.h>

namespace Config {

    std::map<std::string, ConfigValue> Manager::registry;

    void Manager::Init() {
        // Создаём папку для конфигов если нет
        _mkdir(ConfigPath.c_str());

        // Регистрируем все настройки для сохранения
        // AIMBOT
        Register("aimbot.enabled", ConfigValue(Globals::Aimbot::Enabled));
        Register("aimbot.sticky", ConfigValue(Globals::Aimbot::AimbotSticky));
        Register("aimbot.knocked", ConfigValue(Globals::Aimbot::KnockedCheck));
        Register("aimbot.type", ConfigValue(Globals::Aimbot::Aimbot_type));
        Register("aimbot.hitpart", ConfigValue(Globals::Aimbot::HitPart));
        Register("aimbot.fov_draw", ConfigValue(Globals::Aimbot::DrawFov));
        Register("aimbot.fov_size", ConfigValue(Globals::Aimbot::FovSize));
        Register("aimbot.fov_spin", ConfigValue(Globals::Aimbot::FovSpin));
        Register("aimbot.fov_speed", ConfigValue(Globals::Aimbot::FovSpinSpeed));
        Register("aimbot.fov_use", ConfigValue(Globals::Aimbot::useFov));

        // MOUSE AIMBOT
        Register("aimbot.mouse.smooth_x", ConfigValue(Globals::Aimbot::Mouse::Smoothing_X));
        Register("aimbot.mouse.smooth_y", ConfigValue(Globals::Aimbot::Mouse::Smoothing_Y));
        Register("aimbot.mouse.sens", ConfigValue(Globals::Aimbot::Mouse::Mouse_Sensitivty));

        // CAMERA AIMBOT
        Register("aimbot.camera.smooth_x", ConfigValue(Globals::Aimbot::Camera::Smoothing_X));
        Register("aimbot.camera.smooth_y", ConfigValue(Globals::Aimbot::Camera::Smoothing_Y));

        // SILENT
        Register("silent.enabled", ConfigValue(Globals::Silent::Enabled));
        Register("silent.sticky", ConfigValue(Globals::Silent::StickyAim));
        Register("silent.knocked", ConfigValue(Globals::Silent::KnockedCheck));
        Register("silent.part", ConfigValue(Globals::Silent::AimPart));
        Register("silent.fov_draw", ConfigValue(Globals::Silent::DrawFov));
        Register("silent.fov_size", ConfigValue(Globals::Silent::Fov));
        Register("silent.fov_gunbased", ConfigValue(Globals::Silent::GunBasedFov));
        Register("silent.fov_spin", ConfigValue(Globals::Silent::FovSpin));
        Register("silent.fov_speed", ConfigValue(Globals::Silent::FovSpinSpeed));
        Register("silent.fov_use", ConfigValue(Globals::Silent::UseFov));

        // SILENT GUN FOV
        Register("silent.fov_default", ConfigValue(Globals::Silent::Fov));
        Register("silent.fov_double", ConfigValue(Globals::Silent::FovDoubleBarrel));
        Register("silent.fov_tactical", ConfigValue(Globals::Silent::FovTacticalShotgun));
        Register("silent.fov_revolver", ConfigValue(Globals::Silent::FovRevolver));

        // VISUALS
        Register("visuals.enabled", ConfigValue(Globals::Visuals::Enabled));
        Register("visuals.box", ConfigValue(Globals::Visuals::Box));
        Register("visuals.box_fill", ConfigValue(Globals::Visuals::Box_Fill));
        Register("visuals.box_gradient", ConfigValue(Globals::Visuals::Box_Fill_Gradient));
        Register("visuals.box_type", ConfigValue(Globals::Visuals::Box_Type));
        Register("visuals.healthbar", ConfigValue(Globals::Visuals::Healthbar));
        Register("visuals.healthbar_type", ConfigValue(Globals::Visuals::Healthbar_Type));
        Register("visuals.health", ConfigValue(Globals::Visuals::Health));
        Register("visuals.name", ConfigValue(Globals::Visuals::Name));
        Register("visuals.name_type", ConfigValue(Globals::Visuals::Name_Type));
        Register("visuals.distance", ConfigValue(Globals::Visuals::Distance));
        Register("visuals.rig", ConfigValue(Globals::Visuals::Rig_Type));
        Register("visuals.tool", ConfigValue(Globals::Visuals::Tool));
        Register("visuals.skeleton", ConfigValue(Globals::Visuals::Skeleton));
        Register("visuals.chams", ConfigValue(Globals::Visuals::Chams));
        Register("visuals.chams_fade", ConfigValue(Globals::Visuals::ChamsFade));
        Register("visuals.chams_speed", ConfigValue(Globals::Visuals::ChamsFadeSpeed));
        Register("visuals.render_dist", ConfigValue(Globals::Visuals::Render_Distance));
        Register("visuals.thickness", ConfigValue(Globals::Visuals::Thickness));

        // WORLD
        Register("world.skybox", ConfigValue(Globals::World::Skybox));
        Register("world.skybox_type", ConfigValue(Globals::World::Skybox_Type));
        Register("world.rotate", ConfigValue(Globals::World::Rotate));
        Register("world.rotate_speed", ConfigValue(Globals::World::Skybox_Rotate_Speed));
        Register("world.ambience", ConfigValue(Globals::World::Ambience));
        Register("world.fog", ConfigValue(Globals::World::Fog));
        Register("world.fog_dist", ConfigValue(Globals::World::Fog_Distance));
        Register("world.brightness", ConfigValue(Globals::World::Brightness));
        Register("world.brightness_val", ConfigValue(Globals::World::BrightnessI));
        Register("world.exposure", ConfigValue(Globals::World::Exposure));
        Register("world.exposure_val", ConfigValue(Globals::World::ExposureI));
        Register("world.fov", ConfigValue(Globals::World::FOV));
        Register("world.fov_val", ConfigValue(Globals::World::FOV_Distance));

        // MISC
        Register("misc.firerate", ConfigValue(Globals::Misc::firerate));
        Register("misc.firerate_val", ConfigValue(Globals::Misc::fireratevalue));
        Register("misc.fly", ConfigValue(Globals::Misc::Fly));
        Register("misc.fly_speed", ConfigValue(Globals::Misc::Fly_Speed));
        Register("misc.noclip", ConfigValue(Globals::Misc::Noclip));
        Register("misc.desync", ConfigValue(Globals::Misc::Desync));
        Register("misc.desync_chams", ConfigValue(Globals::Misc::DesyncChams));
        Register("misc.hitbox", ConfigValue(Globals::Hitbox::Enabled));
        Register("misc.hitbox_size", ConfigValue(Globals::Hitbox::Size));
        Register("misc.hitbox_headonly", ConfigValue(Globals::Hitbox::HeadOnly));
        Register("misc.hitbox_draw", ConfigValue(Globals::Hitbox::DrawHitbox));
        Register("misc.hitbox_fill", ConfigValue(Globals::Hitbox::FillHitbox));
        Register("misc.hitbox_thick", ConfigValue(Globals::Hitbox::HitboxThickness));

        // SETTINGS
        Register("settings.streamproof", ConfigValue(Globals::Settings::Streamproof));
        Register("settings.perf_mode", ConfigValue(Globals::Settings::Performance_Mode));
        Register("settings.wobbly", ConfigValue(Globals::Settings::Wobbly_Enabled));
        Register("settings.wobbly_str", ConfigValue(Globals::Settings::Wobbly_Strength));
        Register("settings.wobbly_spd", ConfigValue(Globals::Settings::Wobbly_Speed));

        // COLORS (пример - добавь остальные по аналогии)
        Register("color.aimbot_fov", ConfigValue(Globals::Aimbot::FovColor));
        Register("color.silent_fov", ConfigValue(Globals::Silent::FovColor));
        Register("color.box", ConfigValue(Globals::Visuals::Colors::Box));
        Register("color.box_fill_top", ConfigValue(Globals::Visuals::Colors::BoxFill_Top));
        Register("color.box_fill_bot", ConfigValue(Globals::Visuals::Colors::BoxFill_Bottom));
        Register("color.healthbar", ConfigValue(Globals::Visuals::Colors::Healthbar));
        Register("color.health", ConfigValue(Globals::Visuals::Colors::Health));
        Register("color.name", ConfigValue(Globals::Visuals::Colors::Name));
        Register("color.distance", ConfigValue(Globals::Visuals::Colors::Distance));
        Register("color.rig", ConfigValue(Globals::Visuals::Colors::Rig_Type));
        Register("color.tool", ConfigValue(Globals::Visuals::Colors::Tool));
        Register("color.skeleton", ConfigValue(Globals::Visuals::Colors::Skeleton));
        Register("color.chams", ConfigValue(Globals::Visuals::Colors::Chams));
        Register("color.chams_outline", ConfigValue(Globals::Visuals::Colors::ChamsOutline));
        Register("color.ghost", ConfigValue(Globals::Misc::DesyncChamsColor));
        Register("color.ghost_outline", ConfigValue(Globals::Misc::DesyncChamsOutline));
        Register("color.hitbox", ConfigValue(Globals::Hitbox::HitboxColor));
        Register("color.hitbox_fill", ConfigValue(Globals::Hitbox::HitboxFillColor));
    }

    void Manager::Register(const std::string& key, const ConfigValue& value) {
        registry[key] = value;
    }

    std::string Manager::Serialize(const ConfigValue& val) {
        std::ostringstream oss;
        switch (val.type) {
        case ConfigValue::BOOL:
            oss << "bool:" << (val.data.b ? "1" : "0");
            break;
        case ConfigValue::INT:
            oss << "int:" << val.data.i;
            break;
        case ConfigValue::FLOAT:
            oss << "float:" << val.data.f;
            break;
        case ConfigValue::STRING:
            oss << "string:" << val.s;
            break;
        case ConfigValue::COLOR:
            oss << "color:" << val.color[0] << ","
                << val.color[1] << ","
                << val.color[2] << ","
                << val.color[3];
            break;
        }
        return oss.str();
    }

    ConfigValue Manager::Deserialize(const std::string& str, ConfigValue::Type type) {
        ConfigValue result(type);
        size_t colon = str.find(':');
        if (colon == std::string::npos) return result;

        std::string value = str.substr(colon + 1);

        switch (type) {
        case ConfigValue::BOOL:
            result.data.b = (value == "1");
            break;
        case ConfigValue::INT:
            result.data.i = std::stoi(value);
            break;
        case ConfigValue::FLOAT:
            result.data.f = std::stof(value);
            break;
        case ConfigValue::STRING:
            result.s = value;
            break;
        case ConfigValue::COLOR: {
            std::istringstream iss(value);
            std::string token;
            int i = 0;
            while (std::getline(iss, token, ',') && i < 4) {
                result.color[i++] = std::stof(token);
            }
            break;
        }
        }
        return result;
    }

    void Manager::Save(const std::string& name) {
        std::string filepath = ConfigPath + name + ".extera";
        std::ofstream file(filepath);
        if (!file.is_open()) return;

        file << "# Fang Config File\n";
        file << "# Generated: " << __DATE__ << " " << __TIME__ << "\n\n";

        for (const auto& [key, value] : registry) {
            file << key << "=" << Serialize(value) << "\n";
        }

        file.close();
    }

    void Manager::Load(const std::string& name) {
        std::string filepath = ConfigPath + name + ".extera";
        std::ifstream file(filepath);
        if (!file.is_open()) return;

        std::string line;
        while (std::getline(file, line)) {
            // Пропускаем комментарии и пустые строки
            if (line.empty() || line[0] == '#') continue;

            size_t eq = line.find('=');
            if (eq == std::string::npos) continue;

            std::string key = line.substr(0, eq);
            std::string value = line.substr(eq + 1);

            // Ищем в реестре и загружаем
            auto it = registry.find(key);
            if (it != registry.end()) {
                ConfigValue loaded = Deserialize(value, it->second.type);

                // Применяем к глобальным переменным
                ApplyConfig(key, loaded);
            }
        }

        file.close();
    }

    // Применяет загруженное значение к соответствующей переменной
    void Manager::ApplyConfig(const std::string& key, const ConfigValue& val) {
        // AIMBOT
        if (key == "aimbot.enabled" && val.type == ConfigValue::BOOL)
            Globals::Aimbot::Enabled = val.data.b;
        else if (key == "aimbot.sticky" && val.type == ConfigValue::BOOL)
            Globals::Aimbot::AimbotSticky = val.data.b;
        else if (key == "aimbot.knocked" && val.type == ConfigValue::BOOL)
            Globals::Aimbot::KnockedCheck = val.data.b;
        else if (key == "aimbot.type" && val.type == ConfigValue::INT)
            Globals::Aimbot::Aimbot_type = val.data.i;
        else if (key == "aimbot.hitpart" && val.type == ConfigValue::INT)
            Globals::Aimbot::HitPart = val.data.i;
        else if (key == "aimbot.fov_draw" && val.type == ConfigValue::BOOL)
            Globals::Aimbot::DrawFov = val.data.b;
        else if (key == "aimbot.fov_size" && val.type == ConfigValue::FLOAT)
            Globals::Aimbot::FovSize = val.data.f;
        else if (key == "aimbot.fov_spin" && val.type == ConfigValue::BOOL)
            Globals::Aimbot::FovSpin = val.data.b;
        else if (key == "aimbot.fov_speed" && val.type == ConfigValue::INT)
            Globals::Aimbot::FovSpinSpeed = val.data.i;
        else if (key == "aimbot.fov_use" && val.type == ConfigValue::BOOL)
            Globals::Aimbot::useFov = val.data.b;

        // MOUSE
        else if (key == "aimbot.mouse.smooth_x" && val.type == ConfigValue::FLOAT)
            Globals::Aimbot::Mouse::Smoothing_X = val.data.f;
        else if (key == "aimbot.mouse.smooth_y" && val.type == ConfigValue::FLOAT)
            Globals::Aimbot::Mouse::Smoothing_Y = val.data.f;
        else if (key == "aimbot.mouse.sens" && val.type == ConfigValue::FLOAT)
            Globals::Aimbot::Mouse::Mouse_Sensitivty = val.data.f;

        // CAMERA
        else if (key == "aimbot.camera.smooth_x" && val.type == ConfigValue::FLOAT)
            Globals::Aimbot::Camera::Smoothing_X = val.data.f;
        else if (key == "aimbot.camera.smooth_y" && val.type == ConfigValue::FLOAT)
            Globals::Aimbot::Camera::Smoothing_Y = val.data.f;

        // SILENT
        else if (key == "silent.enabled" && val.type == ConfigValue::BOOL)
            Globals::Silent::Enabled = val.data.b;
        else if (key == "silent.sticky" && val.type == ConfigValue::BOOL)
            Globals::Silent::StickyAim = val.data.b;
        else if (key == "silent.knocked" && val.type == ConfigValue::BOOL)
            Globals::Silent::KnockedCheck = val.data.b;
        else if (key == "silent.part" && val.type == ConfigValue::INT)
            Globals::Silent::AimPart = val.data.i;
        else if (key == "silent.fov_draw" && val.type == ConfigValue::BOOL)
            Globals::Silent::DrawFov = val.data.b;
        else if (key == "silent.fov_size" && val.type == ConfigValue::FLOAT)
            Globals::Silent::Fov = val.data.f;
        else if (key == "silent.fov_gunbased" && val.type == ConfigValue::BOOL)
            Globals::Silent::GunBasedFov = val.data.b;
        else if (key == "silent.fov_spin" && val.type == ConfigValue::BOOL)
            Globals::Silent::FovSpin = val.data.b;
        else if (key == "silent.fov_speed" && val.type == ConfigValue::INT)
            Globals::Silent::FovSpinSpeed = val.data.i;
        else if (key == "silent.fov_use" && val.type == ConfigValue::BOOL)
            Globals::Silent::UseFov = val.data.b;

        // SILENT GUN FOV
        else if (key == "silent.fov_default" && val.type == ConfigValue::FLOAT)
            Globals::Silent::Fov = val.data.f;
        else if (key == "silent.fov_double" && val.type == ConfigValue::FLOAT)
            Globals::Silent::FovDoubleBarrel = val.data.f;
        else if (key == "silent.fov_tactical" && val.type == ConfigValue::FLOAT)
            Globals::Silent::FovTacticalShotgun = val.data.f;
        else if (key == "silent.fov_revolver" && val.type == ConfigValue::FLOAT)
            Globals::Silent::FovRevolver = val.data.f;

        // VISUALS
        else if (key == "visuals.enabled" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Enabled = val.data.b;
        else if (key == "visuals.box" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Box = val.data.b;
        else if (key == "visuals.box_fill" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Box_Fill = val.data.b;
        else if (key == "visuals.box_gradient" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Box_Fill_Gradient = val.data.b;
        else if (key == "visuals.box_type" && val.type == ConfigValue::INT)
            Globals::Visuals::Box_Type = val.data.i;
        else if (key == "visuals.healthbar" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Healthbar = val.data.b;
        else if (key == "visuals.healthbar_type" && val.type == ConfigValue::INT)
            Globals::Visuals::Healthbar_Type = val.data.i;
        else if (key == "visuals.health" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Health = val.data.b;
        else if (key == "visuals.name" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Name = val.data.b;
        else if (key == "visuals.name_type" && val.type == ConfigValue::INT)
            Globals::Visuals::Name_Type = val.data.i;
        else if (key == "visuals.distance" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Distance = val.data.b;
        else if (key == "visuals.rig" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Rig_Type = val.data.b;
        else if (key == "visuals.tool" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Tool = val.data.b;
        else if (key == "visuals.skeleton" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Skeleton = val.data.b;
        else if (key == "visuals.chams" && val.type == ConfigValue::BOOL)
            Globals::Visuals::Chams = val.data.b;
        else if (key == "visuals.chams_fade" && val.type == ConfigValue::BOOL)
            Globals::Visuals::ChamsFade = val.data.b;
        else if (key == "visuals.chams_speed" && val.type == ConfigValue::INT)
            Globals::Visuals::ChamsFadeSpeed = val.data.i;
        else if (key == "visuals.render_dist" && val.type == ConfigValue::FLOAT)
            Globals::Visuals::Render_Distance = val.data.f;
        else if (key == "visuals.thickness" && val.type == ConfigValue::INT)
            Globals::Visuals::Thickness = val.data.i;

        // WORLD
        else if (key == "world.skybox" && val.type == ConfigValue::BOOL)
            Globals::World::Skybox = val.data.b;
        else if (key == "world.skybox_type" && val.type == ConfigValue::INT)
            Globals::World::Skybox_Type = val.data.i;
        else if (key == "world.rotate" && val.type == ConfigValue::BOOL)
            Globals::World::Rotate = val.data.b;
        else if (key == "world.rotate_speed" && val.type == ConfigValue::FLOAT)
            Globals::World::Skybox_Rotate_Speed = val.data.f;
        else if (key == "world.ambience" && val.type == ConfigValue::BOOL)
            Globals::World::Ambience = val.data.b;
        else if (key == "world.fog" && val.type == ConfigValue::BOOL)
            Globals::World::Fog = val.data.b;
        else if (key == "world.fog_dist" && val.type == ConfigValue::FLOAT)
            Globals::World::Fog_Distance = val.data.f;
        else if (key == "world.brightness" && val.type == ConfigValue::BOOL)
            Globals::World::Brightness = val.data.b;
        else if (key == "world.brightness_val" && val.type == ConfigValue::FLOAT)
            Globals::World::BrightnessI = val.data.f;
        else if (key == "world.exposure" && val.type == ConfigValue::BOOL)
            Globals::World::Exposure = val.data.b;
        else if (key == "world.exposure_val" && val.type == ConfigValue::FLOAT)
            Globals::World::ExposureI = val.data.f;
        else if (key == "world.fov" && val.type == ConfigValue::BOOL)
            Globals::World::FOV = val.data.b;
        else if (key == "world.fov_val" && val.type == ConfigValue::FLOAT)
            Globals::World::FOV_Distance = val.data.f;

        // MISC
        else if (key == "misc.firerate" && val.type == ConfigValue::BOOL)
            Globals::Misc::firerate = val.data.b;
        else if (key == "misc.firerate_val" && val.type == ConfigValue::FLOAT)
            Globals::Misc::fireratevalue = val.data.f;
        else if (key == "misc.fly" && val.type == ConfigValue::BOOL)
            Globals::Misc::Fly = val.data.b;
        else if (key == "misc.fly_speed" && val.type == ConfigValue::FLOAT)
            Globals::Misc::Fly_Speed = val.data.f;
        else if (key == "misc.noclip" && val.type == ConfigValue::BOOL)
            Globals::Misc::Noclip = val.data.b;
        else if (key == "misc.desync" && val.type == ConfigValue::BOOL)
            Globals::Misc::Desync = val.data.b;
        else if (key == "misc.desync_chams" && val.type == ConfigValue::BOOL)
            Globals::Misc::DesyncChams = val.data.b;
        else if (key == "misc.hitbox" && val.type == ConfigValue::BOOL)
            Globals::Hitbox::Enabled = val.data.b;
        else if (key == "misc.hitbox_size" && val.type == ConfigValue::FLOAT)
            Globals::Hitbox::Size = val.data.f;
        else if (key == "misc.hitbox_headonly" && val.type == ConfigValue::BOOL)
            Globals::Hitbox::HeadOnly = val.data.b;
        else if (key == "misc.hitbox_draw" && val.type == ConfigValue::BOOL)
            Globals::Hitbox::DrawHitbox = val.data.b;
        else if (key == "misc.hitbox_fill" && val.type == ConfigValue::BOOL)
            Globals::Hitbox::FillHitbox = val.data.b;
        else if (key == "misc.hitbox_thick" && val.type == ConfigValue::FLOAT)
            Globals::Hitbox::HitboxThickness = val.data.f;

        // SETTINGS
        else if (key == "settings.streamproof" && val.type == ConfigValue::BOOL)
            Globals::Settings::Streamproof = val.data.b;
        else if (key == "settings.perf_mode" && val.type == ConfigValue::INT)
            Globals::Settings::Performance_Mode = val.data.i;
        else if (key == "settings.wobbly" && val.type == ConfigValue::BOOL)
            Globals::Settings::Wobbly_Enabled = val.data.b;
        else if (key == "settings.wobbly_str" && val.type == ConfigValue::FLOAT)
            Globals::Settings::Wobbly_Strength = val.data.f;
        else if (key == "settings.wobbly_spd" && val.type == ConfigValue::FLOAT)
            Globals::Settings::Wobbly_Speed = val.data.f;

        // COLORS
        else if (key == "color.aimbot_fov" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Aimbot::FovColor[i] = val.color[i];
        else if (key == "color.silent_fov" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Silent::FovColor[i] = val.color[i];
        else if (key == "color.box" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Box[i] = val.color[i];
        else if (key == "color.box_fill_top" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::BoxFill_Top[i] = val.color[i];
        else if (key == "color.box_fill_bot" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::BoxFill_Bottom[i] = val.color[i];
        else if (key == "color.healthbar" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Healthbar[i] = val.color[i];
        else if (key == "color.health" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Health[i] = val.color[i];
        else if (key == "color.name" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Name[i] = val.color[i];
        else if (key == "color.distance" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Distance[i] = val.color[i];
        else if (key == "color.rig" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Rig_Type[i] = val.color[i];
        else if (key == "color.tool" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Tool[i] = val.color[i];
        else if (key == "color.skeleton" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Skeleton[i] = val.color[i];
        else if (key == "color.chams" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::Chams[i] = val.color[i];
        else if (key == "color.chams_outline" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Visuals::Colors::ChamsOutline[i] = val.color[i];
        else if (key == "color.ghost" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Misc::DesyncChamsColor[i] = val.color[i];
        else if (key == "color.ghost_outline" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Misc::DesyncChamsOutline[i] = val.color[i];
        else if (key == "color.hitbox" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Hitbox::HitboxColor[i] = val.color[i];
        else if (key == "color.hitbox_fill" && val.type == ConfigValue::COLOR)
            for (int i = 0; i < 4; i++) Globals::Hitbox::HitboxFillColor[i] = val.color[i];
    }

    std::vector<std::string> Manager::ListConfigs() {
        std::vector<std::string> configs;
        try {
            // Создаём папку если нет
            std::filesystem::create_directories(ConfigPath);

            for (const auto& entry : std::filesystem::directory_iterator(ConfigPath)) {
                if (entry.path().extension() == ".extera") {
                    configs.push_back(entry.path().stem().string());
                }
            }
        }
        catch (const std::exception& e) {
            // Можно логировать: Output::Error("Config error: %s", e.what());
        }
        catch (...) {
            // Игнорируем неизвестные ошибки
        }
        return configs;
    }

    void Manager::DeleteConfig(const std::string& name) {
        std::string filepath = ConfigPath + name + ".extera";
        std::filesystem::remove(filepath);
    }

}