import { _decorator, Button, Component, Label, Node, Prefab, sp, Sprite, Tween, tween, UIOpacity, v3, Vec3 } 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('FreeSpinEnter')
export class FreeSpinEnter extends Component {
    @property(Prefab)
    freeSpinEnterPre: Prefab = null;

    freeSpinEnterUI: Node = null;

    freeSpinEnterSpine: sp.Skeleton | null = null;
    Node_1: Node | null = null;

    freeSpinEnterTitleSp: Sprite | null = null;
    freeSpinEnterContentSp: Sprite | null = null;
    NumNodeOpacityCom: UIOpacity | null = null;
    freeCountLabel: Label | null = null;
    freeCount: number = 0;
    btn: Button | null = null;

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

    show(freeCount: number, closeCallBack: (() => void) | null = null) {
        AudioManager.instance.playBGM("Free_Mode_BGM");
        this.freeSpinEnterUI = NodePoolManager.instance.getNodeFromPoolStatic('freeSpinEnter', this.freeSpinEnterPre);
        this.freeSpinEnterSpine = this.freeSpinEnterUI.getChildByName('spine').getComponent(sp.Skeleton);
        this.Node_1 = this.freeSpinEnterSpine.node.getChildByName('Node');
        let numNode = this.Node_1.getChildByName('NumNode');
        this.NumNodeOpacityCom = numNode.getComponent(UIOpacity);
        this.NumNodeOpacityCom.opacity = 255;
        Tween.stopAllByTarget(this.NumNodeOpacityCom.node);
        this.freeCountLabel = numNode.getChildByName('Label').getComponent(Label);

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

        let contentNode = this.Node_1.getChildByName('ContentNode');
        this.freeSpinEnterContentSp = contentNode.getChildByName('SpriteCONTENT').getComponent(Sprite);

        let btnNode = this.Node_1.getChildByName('BtnNode');
        this.btn = btnNode.getChildByName('BtnStart').getComponent(Button);

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

        this.freeCount = freeCount;
        this.freeCountLabel.string = `${freeCount}`;
        this.btn.node.active = false;
        this.node.addChild(this.freeSpinEnterUI);
        this.playAnimation();
        this.closeCallBack = closeCallBack;
    }

    playAnimation() {
        this.freeSpinEnterSpine.clearTracks();
        this.freeSpinEnterSpine.setAnimation(0, 'animation', true);
        this.freeSpinEnterSpine.node.setScale(0, 0, 0)
        this.freeSpinEnterSpine.node.getComponent(UIOpacity).opacity = 0
        tween(this.freeSpinEnterSpine.node)
            .to(0.5, { scale: new Vec3(1, 1, 1) })
            .call(() => {
                this.btn.node.active = true
                this.scheduleOnce(() => {
                    this.onClose();
                }, 5)
            })
            .start()

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

    // 加入回调
    onClose(isClick: boolean = true) {
        this.btn.node.off(Button.EventType.CLICK, this.onClose, this);
        this.btn.node.active = false;
        this.unscheduleAllCallbacks();
        // this.fadeOutScoreLabel(0.5, 0.5);
        tween(this.freeSpinEnterSpine.node.getComponent(UIOpacity))
            .to(0.5, { opacity: 0 })
            .call(() => {
                this.freeSpinEnterUI.removeFromParent();
                NodePoolManager.instance.putNodeToPool('freeSpinEnter', this.freeSpinEnterUI);
                this.closeCallBack && this.closeCallBack();
            })
            .start()
    }

}



