// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import PoolMgr, { NODEPOOLPREFABTYPE } from "../../mgr/PoolMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SpineEffectPlay extends cc.Component {


    /**spine动画 */
    public spine: sp.Skeleton = null;
    /**当前的动画名称 */
    public ifLoop: boolean = false;
    /**当前的动画名称 */
    public curAniName: string = "";
    /**spine动画播放结束回调 */
    public spineAniEndCallBack: Function = null;

    @property({
        type: cc.Enum(NODEPOOLPREFABTYPE),
        serializable: true
    })
    public nodePoolPrefabType: NODEPOOLPREFABTYPE = 0;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

    }

    onEnable() {
        this.spine = this.getComponent(sp.Skeleton);
        if (!this.spine) {
            mk.console.error("SpineEffectPlay 绑定的节点没有spine")
            return;
        }
        this.spine.setAnimation(0, this.spine.animation, false);
        this.spine.setCompleteListener(() => {
            this.spineLoopAniComplete();
        })
    }

    // update (dt) {}
    /**播放Spine动画 */
    playSpineAni(name: string, ifLoop: boolean = false, callBack: Function = null) {
        if (name) {
            this.curAniName = name;
            this.spine.setAnimation(0, name, ifLoop)
        }
       this.spineAniEndCallBack = callBack;
    }

    /**Spine动画循环播放结束回调函数 */
    spineLoopAniComplete() {
        //LogUtil.log("动画播放完毕！！！！！！")
        if (this.spineAniEndCallBack) {
            this.spineAniEndCallBack();
        }
        if (!this.ifLoop) {
            PoolMgr.Inst.recyclePoolPrefab(this.nodePoolPrefabType, this.node);
        }
    }

    clearLoopAniCompleteCallBack() {
        this.spineAniEndCallBack = null;
    }

}


