import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;    
import { JsonManager } from "./JsonManager";
import InGameView from "../View/InGameView";
import { GameTools } from "./GameTools";
import { EventManager, StageMode } from "./EventManager";
import { GameManager } from "../Main/GameManager";
import { AccountManager } from "../Net/AccountManager";

export  class GamePool  {
   //单例
   private static Ins:GamePool;

   public BallPrefab:string[] = new Array<string>("2","4","8","16","32","64","128","256","512","1024","2048","4096","8192","16384");
   //是否已经克隆完毕
   public Cloned:boolean = false;
   //球预制体数组
   public PrefabControl:Laya.Prefab[] = [];

    constructor() {
        GamePool.Ins = this;
        EventManager.StageOn(StageMode.Start,this,this.FreshScore);
        this.Init();
      }
    public static get Instance():GamePool{
        if(GamePool.Ins){
            return GamePool.Ins
        }
        else{
            return new GamePool();
        }
    }
    Init(){
        for (let i = 0; i < 14; i++) {
            this.ReadJSON(i);
        }
        // this.FreshScore();
    }
    ReadJSON(i:number){
        Laya.loader.create("Prefab/"+this.BallPrefab[i]+".json",Laya.Handler.create(this,this.creat));
    }
    creat(Prefab:any){
        let obj = new Laya.Prefab();
        obj.json = Prefab;
        this.PrefabControl.push(obj);
    }
    /**创建球预制体 */
    CreatPrefab(id:number,_pos:Laya.Vector2){
       let ball = this.PrefabControl[id];
       let pre = Laya.Pool.getItemByCreateFun(this.BallPrefab[id],ball.create,ball) as Laya.Image;
       pre.pos(_pos.x,_pos.y);
       EventManager.StageTrigger(StageMode.Clone,[pre]);
       this.Cloned = true;
    }
    //测试用克隆方法
    CreatPrefab2(id:number,_pos:Laya.Vector2){
       let ball = this.PrefabControl[id];
       let pre = Laya.Pool.getItemByCreateFun(this.BallPrefab[id],ball.create,ball) as Laya.Image;
       pre.pos(_pos.x,_pos.y);
       EventManager.StageTrigger(StageMode.Clone,[pre]);
    }
    /** 根据分数判断克隆哪种球体*/
    PrecossData(_num:number):number{
        for (let i = 0; i < this.BallPrefab.length; i++) {
            if(_num == Number(this.BallPrefab[i]) ){
                return i
            }
            if(_num>Number(this.BallPrefab[i]) && _num<Number(this.BallPrefab[i+1])){
                return i
            }
        }
    }
    /**通过总分数判断应该加多少分 */
    ProcessScore(_num):number{
        for (let i = 0; i < this.BallPrefab.length; i++) {
            if(_num == Number(this.BallPrefab[i]) ){
                return Number(this.BallPrefab[i]);
            }
            if(_num>Number(this.BallPrefab[i]) && _num<Number(this.BallPrefab[i+1])){
                return Number(this.BallPrefab[i])
            }
        }
    }
    //获取本关卡最小球 及 次小球
    FreshScore(){
        let level = JsonManager.Level;
        let stage = level[GameManager.Instance.STAGE];
        let edge = stage.edge.split(";");
        let min =Number(edge[0]);
        let minnext = Number(edge[1]);
        //获取消除道具中 本局 最小球 及 次小球
        let minball =Number(this.BallPrefab[min-1]);
        let minnextball = Number(this.BallPrefab[minnext-1]);
        GameManager.Instance.MinBall = minball;
        GameManager.Instance.MinNextBall = minnextball;
    }
    //测试，每隔一段时间掉落两个球
    FallDown(_time:number = 2) {
        let level = JsonManager.Level;
        let stage = level[GameManager.Instance.STAGE];
        let edge = stage.edge.split(";");
        let min =Number(edge[0]);
        let minnext = Number(edge[1]);
        //获取消除道具中 本局 最小球 及 次小球
        let minball =Number(this.BallPrefab[min-1]);
        let minnextball = Number(this.BallPrefab[minnext-1]);
        // InGameView.Instance.MinBall = minball;
        // InGameView.Instance.MinNextBall = minnextball;
        
        let max =Number(edge[edge.length-1]);
        for (let j = 0; j < _time; j++) {
            let num = GameTools.RandomANumber(min,max+1);
            let X = GameTools.RandomANumber(100,500);
            let pos = new Laya.Vector2(X, -30);
            this.CreatPrefab2(num-1,pos);
        }
    }
}