
import Vector3 = Laya.Vector3;
import Dictionary from "../Util/Dictionary";
export class PoolManager {
    private static instance: PoolManager;
    public static get Instance(): PoolManager {
        return PoolManager.instance || (PoolManager.instance = new PoolManager());
    }

    constructor() {
        this.poolDic = new Dictionary();
        this.prefabDic = new Dictionary();
    }

    //存储多个
    private poolDic: Dictionary;

    //预设对象
    private prefabDic: Dictionary;

    public InitData(name: string, prefab) {
        if (prefab != null) {
            if (!this.prefabDic.has(name))
                this.prefabDic.set(name, prefab);
        }
    }
    public SetActiveObject(name: string, scale?) {
        if (this.poolDic.has(name)) {
            if (this.poolDic.get(name).length > 0) {
                var result = this.poolDic.get(name).shift();
                result.active = true;
                return result;
            } else {
                if (this.prefabDic.has(name)) {
                    var curObj = Laya.Sprite3D.instantiate(this.prefabDic.get(name)) as Laya.Sprite3D;
                    if (scale != null) {
                        curObj.transform.localScale = new Vector3(scale, scale, scale);
                    }
                    curObj.name = name;
                    return curObj;
                } else {
                    return null;
                }
            }
        } else {
            if (this.prefabDic.has(name)) {
                var curObj = Laya.Sprite3D.instantiate(this.prefabDic.get(name)) as Laya.Sprite3D;
                if (scale != null) {
                    curObj.transform.localScale = new Vector3(scale, scale, scale);
                }
                curObj.name = name;
                return curObj;
            } else {
                return null;
            }
        }
    }

    //隐藏对象到dic
    public SetDisableObject(go: Laya.Sprite3D) {
        if (go) go.active = false;
        var key = go.name;
        if (!this.poolDic.has(key)) {
            this.poolDic.set(key, new Array<any>());
        }
        this.poolDic.get(key).push(go);
    }

    public DestoryObj(go: Laya.Sprite3D) {
        if (go) go.destroy(true);
    }

    //延时隐藏对象到dic
    public SetDisableMutiObject(go: Laya.Sprite3D, timer) {
        Laya.timer.once(timer, this, () => {
            if (go) go.active = false;
            var key = go.name;
            if (!this.poolDic.has(key)) {
                this.poolDic.set(key, new Array<any>());
            }
            this.poolDic.get(key).push(go);
        });
    }


}