
import Vector3 = Laya.Vector3;
import Dictionary from "../Util/Dictionary";
import Sprite3D = Laya.Sprite3D;
export enum PoolType {
   // CoinText1 = 0,
    WarnTips = 1,
    HpText=2
}
export class GamePool {
    static readonly modeltype = {
      Mound:"Mound",
      PlayerShoot:"PlayerShoot",
      Player:"Character",
      Enemy:"enemy"
    }
    private static instance: GamePool;
    public static get Instance(): GamePool {
        return GamePool.instance || (GamePool.instance = new GamePool());
    }

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

    //存储多个
    private poolDic: Dictionary;

    //预设对象
    public prefabDic: Dictionary;

    //单个物体池子
    private singleobjDic: Dictionary;

    public InitSelf(name, prefab) {
        if (prefab != null) {
            if (!this.prefabDic.has(name))
                this.prefabDic.set(name, prefab);
        }
    }

/**************************************************************************************************** */
    public onComplete(s:any):void {
        Laya.Sprite3D.load("res/threeDimen/staticModel/lizard/lizard.lh", Laya.Handler.create(this, function(sprite:Laya.Sprite3D):void {
            var monster1:Laya.Sprite3D = this.scene.addChild(sprite) as Laya.Sprite3D;
            monster1.transform.position = new Laya.Vector3(-0.6, 0, 0);
            monster1.transform.localScale = new Laya.Vector3(0.075, 0.075, 0.075);
            var monster2:Laya.Sprite3D = Laya.Sprite3D.instantiate(monster1, this.scene, false, new Laya.Vector3(0.6, 0, 0));
            monster2.transform.localScale = new Laya.Vector3(0.075, 0.075, 0.075);
            for (var i:number= 0; i < monster2.getChildByName("lizard").numChildren; i++) {
                var meshSprite3D:Laya.MeshSprite3D = monster2.getChildByName("lizard").getChildAt(i) as Laya.MeshSprite3D;
                var material:Laya.BlinnPhongMaterial = meshSprite3D.meshRenderer.material as Laya.BlinnPhongMaterial;
                //法线贴图
                Laya.Texture2D.load(this.normalMapUrl[i], Laya.Handler.create(this, function(mat:Laya.BlinnPhongMaterial, texture:Laya.Texture2D):void {
                    mat.normalTexture = texture;
                }, [material]));
            }
            
            Laya.timer.frameLoop(1, this, function():void {
                monster1.transform.rotate(this.rotation);
                monster2.transform.rotate(this.rotation);
            });
        }));
    
    }
/**************************************************************************************************** */

    public GetUi(type): any {
        if (this.singleobjDic.has(type)) {
            if (this.singleobjDic.get(type).length > 0) {
                var result = this.singleobjDic.get(type).shift();
                result.visible = true;
                return result;
            } else {
                return this.GetByType(type);
            }
        } else {
            return this.GetByType(type);
        }
    }
    
    public RecoverUi(type, go) {
        go.visible = false;
        go.removeSelf();
        if (!this.singleobjDic.has(type)) {
            this.singleobjDic.set(type, new Array<any>());
        }
        this.singleobjDic.get(type).push(go);
    }

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

    static bulletCount=0;
    public GetModel(name: string): Sprite3D {
        if(name=="PlayerShoot")GamePool.bulletCount++;
        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 = Sprite3D.instantiate(this.prefabDic.get(name)) as Sprite3D;
                    curObj.name = name;
                    console.log("正在克隆中>>>>>" + name + "__id:" + curObj.id)
                    return curObj;
                } else {
                    return null;
                }
            }
        } else {
            if (this.prefabDic.has(name)) {
                var curObj = Sprite3D.instantiate(this.prefabDic.get(name)) as Sprite3D;
                curObj.name = name;
                  console.log("正在克隆中>>>>>" + name + "__id:" + curObj.id)
                return curObj;
            } else {
                return null;
            }
        }
    }

    /*********************************************************** */
    	/**
	 * 动画状态开始时执行。
	 */
	public onStateEnter():void {
		console.log("动画开始播放了");
	}
		
	/**
	 * 动画状态更新时执行。
	 */
	public onStateUpdate():void {
		console.log("动画状态更新了");
	}
		
	/**
	 * 动画状态退出时执行。
	 */
	public onStateExit():void {
		console.log("动画退出了");
    }
    /*********************************************************** */


    private GetByType(type) {
        if (type == PoolType.WarnTips) {
            var _t = new Laya.Text();
            _t.width = 720;
            _t.height = 60;
            _t.align = "center";
            _t.valign = "middle";
            _t.fontSize = 35;
            _t.bold = true;
            _t.pivotX = 360;
            _t.pivotY = 30;
            _t.color = "#ffffff";
            _t.stroke=5;
            _t.strokeColor="#000000"
            return _t;
        }
            else if (type == PoolType.HpText) {
                var _t = new Laya.Text();
                _t.font="Microsoft YaHei";
                _t.width = 260;
                _t.height = 50;
                _t.align = "center";
                _t.valign = "middle";
                _t.fontSize = 30;
                _t.bold = true;
                _t.pivotX = 130;
                _t.pivotY = 25;
                _t.color = "#ffffff";
                return _t;
            }
    }


}