import { _decorator, Button, Component, Label, Node, Prefab, sp, Sprite, Tween, tween, UIOpacity, v3, Vec3 } 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;

    freeSpinAddSpine: 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) {
        this.freeSpinAddUI = NodePoolManager.instance.getNodeFromPoolStatic('freeSpinAdd', this.freeSpinAddPre);
        this.freeSpinAddSpine = this.freeSpinAddUI.getChildByName('spine').getComponent(sp.Skeleton);

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

        let titleNode = this.freeSpinAddSpine.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.freeSpinAddSpine.clearTracks();
        this.freeSpinAddSpine.setAnimation(0, 'animation', true);
        this.freeSpinAddSpine.node.setScale(0, 0, 0)
        this.freeSpinAddSpine.node.getComponent(UIOpacity).opacity = 0
        tween(this.freeSpinAddSpine.node)
            .to(0.5, { scale: new Vec3(1, 1, 1) })
            .call(() => {
                this.scheduleOnce(() => {
                    this.onClose();
                }, 5)
            })
            .start()

        tween(this.freeSpinAddSpine.node.getComponent(UIOpacity))
            .to(0.5, { opacity: 255 })
            .start()
    }

    // 加入回调
    onClose() {
        this.unscheduleAllCallbacks();
        tween(this.freeSpinAddSpine.node.getComponent(UIOpacity))
            .to(0.5, { opacity: 0 })
            .call(() => {
                this.freeSpinAddUI.removeFromParent();
                NodePoolManager.instance.putNodeToPool('freeSpinAdd', this.freeSpinAddUI);
                this.closeCallBack && this.closeCallBack();
            })
            .start()
    }
}


