import { PlayerManager } from "./PlayerManager";
import { DispatcherManager, StageState } from "../GameUtils/EventManager";
import WallBehav from "../Game/WallBehav";
import { ViewManager, ViewType } from "../View/ViewManager";
import { MateMove } from "../Game/MateMove";
import { CameraManager } from "./CameraManager";
import { AudioManager, BGMType } from "./AudioManager";

export class LevelManager{
    private static ins:LevelManager;
    constructor(){
        LevelManager.ins = this;
    }
    public static get Instance():LevelManager{
        if(LevelManager.ins == null){
            return new LevelManager();
        }
        return LevelManager.ins;
    }
    public spritepool:Laya.Sprite3D;

    RegEvent(){
        DispatcherManager.StageOn(StageState.Lose,this,this.GameLose);
        DispatcherManager.StageOn(StageState.Win,this,this.GameWin);
        DispatcherManager.StageOn(StageState.Pause,this,this.GamePause);
        DispatcherManager.StageOn(StageState.Resume,this,this.GameResume);
        DispatcherManager.StageOn(StageState.Over,this,this.GameStart);
    }
    GameStart(){
        PlayerManager.Instance.Init();
        CameraManager.Instance.Init();
        ViewManager.Instance.ShowView(ViewType.HomeView);
        AudioManager.PlayBGM(BGMType.bgm);
    }
    GameLose(){
        PlayerManager.Instance.ListenOff();
        Laya.timer.once(1000,this,()=>{
            ViewManager.Instance.ShowView(ViewType.GameResultView,false);
        })
        AudioManager.StopMusic();
    }
    GameWin(){
        PlayerManager.Instance.ListenOff();
        Laya.timer.once(1000,this,()=>{
            ViewManager.Instance.ShowView(ViewType.GameResultView,true);
        })
        AudioManager.StopMusic();
    }
    GamePause(){

    }
    GameResume(){

    }
    BackMain() {
        for (let i = this.spritepool.numChildren - 1; i >= 0; i--) {
            let child = this.spritepool.getChildAt(i);
            let _well = child.getComponent(WallBehav) as WallBehav;
            if (_well) {
                _well.destroy();
            }
            let _matemove = child.getComponent(MateMove) as MateMove;
            if(_matemove){
                _matemove.destroy();
            }
            child.removeSelf();
            child.destroy();
            child = null;
        }
    }
}