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

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

    freeSpinAddUI: Node = null;

    freeSpinEnterSpine: sp.Skeleton | null = null;
    freeSpinEnterTitleSp: Sprite | null = null;
    freeCountLabel: Label | null = null;
    freeCount: number = 0;

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

    show(freeCount: number, closeCallBack: (() => void) | null = null) {
        AudioManager.instance.playSFX('Free_Trigger_Free_Sound');
        this.freeSpinAddUI = NodePoolManager.instance.getNodeFromPoolStatic('freeSpinAdd', this.freeSpinAddPre);
        this.freeSpinEnterSpine = this.freeSpinAddUI.getChildByName('spine').getComponent(sp.Skeleton);

        let numNode = this.freeSpinEnterSpine.node.getChildByName('NumNode');
        this.freeCountLabel = numNode.getChildByName('Label').getComponent(Label);

        let titleNode = this.freeSpinEnterSpine.node.getChildByName('TitleNode');
        this.freeSpinEnterTitleSp = titleNode.getChildByName('SpriteFREESPINWON').getComponent(Sprite);

        this.freeCount = freeCount;
        this.freeCountLabel.string = `${freeCount}`;
        this.node.addChild(this.freeSpinAddUI);
        this.playAnimation();
        this.closeCallBack = closeCallBack;
    }

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

    // 加入回调
    onClose() {
        this.unscheduleAllCallbacks();
        this.freeSpinEnterSpine.clearTracks();
        this.freeSpinEnterSpine.setAnimation(0, 'out', true);
        this.freeSpinEnterSpine.setCompleteListener(() => {
            this.freeSpinAddUI.removeFromParent();
            NodePoolManager.instance.putNodeToPool('freeSpinAdd', this.freeSpinAddUI);
            this.closeCallBack && this.closeCallBack();
        })
    }
}


