// Ciallo 特效管理器类
class CialloEffectManager {
    constructor() {
        // 兼容两种配置键名
        this.enabled = ConfigManager.getBoolConfig('cialloEffectEnabled', true) || 
                      ConfigManager.getBoolConfig('enableCialloEffect', true); // 默认开启
        
        // 点击特效配置
        this.textVariants = [
            'Ciallo～(∠・ω< )⌒★',
            '0721',
            'Zako'
        ];
        
        // 当前选中的文本特效
        this.selectedText = ConfigManager.getConfig('selectedTextEffect', 'Ciallo～(∠・ω< )⌒★');
        this.colors = [
            '#FF6B6B', // 红色
            '#4ECDC4', // 青色
            '#45B7D1', // 蓝色
            '#96CEB4', // 绿色
            '#FFEAA7', // 黄色
            '#DDA0DD', // 紫色
            '#FFB347', // 橙色
            '#FF69B4', // 粉色
            '#87CEEB', // 天蓝色
            '#98FB98'  // 浅绿色
        ];
        
        // 初始化特效
        this.init();
        console.log('Ciallo 特效管理器初始化完成');
    }

    // 初始化特效
    init() {
        if (!this.enabled) return;

        // 添加Ciallo特效样式
        const style = document.createElement('style');
        style.textContent = `
            .full-ciallo-effect {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                pointer-events: none;
                z-index: 9999;
                opacity: 0;
                transition: opacity 0.15s ease;
                background: radial-gradient(circle, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0) 70%);
                transform: scale(0);
            }

            .full-ciallo-effect.active {
                opacity: 1;
                transform: scale(1);
            }

            .ciallo-text {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                font-size: 5rem;
                font-weight: bold;
                color: #ff00ff;
                text-shadow: 0 0 10px #ff00ff, 0 0 20px #ff00ff, 0 0 30px #ff00ff;
                animation: ciallo-pulse 0.25s infinite alternate;
            }

            @keyframes ciallo-pulse {
                from { transform: translate(-50%, -50%) scale(1); }
                to { transform: translate(-50%, -50%) scale(1.1); }
            }
        `;
        document.head.appendChild(style);

        // 创建Ciallo特效元素
        this.cialloEffect = document.createElement('div');
        this.cialloEffect.className = 'full-ciallo-effect';
        this.cialloEffect.innerHTML = '<div class="ciallo-text">Ciallo～(∠・ω<)⌒☆</div>';
        document.body.appendChild(this.cialloEffect);

        // 监听快捷键
        document.addEventListener('keydown', (e) => {
            // Ctrl+Shift+C 触发Ciallo特效
            if (e.ctrlKey && e.shiftKey && e.key === 'C') {
                this.triggerCialloEffect();
            }
        });
    }

    // 设置是否启用特效
    setEnabled(enabled) {
        this.enabled = enabled;
        // 同时更新两种配置键名
        ConfigManager.setBoolConfig('cialloEffectEnabled', enabled);
        ConfigManager.setBoolConfig('enableCialloEffect', enabled);
        console.log('Ciallo 特效状态已切换为:', enabled ? '开启' : '关闭');
    }

    // 获取是否启用
    isEnabled() {
        return this.enabled;
    }

    // 创建点击特效元素
    createEffect(x, y) {
        if (!this.enabled) return;

        // 使用选中的文本和随机颜色
        const textVariant = this.selectedText;
        const randomColor = this.colors[Math.floor(Math.random() * this.colors.length)];

        // 创建特效元素
        const effect = document.createElement('div');
        effect.className = 'ciallo-effect'; // 使用原来的类名
        effect.textContent = textVariant;
        effect.style.color = randomColor;
        effect.style.position = 'fixed';
        effect.style.left = `${x}px`;
        effect.style.top = `${y}px`;
        effect.style.transform = 'translate(-50%, -50%)'; // 以鼠标位置为中心
        effect.style.pointerEvents = 'none';
        effect.style.zIndex = '9999';
        effect.style.fontSize = '1.5rem'; // 恢复原来的字体大小
        effect.style.fontWeight = 'bold';
        
        // 使用新的动画样式
        const fadeStyle = document.createElement('style');
        fadeStyle.textContent = `
            .ciallo-effect {
                position: fixed;
                pointer-events: none;
                z-index: 9999;
                font-size: 1.5rem;
                font-weight: bold;
                animation: ciallo-fade 0.8s ease-out forwards;
            }
            
            @keyframes ciallo-fade {
                0% { opacity: 1; transform: translate(-50%, -50%) translateY(0) scale(1); }
                30% { opacity: 1; transform: translate(-50%, -50%) translateY(0) scale(1); }
                100% { opacity: 0; transform: translate(-50%, -50%) translateY(-60px) scale(1); }
            }
        `;
        document.head.appendChild(fadeStyle);

        // 添加到页面
        document.body.appendChild(effect);

        // 动画结束后移除元素
        setTimeout(() => {
            if (effect.parentNode) {
                effect.parentNode.removeChild(effect);
            }
            if (fadeStyle.parentNode) {
                fadeStyle.parentNode.removeChild(fadeStyle);
            }
        }, 800);
    }

    // 处理点击事件
    handleClick(event) {
        if (!this.enabled) return;

        // 获取点击位置
        const x = event.clientX;
        const y = event.clientY;

        // 创建特效
        this.createEffect(x, y);
    }
    
    // 触发全屏特效
    triggerCialloEffect() {
        if (!this.enabled) return;

        this.cialloEffect.classList.add('active');

        // 播放音效
        if (window.soundEffectManager) {
            window.soundEffectManager.playSound('ciallo');
        }

        // 0.75秒后隐藏特效
        setTimeout(() => {
            this.cialloEffect.classList.remove('active');
        }, 750);
    }

    // 切换启用状态
    toggleEnabled() {
        this.enabled = !this.enabled;
        // 同时更新两种配置键名
        ConfigManager.setBoolConfig('cialloEffectEnabled', this.enabled);
        ConfigManager.setBoolConfig('enableCialloEffect', this.enabled);

        if (!this.enabled && this.cialloEffect) {
            this.cialloEffect.classList.remove('active');
        }
    }
    
    // 设置选中的文本特效
    setSelectedText(text) {
        this.selectedText = text;
        ConfigManager.setConfig('selectedTextEffect', text);
        console.log(`选中文本特效已更改为: ${text}`);
    }
    
    // 获取选中的文本特效
    getSelectedText() {
        return this.selectedText;
    }
    
    // 获取所有可用的文本特效
    getTextVariants() {
        return this.textVariants;
    }
}
