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

/**
 * PassModal 回调接口
 */
export interface PassModalCallbacks {
    /** 点击下一关回调 */
    onNextLevel?: () => void;
    /** 点击分享回调 */
    onShare?: () => void;
}

/**
 * 通关弹窗组件
 * 继承 BaseView，显示通关成功弹窗，提供"下一关"和"分享给好友"两个按钮
 */
@ccclass('PassModal')
export class PassModal extends BaseView {
    /** 静态常量：弹窗层级 */
    public static readonly MODAL_Z_INDEX = 999;

    /** 下一关按钮 */
    @property(Node)
    nextLevelButton: Node | null = null;

    /** 分享按钮 */
    @property(Node)
    shareButton: Node | null = null;

    /** 提示Label（如 恭喜通关） */
    @property(Label)
    tipLabel: Label | null = null;

    /** 通关音效 */
    @property(AudioClip)
    successAudio: AudioClip | null = null;

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

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

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

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

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

    /**
     * 页面销毁时调用
     */
    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.nextLevelButton) {
            this.nextLevelButton.on(Node.EventType.TOUCH_END, this._onNextLevelClick, this);
        }
        if (this.shareButton) {
            this.shareButton.on(Node.EventType.TOUCH_END, this._onShareClick, this);
        }
    }

    /**
     * 解除按钮事件绑定
     */
    private _unbindButtonEvents(): void {
        // 节点可能在销毁过程中已被置空，需要检查 isValid
        if (this.nextLevelButton && this.nextLevelButton.isValid) {
            this.nextLevelButton.off(Node.EventType.TOUCH_END, this._onNextLevelClick, this);
        }
        if (this.shareButton && this.shareButton.isValid) {
            this.shareButton.off(Node.EventType.TOUCH_END, this._onShareClick, this);
        }
    }

    /**
     * 播放通关音效
     */
    private _playSuccessSound(): void {
        if (!this.successAudio) {
            return;
        }

        const audioSource = this.node.getComponent(AudioSource);
        if (audioSource) {
            audioSource.playOneShot(this.successAudio);
        }
    }

    /**
     * 下一关按钮点击
     */
    private _onNextLevelClick(): void {
        console.log('[PassModal] 点击下一关');
        this._callbacks.onNextLevel?.();
    }

    /**
     * 分享按钮点击
     */
    private _onShareClick(): void {
        console.log('[PassModal] 点击分享');

        // 调用微信分享
        WxSDK.shareAppMessage({
            title: '快来一起玩这款游戏吧',
            query: `level=${this._params?.levelIndex ?? 1}`
        });

        this._callbacks.onShare?.();
    }
}
