// 日志记录系统
class AccessLogger {
    constructor() {
        this.userIP = 'Unknown';
        this.userAgent = navigator.userAgent;
        this.initLogger();
    }

    async initLogger() {
        await this.getUserIP();
        this.logAccess();
    }

    async getUserIP() {
        try {
            // 只使用ipinfo.io获取IP和地理位置信息
            try {
                const response = await fetch('https://ipinfo.io/json');
                const data = await response.json();
                this.userIP = data.ip || 'Unknown';

                // 保存完整的ipinfo.io数据
                this.ipInfo = data;

                if (this.userIP !== 'Unknown') {
                    console.log('成功从ipinfo.io获取IP和地理位置信息');
                } else {
                    console.warn('ipinfo.io返回的数据中没有IP字段');
                }
            } catch (e) {
                console.warn('ipinfo.io获取IP失败:', e);
                this.userIP = 'Unknown';
                this.ipInfo = null;
            }
        } catch (error) {
            console.warn('无法获取IP地址:', error);
            this.userIP = 'Unknown';
            this.ipInfo = null;
        }
    }

    // WebRTC方法已移除，现在只使用ipinfo.io获取IP地址

    getBrowserInfo() {
        const ua = navigator.userAgent;
        let browser = 'Unknown Browser';

        if (ua.includes('Chrome') && !ua.includes('Edg')) browser = 'Chrome';
        else if (ua.includes('Firefox')) browser = 'Firefox';
        else if (ua.includes('Safari') && !ua.includes('Chrome')) browser = 'Safari';
        else if (ua.includes('Edg')) browser = 'Edge';
        else if (ua.includes('Opera')) browser = 'Opera';

        return browser;
    }

    logAccess() {
        const timestamp = new Date().toLocaleString('zh-CN');
        const browser = this.getBrowserInfo();

        // 构建地理位置信息部分
        let geoInfo = '';
        if (this.ipInfo) {
            const { city, region, country, postal, timezone, org } = this.ipInfo;
            geoInfo = `
🏙️ 城市: ${city || '未知'}
🌍 地区: ${region || '未知'}
🌐 国家: ${country || '未知'}
📮 邮编: ${postal || '未知'}
🕐 时区: ${timezone || '未知'}
🏢 ISP/组织: ${org || '未知'}
            `;
        }

        console.log(`
🌟====================================🌟
📅 访问时间: ${timestamp}
🔍 IP地址: 【🎯 ${this.userIP} 🎯】
🌐 浏览器: ${browser}
📱 设备信息: ${navigator.platform}
🖥️ 屏幕分辨率: ${screen.width}x${screen.height}
${geoInfo}
🌟=============================
        `);
    }

    logVideoPlay(videoTitle) {
        const timestamp = new Date().toLocaleString('zh-CN');

        // 构建地理位置信息部分
        let geoInfo = '';
        if (this.ipInfo) {
            const { city, region, country } = this.ipInfo;
            geoInfo = `📍 位置: ${city || '未知'}, ${region || '未知'}, ${country || '未知'}`;
        }

        console.log(`
🎬 视频播放日志 🎬
📅 时间: ${timestamp}
🔍 IP: 【🎯 ${this.userIP} 🎯】
🎥 视频: ${videoTitle}
🌐 浏览器: ${this.getBrowserInfo()}
${geoInfo}
        `);
    }

    logModeSwitch(mode) {
        const timestamp = new Date().toLocaleString('zh-CN');

        // 构建地理位置信息部分
        let geoInfo = '';
        if (this.ipInfo) {
            const { city, region, country } = this.ipInfo;
            geoInfo = `📍 位置: ${city || '未知'}, ${region || '未知'}, ${country || '未知'}`;
        }

        console.log(`
🔄 模式切换日志 🔄
📅 时间: ${timestamp}
🔍 IP: 【🎯 ${this.userIP} 🎯】
📂 切换至: ${mode === 'tag' ? '分类浏览' : '全部视频'}
🌐 浏览器: ${this.getBrowserInfo()}
${geoInfo}
        `);
    }
}
