// 主要功能脚本 - 轻量级版本
class LightweightTheme {
constructor() {
this.init();
}

init() {
this.setupEventListeners();
this.setupScrollToTop();
this.setupLazyLoading();
this.setupMobileMenu();
}

setupEventListeners() {
// 搜索表单增强
this.enhanceSearchForms();

// 文章卡片悬停效果
this.setupPostCards();

// 平滑滚动
this.setupSmoothScroll();
}

enhanceSearchForms() {
const searchForms = document.querySelectorAll('.search-form');
searchForms.forEach(form => {
form.addEventListener('submit', (e) => {
const input = form.querySelector('.search-input');
if (!input.value.trim()) {
e.preventDefault();
input.focus();
this.showMessage('请输入搜索关键词', 'warning');
}
});
});
}

setupPostCards() {
const postCards = document.querySelectorAll('.post-card');
postCards.forEach(card => {
card.addEventListener('mouseenter', () => {
card.style.transform = 'translateY(-5px)';
});

card.addEventListener('mouseleave', () => {
card.style.transform = 'translateY(0)';
});
});
}

setupSmoothScroll() {
const links = document.querySelectorAll('a[href^="#"]');
links.forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
if (href !== '#') {
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
});
});
}

setupScrollToTop() {
const scrollButton = document.createElement('button');
scrollButton.innerHTML = '↑';
scrollButton.className = 'scroll-to-top';
scrollButton.setAttribute('aria-label', '回到顶部');
scrollButton.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
background: var(--primary-color);
color: white;
border: none;
border-radius: 50%;
width: 50px;
height: 50px;
font-size: 1.2rem;
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
z-index: 1000;
box-shadow: var(--shadow-hover);
`;

document.body.appendChild(scrollButton);

window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
scrollButton.style.opacity = '1';
scrollButton.style.visibility = 'visible';
scrollButton.style.transform = 'scale(1)';
} else {
scrollButton.style.opacity = '0';
scrollButton.style.visibility = 'hidden';
scrollButton.style.transform = 'scale(0.8)';
}
});

scrollButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}

setupLazyLoading() {
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
if (img.dataset.src) {
img.src = img.dataset.src;
}
if (img.dataset.srcset) {
img.srcset = img.dataset.srcset;
}
img.classList.remove('lazy');
imageObserver.unobserve(img);
}
});
});

document.querySelectorAll('img[data-src]').forEach(img => {
imageObserver.observe(img);
});
}
}

setupMobileMenu() {
// 在移动设备上添加菜单切换按钮
if (window.innerWidth <= 768) {
const nav = document.querySelector('.nav-menu');
if (nav) {
const toggleButton = document.createElement('button');
toggleButton.innerHTML = '☰';
toggleButton.className = 'mobile-menu-toggle';
toggleButton.style.cssText = `
display: none;
background: var(--primary-color);
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: var(--border-radius);
cursor: pointer;
margin: 0.5rem 0;
font-size: 1.2rem;
`;

nav.parentNode.insertBefore(toggleButton, nav);

toggleButton.addEventListener('click', () => {
const isHidden = nav.style.display === 'none';
nav.style.display = isHidden ? 'flex' : 'none';
toggleButton.innerHTML = isHidden ? '✕' : '☰';
});

// 默认在移动端隐藏菜单
nav.style.display = 'none';
toggleButton.style.display = 'block';
}
}
}

showMessage(message, type = 'info') {
// 简单的消息提示功能
const messageEl = document.createElement('div');
messageEl.textContent = message;
messageEl.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: ${type === 'warning' ? '#ff9800' : '#4caf50'};
color: white;
padding: 1rem 1.5rem;
border-radius: var(--border-radius);
box-shadow: var(--shadow-hover);
z-index: 1001;
transform: translateX(100%);
transition: transform 0.3s ease;
`;

document.body.appendChild(messageEl);

// 显示动画
setTimeout(() => {
messageEl.style.transform = 'translateX(0)';
}, 100);

// 自动隐藏
setTimeout(() => {
messageEl.style.transform = 'translateX(100%)';
setTimeout(() => {
if (messageEl.parentNode) {
messageEl.parentNode.removeChild(messageEl);
}
}, 300);
}, 3000);
}
}

// 工具函数
const ThemeUtils = {
// 防抖函数
debounce: (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
},

// 格式化日期
formatDate: (timestamp) => {
return new Date(timestamp * 1000).toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
},

// 截断文本
truncateText: (text, length) => {
if (text.length <= length) return text;
return text.substr(0, length) + '...';
},

// 检测移动设备
isMobile: () => {
return window.innerWidth <= 768;
}
};

// 初始化主题
document.addEventListener('DOMContentLoaded', () => {
new LightweightTheme();

// 添加加载类用于动画
document.body.classList.add('loaded');

// 性能监控
if (typeof performance !== 'undefined') {
const loadTime = performance.timing.domContentLoadedEventEnd - performance.timing.navigationStart;
console.log(`页面加载时间: ${loadTime}ms`);
}
});

// 错误处理
window.addEventListener('error', (e) => {
console.error('JavaScript错误:', e.error);
});

// 离线支持
window.addEventListener('online', () => {
console.log('网络连接已恢复');
});

window.addEventListener('offline', () => {
console.log('网络连接已断开');
});