/**
 * 分享引导工具
 * 用于控制每日免费下载次数和分享引导
 */

const STORAGE_KEY_DOWNLOAD_DATE = 'downloadDate';
const STORAGE_KEY_DOWNLOAD_COUNT = 'downloadCount';
const STORAGE_KEY_SHARED_DATE = 'sharedDate';

/**
 * 获取今天的日期字符串（YYYY-MM-DD格式）
 */
function getTodayDateString(): string {
    const today = new Date();
    const year = today.getFullYear();
    const month = String(today.getMonth() + 1).padStart(2, '0');
    const day = String(today.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}

/**
 * 检查今天是否已经下载过
 * @returns true表示今天已下载过，false表示今天未下载
 */
export function hasDownloadedToday(): boolean {
    const today = getTodayDateString();
    const downloadDate = wx.getStorageSync(STORAGE_KEY_DOWNLOAD_DATE);
    return downloadDate === today;
}

/**
 * 记录今天的下载成功
 */
export function recordDownloadSuccess(): void {
    const today = getTodayDateString();
    wx.setStorageSync(STORAGE_KEY_DOWNLOAD_DATE, today);
    const count = wx.getStorageSync(STORAGE_KEY_DOWNLOAD_COUNT) || 0;
    wx.setStorageSync(STORAGE_KEY_DOWNLOAD_COUNT, count + 1);
}

/**
 * 检查今天是否已经分享过
 * @returns true表示今天已分享过，false表示今天未分享
 */
export function hasSharedToday(): boolean {
    const today = getTodayDateString();
    const sharedDate = wx.getStorageSync(STORAGE_KEY_SHARED_DATE);
    return sharedDate === today;
}

/**
 * 记录今天的分享成功
 */
export function recordShareSuccess(): void {
    const today = getTodayDateString();
    wx.setStorageSync(STORAGE_KEY_SHARED_DATE, today);
}

/**
 * 检查是否需要显示分享引导
 * @returns true表示需要显示分享引导，false表示不需要
 */
export function shouldShowShareGuide(): boolean {
    // 如果今天已经分享过，不需要显示引导
    const env = getApp().globalData.env;
    if (env === 'develop' || hasSharedToday()) {
        return false;
    }
    // 如果今天已经下载过，需要显示引导
    return hasDownloadedToday();
}
