import { _decorator, Button, Component, Label, Node, Prefab, sp, Sprite, Tween, tween, UIOpacity, v3 } from 'cc';
import { NodePoolManager } from '../../../Loading/scripts/manager/NodePoolManager';
import { gold2cash } from '../../../Loading/scripts/comm';
import { AudioManager } from '../../../Loading/scripts/manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('TotalWin')
export class TotalWin extends Component {
    @property(Prefab)
    totalWinUiPre: Prefab = null;

    totalWinUINode: Node = null;
    totalWinNodeOpacity: UIOpacity | null = null;
    totalWinSpine: sp.Skeleton | null = null;
    totalWinSprite: Sprite | null = null;
    NumNodeOpacityCom: UIOpacity | null = null;
    scoreLabel: Label | null = null;        // 分数节点
    winScore: number = 0;                   // 最终赢分
    btn: Button | null = null;

    closeCallBack: (() => void) | null = null;


    show(winScore: number, closeCallBack: (() => void) | null = null) {
        this.totalWinUINode = NodePoolManager.instance.getNodeFromPoolStatic('totalWinUI', this.totalWinUiPre);
        this.totalWinSpine = this.totalWinUINode.getChildByName('spine').getComponent(sp.Skeleton);

        let titleNode = this.totalWinSpine.node.getChildByName('TitleNode');
        this.totalWinNodeOpacity = titleNode.getComponent(UIOpacity);
        this.totalWinNodeOpacity.opacity = 255;
        this.totalWinSprite = titleNode.getChildByName('TOTALWIN').getComponent(Sprite);

        let numNode = this.totalWinSpine.node.getChildByName('NumNode');
        this.NumNodeOpacityCom = numNode.getComponent(UIOpacity);
        this.NumNodeOpacityCom.opacity = 255;
        this.scoreLabel = numNode.getChildByName('Label').getComponent(Label);
        Tween.stopAllByTarget(this.NumNodeOpacityCom.node);

        let btnNode = this.totalWinSpine.node.getChildByName('BtnNode');
        this.btn = btnNode.getChildByName('btnCollect').getComponent(Button);

        this.btn.node.on(Button.EventType.CLICK, this.onClose, this);

        this.winScore = winScore;
        this.scoreLabel.string = gold2cash(winScore);
        this.btn.node.active = false;
        this.node.addChild(this.totalWinUINode);
        this.playAnimation();
        AudioManager.instance.playSFX('Total_Settle_Sound');

        this.closeCallBack = closeCallBack;
    }

    playAnimation() {
        this.totalWinSpine.clearTracks();
        this.totalWinSpine.setAnimation(0, 'in', false);
        this.totalWinSpine.setCompleteListener(() => {
            this.totalWinSpine.setAnimation(0, 'loop', true);
            this.btn.node.active = true;
            this.scheduleOnce(() => {
                this.onClose();
            }, 5)
        })
    }

    // 加入回调
    onClose() {
        this.btn.node.off(Button.EventType.CLICK, this.onClose, this);
        AudioManager.instance.playSFX('Total_Settle_Collect_Click');
        this.scheduleOnce(() => {
            AudioManager.instance.playSFX('Total_Settle_Sound_End');
        }, 0.5)

        this.fadeOutScoreLabel(1.7, 0.6);
        this.btn.node.active = false;
        this.unscheduleAllCallbacks();
        this.totalWinSpine.clearTracks();
        this.totalWinSpine.setAnimation(0, 'out', true);
        this.totalWinSpine.setCompleteListener(() => {
            this.totalWinUINode.removeFromParent();
            NodePoolManager.instance.putNodeToPool('totalWinUI', this.totalWinUINode);
            this.closeCallBack && this.closeCallBack();
        })
    }



    fadeOutScoreLabel(delay: number, duration: number) {
        // 先停止可能正在进行的动画
        Tween.stopAllByTarget(this.NumNodeOpacityCom.node);

        // 确保标签是可见的
        this.NumNodeOpacityCom.opacity = 255;

        // 创建并执行渐隐动画
        tween(this.NumNodeOpacityCom)
            .delay(delay)
            .to(duration, { opacity: 0 })
            .start();


        // 先停止可能正在进行的动画
        Tween.stopAllByTarget(this.totalWinNodeOpacity.node);

        // 确保标签是可见的
        this.totalWinNodeOpacity.opacity = 255;

        // 创建并执行渐隐动画
        tween(this.totalWinNodeOpacity)
            .delay(delay)
            .to(duration, { opacity: 0 })
            .start();


    }
}


