
import Sprite3D = Laya.Sprite3D;
import { EventManager, EventType } from "./EventManager";
import { Runner } from "./Runner";
import { GameState } from "./GameManager";

export class MeshSprite3DData
{
    constructor(_sprite:Sprite3D)
    {
        this.ownSprite = _sprite;
        this.position = _sprite.transform.position.clone();
        this.rotation = _sprite.transform.rotation.clone();
        this.scale = _sprite.transform.scale.clone();
    }
    public ownSprite:Sprite3D;
    public position:Laya.Vector3;
    public rotation:Laya.Quaternion;
    public scale:Laya.Vector3;
}

export default class PoolManager
{
    private static PoolManagerInstance:PoolManager;

    constructor()
    {
        PoolManager.PoolManagerInstance = this;
        EventManager.GameStateEventOn(GameState.Idle,this,this.ClearPool);
    }

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

    private PoolSize = 30;
    private ObjDic:{[key:string]:Array<MeshSprite3DData>} = {};

    CreatMeshSprite3D(_obj:Sprite3D,_group:string):Sprite3D
    {
        
        var objName = _group + _obj.name;
        if(this.ObjDic[objName] != null && this.ObjDic[objName] != undefined)
        {
            if(this.ObjDic[objName].length>0)
            {
                //return _obj.clone();
                var obj = this.ObjDic[objName][0];
                this.ObjDic[objName].splice(0,1);
                if(obj.ownSprite.destroyed)
                {
                    obj.ownSprite = Sprite3D.instantiate(_obj);
                }
                obj.ownSprite.active = true;
                obj.ownSprite.transform.position = _obj.transform.position.clone();
                obj.ownSprite.transform.rotation = _obj.transform.rotation.clone();
                obj.ownSprite.transform.scale = _obj.transform.scale.clone();
                return obj.ownSprite;
            }
            else
            {
                return Sprite3D.instantiate(_obj);
            }
        }
        else
        {
            return Sprite3D.instantiate(_obj);
        }
    }

    RecoverMeshSprite3D(_obj:Sprite3D,_group = "",_destory = false)
    {
        if(!_obj)
        {
            return;
        }
        if(_destory)
        {
            _obj.destroy();
            return;
        }

        if(_obj.destroyed)
        {
            return;
        }

        if(_obj.active == false)
        {
            return;
        }

        var objName = _group + _obj.name;

        if(this.ObjDic[objName] != null && this.ObjDic[objName] != undefined)
        {
            var array = this.ObjDic[objName];
            if(array.length > this.PoolSize)
            {
                _obj.destroy();
                return;
            }
            else
            {
                array.push(new MeshSprite3DData(_obj));
            }
        }
        else
        {
            array = [];
            array.push(new MeshSprite3DData(_obj));
            this.ObjDic[objName] = array;
        }

        var runner = _obj.getComponent(Runner) as Runner;
        if(runner)runner.destroy();

        var rig = _obj.getComponent(Laya.Rigidbody3D) as Laya.Rigidbody3D;
        if(rig)
        {
            rig.isKinematic = true;
            rig.detectCollisions = false;
        }

        //_obj.transform.localRotationEuler = new Laya.Vector3(0,0,0);
        //_obj.transform.scale = new Laya.Vector3(10,10,10);

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


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