/**
 * 弥夏引导页 2026 核心逻辑
 * 功能：主题切换、音乐控制、时钟、倒计时、随机祝福语、福字特效、访客模拟
 */

// 1. 随机祝福语库
const IDIOMS = [
    "福星高照 · 喜气盈门",
    "万事如意 · 岁岁平安",
    "心想事成 · 前程似锦",
    "龙腾虎跃 · 气象万千",
    "财源广进 · 步步高升",
    "吉星高照 · 五福临门",
    "春和景明 · 阖家欢乐",
    "祥瑞云集 · 诸事大吉",
    "一帆风顺 · 百福具臻",
    "金玉满堂 · 喜上眉梢",
    "年年有余 · 岁岁今朝",
    "瑞雪丰年 · 锦绣前程",
    "安康大吉 · 万象更新",
    "花开富贵 · 竹报平安",
    "大吉大利 · 福寿双全"
];

function toggleTheme() {
    const root = document.documentElement;
    const current = root.getAttribute('theme') || 'light';
    const next = current === 'dark' ? 'light' : 'dark';
    root.setAttribute('theme', next);
    document.getElementById('switchBall').innerText = next === 'dark' ? '🌙' : '☀️';
    localStorage.setItem('misha_theme', next);
}

function toggleMusic() {
    const audio = document.getElementById('bgMusic');
    const icon = document.getElementById('musicIcon');
    if (audio.paused) { 
        audio.play().catch(()=>{}); 
        icon.innerText = '🔊'; 
    } else { 
        audio.pause(); 
        icon.innerText = '🎵'; 
    }
}

function updateClock() {
    const now = new Date();
    document.getElementById('date-str').innerText = now.toLocaleDateString('zh-CN', {month:'long', day:'numeric', weekday:'long'});
    document.getElementById('big-clock').innerText = now.toTimeString().split(' ')[0];
    
    const target = new Date('2027/02/16 00:00:00');
    const diff = target - now;
    if (diff > 0) {
        const d = Math.floor(diff/86400000);
        const h = Math.floor(diff/3600000%24);
        const m = Math.floor(diff/60000%60);
        document.getElementById('countdown').innerText = `${d}天${h}时${m}分`;
    }
}

function createFu() {
    const bg = document.getElementById('fu-bg');
    if (document.querySelectorAll('.floating-fu').length >= 35) return;
    
    const fu = document.createElement('div');
    fu.className = 'floating-fu';
    fu.innerText = '福';
    // 随机大尺寸：30px - 55px
    fu.style.fontSize = (Math.random() * 25 + 30) + 'px';
    fu.style.left = Math.random() * 95 + '%';
    fu.style.top = Math.random() * 90 + '%';
    
    bg.appendChild(fu);
    setTimeout(() => fu.remove(), 8000);
}

// 页面加载初始化
document.addEventListener('DOMContentLoaded', () => {
    // A. 随机切换成语
    const randomIdiom = IDIOMS[Math.floor(Math.random() * IDIOMS.length)];
    document.getElementById('idiom-show').innerText = randomIdiom;

    // B. 初始化主题
    const saved = localStorage.getItem('misha_theme') || 'light';
    document.documentElement.setAttribute('theme', saved);
    document.getElementById('switchBall').innerText = saved === 'dark' ? '🌙' : '☀️';
    
    // C. 访客统计
    let v = parseInt(localStorage.getItem('misha_v') || 43800) + 1;
    localStorage.setItem('misha_v', v);
    document.getElementById('local_v').innerText = v.toLocaleString();

    // D. 开启定时任务
    setInterval(updateClock, 1000);
    setInterval(createFu, 600); // 0.6秒生成一个大福字
    
    updateClock();
});