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;
    btnNode: Node | null = null;

    freeSpinEnterSpine: sp.Skeleton | null = null;
    Node_1: Node | null = null;
    numNode: Node | null = null;
    LabelNode: Node | null = null;
    contentNode: 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');
        AudioManager.instance.stopBGM()
        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');
        this.numNode = this.Node_1.getChildByName('NumNode');
        this.LabelNode = this.Node_1.getChildByName('LabelNode');
        this.NumNodeOpacityCom = this.numNode.getComponent(UIOpacity);
        this.NumNodeOpacityCom.opacity = 255;
        Tween.stopAllByTarget(this.NumNodeOpacityCom.node);
        this.freeCountLabel = this.numNode.getChildByName('Label').getComponent(Label);

        this.contentNode = this.Node_1.getChildByName('ContentNode');
        this.contentNode.getComponent(UIOpacity).opacity = 255

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

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

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

    playAnimation() {
        this.freeSpinEnterSpine.clearTracks();
        this.freeSpinEnterSpine.setAnimation(0, 'in', false);
        this.contentNode.getComponent(UIOpacity).opacity = 0;
        this.NumNodeOpacityCom.opacity = 0;
        this.LabelNode.getComponent(UIOpacity).opacity = 0;

        this.scheduleOnce(() => {
            this.tweenAnim(this.numNode)
            this.tweenAnim(this.LabelNode)
            this.tweenAnim(this.contentNode)
        }, 0.2)


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

    tweenAnim(node: Node) {
        tween(node.getComponent(UIOpacity))
            .to(0.2, { opacity: 255 })
            .start();

        tween(node)
            .set({ scale: v3(0.5, 0.5, 0.5) })
            .to(0.2, { scale: v3(1, 1, 1) })
            .call(() => {
                this.btnNode.active = true;
            })
            .start();
    }

    // 加入回调
    onClose(isClick: boolean = true) {
        this.btn.node.off(Button.EventType.CLICK, this.onClose, this);
        this.btnNode.active = false;
        this.unscheduleAllCallbacks();
        this.fadeOutScoreLabel(this.numNode, 0.5, 0.5);
        this.fadeOutScoreLabel(this.contentNode, 0.5, 0.5);
        this.fadeOutScoreLabel(this.LabelNode, 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(node: Node, delay: number, duration: number) {
        // 先停止可能正在进行的动画
        Tween.stopAllByTarget(node);

        // 确保标签是可见的
        node.getComponent(UIOpacity).opacity = 255;

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

    }
}


