import { sp } from "cc";
import GRoleBase from "../role/GRoleBase";
import JNSkeleton from "../../../../../extensions/ngame/assets/ngame/sync/frame/game/spine/JNFrameSkeleton";
import { Node } from "cc";
import GBaseMode from "../../GBaseMode";
import { Vec2 } from "cc";
import GNode from "./GNode";


//Spine 工具
export default class GSpine{

    //保存当前Spine 播放新 Spine 结束后还原
    static onPlayAnotherSpine(role:GRoleBase<{}>,spine:sp.SkeletonData,play:string,fun:{
        start?:(ske:JNSkeleton) => void,
        end?:() => void,
    } = {}):JNSkeleton{

        //关闭当前角色原本的Spine
        role.spine.enabled = false;
        //添加新的spine
        let spineNode =  GNode.create();
        role.node.addChild(spineNode);
        let another = spineNode.addComponent(JNSkeleton);
        another.skeletonData = spine;
        fun.start && fun.start(another);
        another.setCompleteListener(() => {
            //还原
            role.spine.enabled = true;
            spineNode.destroy();
            fun.end && fun.end();
        });
        another.setAnimation(0,play,false);
        // another.setAnimation(0,play,false);
        return another;

    }

    //创建一个Spine
    static onCreateSpine(spine:sp.SkeletonData):JNSkeleton{

        let spineNode =  GNode.create();
        let another = spineNode.addComponent(JNSkeleton);
        another.premultipliedAlpha = false;
        another.skeletonData = spine;
        return another;

    }

    // 创建一个 Spine 并且 播放 销毁
    static onPlaySceneSpine(scene:GBaseMode<{},{}>,pos:Vec2,spine:sp.SkeletonData,play:string):JNSkeleton{

        let create = this.onCreateSpine(spine);
        scene.addGNode(create.node,pos);
        create.setCompleteListener(() => {
            //销毁
            create.node.destroy();
        });
        create.setAnimation(0,play,false);
        return create;

    }

}
