
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 pooltype = {
      Tower:"Tower",
      PlayerBullet:"PlayerBullet",
      player:"player",
      //friend:"friend",
      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 InitData(name, prefab) {
        if (prefab != null) {
            if (!this.prefabDic.has(name))
                this.prefabDic.set(name, prefab);
        }
    }

    public HasSprInPool(name){
        return this.prefabDic.has(name);
    }

    static bulletCount=0;
    public SetActiveObject(name: string): Sprite3D {
        //  if (name.indexOf("Road_") != -1) {
        //     var curObj = Sprite3D.instantiate(this.prefabDic.get(name)) as Sprite3D;
        //     curObj.name = name;
        //     console.log("正在克隆中>>>>>" + name + "__id:" + curObj.id)
        //     return curObj;
        //    // return;
        //  }
        if(name=="PlayerBullet")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 SetActiveForUi(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);
        }
    }
    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";
                // _t.stroke = 4;
                // _t.strokeColor = "#ff2800";
                return _t;
            }
    }
    public SetDisableForUi(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 SetDisableObject(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);
    }

    //延时隐藏对象到dic
    public SetDisableMutiObject(go: 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);
        });
    }


}