#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <vector>
#include <random>

// 特效类型枚举
enum class EffectType {
    PARTICLE,    // 粒子特效
    LIGHT,       // 光影特效
    SOUND,       // 声音特效
    UI,          // UI特效
    PHYSICS      // 物理特效
};

// 特效延迟管理器（单例模式）
class EffectDelayManager {
private:
    static EffectDelayManager* instance;
    
    // 延迟配置参数
    struct DelayConfig {
        int baseDelayMs;           // 基础延迟
        int maxDelayMs;            // 最大延迟
        float intensity;          // 强度系数
        bool isEnabled;           // 是否启用
    };
    
    std::vector<DelayConfig> effectConfigs;
    bool isGlobalDelayActive;
    int globalDelayMultiplier;    // 全局延迟倍率
    
    // 随机数生成器（用于模拟自然延迟波动）
    std::mt19937 rng;
    std::uniform_int_distribution<int> delayVariation;
    
    EffectDelayManager() : isGlobalDelayActive(false), globalDelayMultiplier(1) {
        // 初始化随机数生成器
        std::random_device rd;
        rng = std::mt19937(rd());
        delayVariation = std::uniform_int_distribution<int>(-5, 5);
        
        // 初始化各特效类型的默认延迟配置
        InitializeDefaultConfigs();
    }
    
    void InitializeDefaultConfigs() {
        effectConfigs.resize(5); // 对应5种特效类型
        
        // 粒子特效配置
        effectConfigs[static_cast<int>(EffectType::PARTICLE)] = {20, 100, 1.0f, true};
        // 光影特效配置
        effectConfigs[static_cast<int>(EffectType::LIGHT)] = {10, 50, 1.0f, true};
        // 声音特效配置
        effectConfigs[static_cast<int>(EffectType::SOUND)] = {5, 20, 1.0f, true};
        // UI特效配置
        effectConfigs[static_cast<int>(EffectType::UI)] = {2, 10, 1.0f, true};
        // 物理特效配置
        effectConfigs[static_cast<int>(EffectType::PHYSICS)] = {15, 60, 1.0f, true};
    }

public:
    static EffectDelayManager* GetInstance() {
        if (!instance) instance = new EffectDelayManager();
        return instance;
    }
    
    // 设置特定特效类型的延迟
    void SetEffectDelay(EffectType type, int baseDelayMs, int maxDelayMs, float intensity = 1.0f) {
        int index = static_cast<int>(type);
        if (index >= 0 && index < effectConfigs.size()) {
            effectConfigs[index].baseDelayMs = baseDelayMs;
            effectConfigs[index].maxDelayMs = maxDelayMs;
            effectConfigs[index].intensity = intensity;
        }
    }
    
    // 启用/禁用特定特效类型的延迟
    void SetEffectEnabled(EffectType type, bool enabled) {
        int index = static_cast<int>(type);
        if (index >= 0 && index < effectConfigs.size()) {
            effectConfigs[index].isEnabled = enabled;
        }
    }
    
    // 设置全局延迟倍率
    void SetGlobalDelayMultiplier(int multiplier) {
        globalDelayMultiplier = std::max(1, std::min(10, multiplier)); // 限制在1-10倍
        isGlobalDelayActive = (multiplier > 1);
    }
    
    // 应用特效延迟（核心方法）
    void ApplyEffectDelay(EffectType type, const std::string& effectName = "") {
        int index = static_cast<int>(type);
        if (index < 0 || index >= effectConfigs.size()) return;
        
        DelayConfig& config = effectConfigs[index];
        if (!config.isEnabled && !isGlobalDelayActive) return;
        
        // 计算实际延迟时间（基础延迟 + 随机波动）
        int actualDelay = config.baseDelayMs + delayVariation(rng);
        actualDelay = std::max(0, std::min(config.maxDelayMs, actualDelay));
        actualDelay *= globalDelayMultiplier;
        actualDelay = static_cast<int>(actualDelay * config.intensity);
        
        if (actualDelay > 0) {
            // 模拟延迟效果
            std::this_thread::sleep_for(std::chrono::milliseconds(actualDelay));
            
            // 输出延迟信息（调试用）
            if (!effectName.empty()) {
                std::string typeName;
                switch(type) {
                    case EffectType::PARTICLE: typeName = "粒子"; break;
                    case EffectType::LIGHT: typeName = "光影"; break;
                    case EffectType::SOUND: typeName = "声音"; break;
                    case EffectType::UI: typeName = "UI"; break;
                    case EffectType::PHYSICS: typeName = "物理"; break;
                }
                
                std::string colorCode = "\033[33m"; // 黄色
                if (actualDelay > 50) colorCode = "\033[31m"; // 红色
                else if (actualDelay <= 10) colorCode = "\033[32m"; // 绿色
                
                std::cout << colorCode << "[特效延迟] " << typeName << "特效: " << effectName 
                         << " - 延迟: " << actualDelay << "ms\033[0m" << std::endl;
            }
        }
    }
    
