

export default class PoolManager
{
    private static PoolManagerInstance:PoolManager;

    constructor()
    {
        PoolManager.PoolManagerInstance = this;
    }

    public static get Instance()
    {
        if(PoolManager.PoolManagerInstance)
        {
            return PoolManager.PoolManagerInstance;
        }
        else
        {
            var instance = new PoolManager();
            return instance;
        }
    }

    

    private static PoolSize = 80;
    private static Sprite3DDic:{[key:string]:Array<Laya.Sprite3D>} = {};


    static GetSprite3D(_obj:Laya.Sprite3D):Laya.Sprite3D
    {
        var objName = _obj.name;
        if(this.Sprite3DDic[objName] != null && this.Sprite3DDic[objName] != undefined)
        {
            if(this.Sprite3DDic[objName].length>0)
            {
                var obj = this.Sprite3DDic[objName][0];

                if(!obj || obj.destroyed)obj = Laya.Sprite3D.instantiate(_obj);

                this.Sprite3DDic[objName].splice(0,1);

                // obj.transform.position = _obj.transform.position.clone();
                // obj.transform.rotation = _obj.transform.rotation.clone();
                obj.transform.scale=_obj.transform.scale.clone();
                obj.active = true;
                return obj;
            }
            else
            {
                var newobj = Laya.Sprite3D.instantiate(_obj);
                newobj.active = true;
                return newobj;
            }
        }
        else
        {
            var newobj = Laya.Sprite3D.instantiate(_obj);
            newobj.active = true;
            return newobj;
        }
    }

    static RecoverSprite3D(_obj:Laya.Sprite3D,_active = false)
    {
        if(!_obj)
        {
            console.warn("PoolManager Recover Error!",null);
            return;
        }

        if(_obj.destroyed)
        {
            console.warn("PoolManager Recover Error!","destroyed");
            return;
        }

        if(!_active && _obj.active == false)
        {
            console.warn("PoolManager Recover Error!",_obj.name);
            return;
        }

        var objName = _obj.name;

        if(this.Sprite3DDic[objName] != null && this.Sprite3DDic[objName] != undefined)
        {
            var array = this.Sprite3DDic[objName];
            if(array.length > this.PoolSize)
            {
                _obj.destroy();
                return;
            }
            else
            {
                array.push(_obj);
            }
        }
        else
        {
            array = [];
            array.push(_obj);
            this.Sprite3DDic[objName] = array;
            //console.log(objName,array.length);
        }

        _obj.transform.position = new Laya.Vector3(0,0,-100);
        _obj.removeSelf();
        _obj.active = false;
    }


    static ClearPool()
    {
        for(var key in this.Sprite3DDic)
        {
            var objs = this.Sprite3DDic[key];
            for(var i = 0;i< objs.length;i++)
            {
                objs[i].destroy();
            }
        }
        this.Sprite3DDic = {};
    }
}