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

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

    //存储多个
    private poolDic: Dictionary;

    //预设对象
    private prefabDic: Dictionary;

    // [ "Coin"  "Key"  "Role"  "Body1"   "Train" ]
    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);
        });
    }


}