import { EventManager, StageMode } from "../Tool/EventManager";
import { ViewManager, ViewType } from "../View/ViewManager";
import { GameManager } from "./GameManager";
import { DropRule } from "../Tool/DropRule";

export class GameControl{
    private static ins:GameControl;

    
    
    constructor(){
        GameControl.ins = this;
        this.Init();
    }
    public static get Instance():GameControl{
        if(GameControl.ins){
            return GameControl.ins
        }
        else{
            return new GameControl();
        }
    }
    Init(){
        EventManager.StageOn(StageMode.Start,this,this.GameStart)
        EventManager.StageOn(StageMode.Lose,this,this.GameLose)
        EventManager.StageOn(StageMode.Win,this,this.GameWin)
    }
    GameStart(){
        ViewManager.Instance.ShowView(ViewType.InGameView);
        if(DropRule.Instance.STAGE == 1){
            DropRule.Instance.GuideFallDown();
            return;
        }
        if(DropRule.Instance.progress.length <= 1 ){
            DropRule.Instance.FallDown(DropRule.Instance.StartNum);
        }
        else{
            DropRule.Instance.RecoverScene();
        }
    }
    GameLose(){
        // Laya.timer.once(1000,this,()=>{
            if(GameManager.Instance.ReviveNum>0){
                ViewManager.Instance.OpenPopView(ViewType.ReviveView);
                GameManager.Instance.ReviveNum--;
            }
            else{
                ViewManager.Instance.OpenPopView(ViewType.FailureView);
            }
        // })
    }
    GameWin(){
        // Laya.timer.once(1000,this,()=>{
            ViewManager.Instance.OpenPopView(ViewType.WinView);
        // })
    }
}