    // 获取当前延迟状态信息
    void PrintDelayStatus() {
        std::cout << "\n=== 特效延迟状态 ===" << std::endl;
        std::cout << "全局延迟倍率: " << globalDelayMultiplier << "x" << std::endl;
        std::cout << "各特效延迟配置:" << std::endl;
        
        std::vector<std::string> typeNames = {"粒子", "光影", "声音", "UI", "物理"};
        for (int i = 0; i < effectConfigs.size(); ++i) {
            const DelayConfig& config = effectConfigs[i];
            std::string status = config.isEnabled ? "启用" : "禁用";
            std::cout << "  " << typeNames[i] << ": " << config.baseDelayMs << "ms (" 
                     << status << ", 强度:" << config.intensity << ")" << std::endl;
        }
        std::cout << "==================" << std::endl;
    }
};

// 初始化单例实例
EffectDelayManager* EffectDelayManager::instance = nullptr;

// 模拟游戏特效系统
class GameEffectSystem {
private:
    EffectDelayManager* delayManager;
    int frameCount;

public:
    GameEffectSystem() : frameCount(0) {
        delayManager = EffectDelayManager::GetInstance();
    }
    
    // 模拟粒子特效生成
    void SpawnParticleEffect(const std::string& effectName) {
        delayManager->ApplyEffectDelay(EffectType::PARTICLE, effectName);
        std::cout << "生成粒子特效: " << effectName << std::endl;
    }
    
    // 模拟光影特效
    void PlayLightEffect(const std::string& effectName) {
        delayManager->ApplyEffectDelay(EffectType::LIGHT, effectName);
        std::cout << "播放光影特效: " << effectName << std::endl;
    }
    
    // 模拟声音特效
    void PlaySoundEffect(const std::string& effectName) {
        delayManager->ApplyEffectDelay(EffectType::SOUND, effectName);
        std::cout << "播放声音特效: " << effectName << std::endl;
    }
    
    // 模拟UI特效
    void ShowUIEffect(const std::string& effectName) {
        delayManager->ApplyEffectDelay(EffectType::UI, effectName);
        std::cout << "显示UI特效: " << effectName << std::endl;
    }
    
    // 模拟物理特效
    void SimulatePhysicsEffect(const std::string& effectName) {
        delayManager->ApplyEffectDelay(EffectType::PHYSICS, effectName);
        std::cout << "模拟物理特效: " << effectName << std::endl;
    }
    
    // 游戏主循环
    void GameMainLoop() {
        while (true) {
            std::cout << "\n--- 帧 " << frameCount << " ---" << std::endl;
            
            // 每10帧改变一次全局延迟倍率，模拟性能波动
            if (frameCount % 10 == 0) {
                int randomMultiplier = 1 + (frameCount / 10) % 3; // 在1-3倍之间循环
                delayManager->SetGlobalDelayMultiplier(randomMultiplier);
            }
            
            // 模拟各种特效的播放
            if (frameCount % 2 == 0) {
                SpawnParticleEffect("爆炸火花");
            }
            if (frameCount % 3 == 0) {
                PlayLightEffect("武器闪光");
            }
            if (frameCount % 4 == 0) {
                PlaySoundEffect("射击音效");
            }
            if (frameCount % 5 == 0) {
                ShowUIEffect("伤害数字");
            }
            if (frameCount % 6 == 0) {
                SimulatePhysicsEffect("碎片飞散");
            }
            
            // 每20帧显示一次延迟状态
            if (frameCount % 20 == 0) {
                delayManager->PrintDelayStatus();
            }
            
            // 模拟正常帧间隔
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
            frameCount++;
            
            // 限制演示时长（实际游戏中可移除）
            if (frameCount > 50) break;
        }
    }
};

int main() {
    // 初始化特效延迟管理器
    auto delayManager = EffectDelayManager::GetInstance();
    
    // 配置各特效类型的延迟参数
    delayManager->SetEffectDelay(EffectType::PARTICLE, 30, 150, 1.2f);
    delayManager->SetEffectDelay(EffectType::LIGHT, 15, 80, 1.0f);
    delayManager->SetEffectDelay(EffectType::SOUND, 8, 30, 0.8f);
    delayManager->SetEffectDelay(EffectType::UI, 5, 20, 0.5f);
    delayManager->SetEffectDelay(EffectType::PHYSICS, 25, 120, 1.5f);
    
    // 禁用UI特效延迟（示例）
    // delayManager->SetEffectEnabled(EffectType::UI, false);
    
    // 创建游戏特效系统并启动主循环
    GameEffectSystem gameSystem;
    gameSystem.GameMainLoop();
    
    return 0;
}
