import { getCachedMembershipStatus, MEMBERSHIP_STORAGE_KEY } from './membership.js';

// 集中管理欢迎消息的颜色逻辑
const WelcomeManager = {
    colorCache: {
        lastBackground: null,
        lastTextColor: null
    },

    lastUserName: null,

    async getLoggedInUserName() {
        try {
            const status = await getCachedMembershipStatus();
            if (!status?.signedIn) return null;
            const name = status.user?.name || status.user?.email;
            return typeof name === 'string' && name.trim() ? name.trim() : null;
        } catch (_) {
            return null;
        }
    },

    // 初始化方法
    initialize() {
        // 清理旧的自定义欢迎语名称
        try {
            localStorage.removeItem('userName');
            chrome.storage.sync.remove(['userName']);
        } catch (_) {
            // ignore
        }

        // 先检查欢迎语是否应该显示，再更新内容
        this.updateWelcomeMessage(true);

        this.initializeColorCache();
        this.setupEventListeners();
        this.setupThemeChangeListener();
    },

    // 更新欢迎消息（需登录，使用登录用户名）
    async updateWelcomeMessage(checkVisibility = true) {
        const welcomeElement = document.getElementById('welcome-message');
        if (!welcomeElement) return;

        const userName = await this.getLoggedInUserName();
        this.lastUserName = userName;

        const apply = (showSetting) => {
            const shouldShow = showSetting !== false && Boolean(userName);
            welcomeElement.style.visibility = 'visible';
            welcomeElement.style.display = shouldShow ? '' : 'none';

            if (!shouldShow) return;

            const now = new Date();
            const hours = now.getHours();
            const getMessage = window.getLocalizedMessage || chrome.i18n.getMessage.bind(chrome.i18n);
            let greeting;

            if (hours < 12) {
                greeting = getMessage('morningGreeting') || 'Good morning';
            } else if (hours < 18) {
                greeting = getMessage('afternoonGreeting') || 'Good afternoon';
            } else {
                greeting = getMessage('eveningGreeting') || 'Good evening';
            }

            welcomeElement.textContent = `${greeting}, ${userName}`;
            this.adjustTextColor(welcomeElement);
        };

        if (checkVisibility) {
            chrome.storage.sync.get(['showWelcomeMessage'], (result) => {
                apply(result.showWelcomeMessage);
            });
        } else {
            apply(true);
        }
    },

    // 初始化颜色缓存
    initializeColorCache() {
        const computedStyle = window.getComputedStyle(document.documentElement);
        const backgroundColor = computedStyle.backgroundColor;
        const backgroundImage = document.body.style.backgroundImage;
        
        // 计算初始文字颜色
        if (backgroundColor && backgroundColor !== 'rgba(0, 0, 0, 0)' && backgroundColor !== 'transparent') {
            const rgb = backgroundColor.match(/\d+/g);
            if (rgb && rgb.length >= 3) {
                const brightness = (parseInt(rgb[0]) * 0.299 + parseInt(rgb[1]) * 0.587 + parseInt(rgb[2]) * 0.114);
                this.colorCache.lastTextColor = brightness > 128 ? 'rgba(51, 51, 51, 0.9)' : 'rgba(255, 255, 255, 0.9)';
            }
        }
        
        this.colorCache.lastBackground = backgroundImage !== 'none' ? backgroundImage : backgroundColor;
        
        // 应用初始颜色
        const welcomeElement = document.getElementById('welcome-message');
        if (welcomeElement) {
            welcomeElement.style.color = this.colorCache.lastTextColor || 'rgba(51, 51, 51, 0.9)';
        }
    },

    // 调整文字颜色
    adjustTextColor(element) {
        const computedStyle = window.getComputedStyle(document.documentElement);
        const backgroundColor = computedStyle.backgroundColor;
        const backgroundImage = document.body.style.backgroundImage;
        const isDarkMode = document.documentElement.getAttribute('data-theme') === 'dark';
        
        // 处理纯色背景的情况
        if (!backgroundImage || backgroundImage === 'none') {
            // 如果是暗色模式，直接使用亮色文本
            if (isDarkMode) {
                element.style.color = 'rgba(255, 255, 255, 0.9)';
                this.colorCache.lastTextColor = 'rgba(255, 255, 255, 0.9)';
                return;
            }
            
            // 亮色模式下，根据背景色计算文字颜色
            let textColor = 'rgba(51, 51, 51, 0.9)'; // 默认深色文本
            
            if (backgroundColor && backgroundColor !== 'rgba(0, 0, 0, 0)' && backgroundColor !== 'transparent') {
                const rgb = backgroundColor.match(/\d+/g);
                if (rgb && rgb.length >= 3) {
                    const brightness = (parseInt(rgb[0]) * 0.299 + parseInt(rgb[1]) * 0.587 + parseInt(rgb[2]) * 0.114);
                    textColor = brightness > 128 ? 'rgba(51, 51, 51, 0.9)' : 'rgba(255, 255, 255, 0.9)';
                }
            }
            
            this.colorCache.lastTextColor = textColor;
            element.style.color = textColor;
            return;
        }

        // 处理壁纸背景的情况
        if (backgroundImage && backgroundImage !== 'none') {
            // 先检查缓存
            if (this.colorCache.lastBackground === backgroundImage && this.colorCache.lastTextColor) {
                element.style.color = this.colorCache.lastTextColor;
                // 如果有缓存，仍然进行新的计算，但不设置临时的白色文本
            } else {
                // 只有在没有缓存时才设置临时的白色文本
                element.style.color = 'rgba(255, 255, 255, 0.9)';
            }
            
            // 进行新的计算...
            const img = new Image();
            img.crossOrigin = "Anonymous";
            img.src = backgroundImage.slice(5, -2);
            
            img.onload = () => {
                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');
                const sampleSize = 50;

                // 获取欢迎文字元素的位置和尺寸
                const elementRect = element.getBoundingClientRect();
                
                // 计算采样区域
                const sampleArea = {
                    x: Math.max(0, elementRect.x),
                    y: Math.max(0, elementRect.y),
                    width: Math.min(elementRect.width, window.innerWidth),
                    height: Math.min(elementRect.height, window.innerHeight)
                };

                // 设置画布尺寸
                canvas.width = sampleSize;
                canvas.height = sampleSize;

                // 计算图片在背景中的实际尺寸和位置
                const backgroundSize = getComputedStyle(document.body).backgroundSize;
                const backgroundPosition = getComputedStyle(document.body).backgroundPosition;
                
                // 计算图片的缩放比例
                const scale = {
                    x: img.width / window.innerWidth,
                    y: img.height / window.innerHeight
                };

                // 根据背景属性计算实际的采样区域
                const sourceArea = {
                    x: (sampleArea.x * scale.x),
                    y: (sampleArea.y * scale.y),
                    width: (sampleArea.width * scale.x),
                    height: (sampleArea.height * scale.y)
                };

                // 绘制采样区域到画布
                ctx.drawImage(
                    img,
                    sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height,  // 源图像区域
                    0, 0, sampleSize, sampleSize  // 目标画布区域
                );

                try {
                    const imageData = ctx.getImageData(0, 0, sampleSize, sampleSize);
                    const data = imageData.data;
                    let r = 0, g = 0, b = 0;
                    let count = 0;

                    // 计算采样区域的平均颜色
                    for (let x = 0, len = data.length; x < len; x += 4) {
                        r += data[x];
                        g += data[x + 1];
                        b += data[x + 2];
                        count++;
                    }

                    r = Math.floor(r / count);
                    g = Math.floor(g / count);
                    b = Math.floor(b / count);

                    const brightness = (r * 0.299 + g * 0.587 + b * 0.114);
                    console.log('[WelcomeManager] Sampled area color:', {
                        area: sampleArea,
                        color: {r, g, b},
                        brightness
                    });

                    const textColor = brightness > 128 ? 
                        'rgba(51, 51, 51, 0.9)' : 
                        'rgba(255, 255, 255, 0.9)';
                    
                    // 更新缓存和应用颜色
                    this.colorCache.lastBackground = backgroundImage;
                    this.colorCache.lastTextColor = textColor;
                    element.style.color = textColor;
                    element.style.transition = 'color 0.3s ease';
                } catch (error) {
                    console.error('分析背景颜色失败:', error, {
                        sampleArea,
                        sourceArea
                    });
                    element.style.color = 'rgba(255, 255, 255, 0.9)';
                }
            };

            img.onerror = () => {
                console.error('背景图片加载失败');
                if (!this.colorCache.lastTextColor) {
                    // 只有在没有缓存颜色时才设置默认颜色
                    element.style.color = 'rgba(255, 255, 255, 0.9)';
                }
            };

            // 在图片加载过程中先使用白色文本
            element.style.color = 'rgba(255, 255, 255, 0.9)';
            return;
        }
    },

    // 设置事件监听器
    setupEventListeners() {
        // 登录状态变化时刷新欢迎语
        chrome.storage.onChanged.addListener((changes, area) => {
            if (area === 'local' && changes[MEMBERSHIP_STORAGE_KEY]) {
                this.updateWelcomeMessage();
            }
            if (area === 'sync' && changes.showWelcomeMessage) {
                this.updateWelcomeMessage();
            }
        });

        // 添加对欢迎消息内容变化的监听
        const welcomeElement = document.getElementById('welcome-message');
        if (welcomeElement) {
            let restoring = false;
            const observer = new MutationObserver((mutations) => {
                if (restoring) return;
                mutations.forEach((mutation) => {
                    if (mutation.type === 'childList' || mutation.type === 'characterData') {
                        const currentText = (welcomeElement.textContent || '').trim();
                        const userName = this.lastUserName;
                        // 任意语言下，若欢迎语被覆盖且缺少用户名，则重新组装
                        if (currentText && userName && !currentText.includes(userName)) {
                            restoring = true;
                            this.updateWelcomeMessage();
                            restoring = false;
                        }
                    }
                });
            });
            
            observer.observe(welcomeElement, {
                childList: true,
                characterData: true,
                subtree: true
            });
        }
    },

    // 添加主题变化监听方法
    setupThemeChangeListener() {
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if (mutation.attributeName === 'data-theme') {
                    const welcomeElement = document.getElementById('welcome-message');
                    if (welcomeElement) {
                        this.adjustTextColor(welcomeElement);
                    }
                }
            });
        });

        observer.observe(document.documentElement, {
            attributes: true,
            attributeFilter: ['data-theme']
        });
    }
};

// 导出给其他模块使用的方法
window.WelcomeManager = WelcomeManager;

// DOM加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
    WelcomeManager.initialize();
    // 每分钟更新一次欢迎消息（问候语随时间变化）
    setInterval(() => WelcomeManager.updateWelcomeMessage(), 60000);
});
