import { FOCUS_FUNCTION_TYPES } from '../../constants/focusFunctions';
import tracker from '../../utils/tracker';
import { defaultShareConfig } from '../../config/config';

Page({
    data: {
        functionList: FOCUS_FUNCTION_TYPES,
    },

    onLoad() {
        // 页面加载
    },

    /**
     * 打开专注力功能页面
     */
    openFocusFunction(e: WechatMiniprogram.TouchEvent) {
        const functionId = e.currentTarget.dataset.function;
        const functionItem = this.data.functionList.find(
            (item) => item.id === functionId,
        );

        if (!functionItem) {
            return;
        }

        // 如果有page字段，跳转到对应页面
        if (functionItem.page) {
            let url = `/focusPages/${functionItem.page}/${functionItem.page}?id=${functionId}`;
            // 如果有 mode 参数，添加到 URL 中
            if (functionItem.mode) {
                url += `&mode=${functionItem.mode}`;
            }
            wx.navigateTo({
                url,
            });
        } else {
            wx.showToast({
                title: `功能开发中，敬请期待`,
                icon: 'none',
                duration: 2500,
            });
        }
    },

    onShareAppMessage() {
        // 上报分享埋点
        tracker.reportShare('专注力游戏');

        return {
            ...defaultShareConfig,
            path: `/pages/focusIndex/focusIndex`,
        };
    },
    onShareTimeline() {
        // 上报分享埋点
        tracker.reportShare('专注力游戏');

        return {
            ...defaultShareConfig,
            query: `/pages/focusIndex/focusIndex`,
        };
    },
});
