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('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.playSFX('Free_Load_Sound');
        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, 'in', false);
        this.Node_1.getComponent(UIOpacity).opacity = 0;
        this.scheduleOnce(() => {

            tween(this.Node_1.getComponent(UIOpacity))
                .to(0.2, { opacity: 255 })
                .start();

            tween(this.Node_1)
                .set({ scale: v3(0.5, 0.5, 0.5) })
                .to(0.2, { scale: v3(1, 1, 1) })
                .start();

        }, 0.8)


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

    // 加入回调
    onClose(isClick: boolean = true) {
        if (isClick) {
            AudioManager.instance.playSFX('Total_Settle_Collect_Click');
        }
        this.btn.node.off(Button.EventType.CLICK, this.onClose, this);
        this.btn.node.active = false;
        this.unscheduleAllCallbacks();
        this.fadeOutScoreLabel(0.5, 0.5);
        this.freeSpinEnterSpine.clearTracks();
        this.freeSpinEnterSpine.setAnimation(0, 'out', true);
        this.freeSpinEnterSpine.setCompleteListener(() => {
            this.freeSpinEnterUI.removeFromParent();
            NodePoolManager.instance.putNodeToPool('freeSpinEnter', this.freeSpinEnterUI);
            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();

    }
}


