import { Node, NodePool, sp, Vec3 } from "cc";
import { cocosUtil } from "../../utils/cocosUtil";
import { constants } from "../data/constants";
import { designManager } from "./designManager";
import { resManager } from "./resManager";


export class skillManager {

    private static _instance: skillManager;
    private constructor() { }
    public static get instance(): skillManager {
        if (!this._instance) {
            this._instance = new skillManager();
        }
        return this._instance;
    }

    skeletonDatasObj: any = {};
    namePoolObj: any = {};

    loadAllSkillEffect(onProgress?: Function, onComplete?: Function) {
        resManager.instance.loadAssetByBundleDir(constants.bundles.common.name,
            constants.bundles.common.spine.skillDir, sp.SkeletonData, onProgress, (skeletonDataArr) => {
                for (let i in skeletonDataArr) {
                    let skeletonData = skeletonDataArr[i];
                    this.skeletonDatasObj[skeletonData.name] = skeletonData;
                }
            });
    }

    showSkillEffect(id: number, worldPos: Vec3, parent: Node, cb?: Function) {
        let row = designManager.instance.getRowById(constants.tableName.skill, id);
        if (!row || !row.name) {
            if (cb) {
                cb();
            }
            return;
        }

        let name = row.body;
        let skeletonData = this.skeletonDatasObj[name];
        let tmpFunc = () => {
            let node = this.getPoolByName(name).get();
            let skeleton = null;
            if (!node) {
                node = new Node();
                skeleton = node.addComponent(sp.Skeleton);
                skeleton.premultipliedAlpha = false;
                skeleton.skeletonData = skeletonData;
            } else {
                skeleton = node.getComponent(sp.Skeleton);
            }
            if (name == "ljf") {
                node.setScale(1.3, 1.3);
            }
            skeleton.setAnimation(0, "animation", false);
            node.parent = parent;
            let pos = cocosUtil.convertToNodeSpace(node, worldPos);
            node.position = pos;
            skeleton.setCompleteListener(() => {
                this.getPoolByName(name).put(node);
                if (cb) {
                    cb();
                }
            });
        };
        if (!skeletonData) {
            let path = constants.bundles.common.spine.skillDir + "/" + name + "/" + name;
            resManager.instance.loadAsset(constants.bundles.common.name, path, sp.SkeletonData, () => { }, (err, sd) => {
                if (err) {
                    return;
                }
                skeletonData = sd;
                this.skeletonDatasObj[name] = skeletonData;
                tmpFunc();
            });
            return;
        }
        tmpFunc();
    }

    getPoolByName(name: string): NodePool {
        let pool = this.namePoolObj[name];
        if (!pool) {
            pool = new NodePool();
            this.namePoolObj[name] = pool;
        }
        return pool;
    }

}

