import { _decorator, Node, view, UITransform, Size } from 'cc';
import { BaseModal } from 'db://assets/scripts/core/BaseModal';
import { WxSDK } from 'db://assets/scripts/utils/WxSDK';
const { ccclass, property } = _decorator;

/**
 * TimeoutModal 回调接口
 */
export interface TimeoutModalCallbacks {
    /** 点击求助好友回调 */
    onShare?: () => void;
    /** 点击再次挑战回调 */
    onRestart?: () => void;
    /** 点击返回主页 / 关闭按钮回调 */
    onHome?: () => void;
}

interface TimeoutModalParams {
    levelIndex?: number;
}

/**
 * 时间耗尽弹窗组件
 * 继承 BaseModal，显示倒计时结束提示，提供"求助好友"、"再次挑战"和"返回主页"三个按钮
 */
@ccclass('TimeoutModal')
export class TimeoutModal extends BaseModal {
    /** 静态常量：弹窗层级 */
    public static readonly MODAL_Z_INDEX = 999;

    /** 关闭按钮 */
    @property(Node)
    closeBtn: Node | null = null;

    /** 求助好友按钮 */
    @property(Node)
    buttonShare: Node | null = null;

    /** 再次挑战按钮 */
    @property(Node)
    buttonRestart: Node | null = null;

    /** 返回主页按钮 */
    @property(Node)
    buttonHome: Node | null = null;

    /** 回调函数 */
    private _callbacks: TimeoutModalCallbacks = {};

    /** 缓存的屏幕尺寸 */
    private _screenSize: Size | null = null;

    /**
     * 设置回调函数
     */
    setCallbacks(callbacks: TimeoutModalCallbacks): void {
        this._callbacks = callbacks;
    }

    /**
     * 页面首次加载时调用
     */
    onViewLoad(): void {
        console.log('[TimeoutModal] onViewLoad');
        this._bindButtonEvents();
    }

    /**
     * 页面每次显示时调用
     */
    onViewShow(): void {
        super.onViewShow();
        this._updateWidget();
    }

    /**
     * 页面销毁时调用
     */
    onViewDestroy(): void {
        this._unbindButtonEvents();
    }

    /**
     * 设置弹窗尺寸为全屏
     */
    private _updateWidget(): void {
        if (!this._screenSize) {
            this._screenSize = view.getVisibleSize();
        }

        const uiTransform = this.node.getComponent(UITransform);
        if (uiTransform) {
            uiTransform.setContentSize(this._screenSize.width, this._screenSize.height);
        }
    }

    /**
     * 绑定按钮事件
     */
    private _bindButtonEvents(): void {
        if (this.closeBtn) {
            this.closeBtn.on(Node.EventType.TOUCH_END, this._onHomeClick, this);
        }
        if (this.buttonShare) {
            this.buttonShare.on(Node.EventType.TOUCH_END, this._onShareClick, this);
        }
        if (this.buttonRestart) {
            this.buttonRestart.on(Node.EventType.TOUCH_END, this._onRestartClick, this);
        }
        if (this.buttonHome) {
            this.buttonHome.on(Node.EventType.TOUCH_END, this._onHomeClick, this);
        }
    }

    /**
     * 解除按钮事件绑定
     */
    private _unbindButtonEvents(): void {
        if (this.closeBtn && this.closeBtn.isValid) {
            this.closeBtn.off(Node.EventType.TOUCH_END, this._onHomeClick, this);
        }
        if (this.buttonShare && this.buttonShare.isValid) {
            this.buttonShare.off(Node.EventType.TOUCH_END, this._onShareClick, this);
        }
        if (this.buttonRestart && this.buttonRestart.isValid) {
            this.buttonRestart.off(Node.EventType.TOUCH_END, this._onRestartClick, this);
        }
        if (this.buttonHome && this.buttonHome.isValid) {
            this.buttonHome.off(Node.EventType.TOUCH_END, this._onHomeClick, this);
        }
    }

    /**
     * 求助好友按钮点击
     */
    private _onShareClick(): void {
        console.log('[TimeoutModal] 点击求助好友');

        WxSDK.shareAppMessage({
            title: '这道题太难了，快来帮帮我！',
            query: `level=${this._params?.levelIndex ?? 1}`
        });

        this._callbacks.onShare?.();
    }

    /**
     * 再次挑战按钮点击
     */
    private _onRestartClick(): void {
        console.log('[TimeoutModal] 点击再次挑战');
        this._callbacks.onRestart?.();
    }

    /**
     * 返回主页 / 关闭按钮点击
     */
    private _onHomeClick(): void {
        console.log('[TimeoutModal] 点击返回主页');
        this._callbacks.onHome?.();
    }
}
