import { EventManager, EventType } from "./EventManager";
import { ScenceManager } from "./ScenceManager";
import { WindowManager, WindowType } from "../../Window/WindowManager";
import { LevelManager } from "./LevelManager";
import { Player } from "./Player";
import CameraManager from "./CameraManager";
import { AImanager } from "./AIManager";
import { GameUtils } from "../../GameUtils/GameUtils";
import { HomeWindow } from "../../Window/HomeWindow";
import { ResourcesManager } from "./ResourcesManager";
import { PlayerAccount, AccountUser } from "../Network/PlayerAccount";
import WxApi from "../../WXSDK/wxsdk";
import { SignWindow } from "../../Window/SignWindow";
import { Queue } from "./Queue";

export enum GameState{Nono,Idle,Queue,Playing,Die,Revive,Win,Over,End}
export class GameManager
{
    static Instance:GameManager;
    constructor()
    {
        GameManager.Instance = this;
        this.RegEvent();
        this.ChangeState(GameState.Nono);
    }

    private curState:GameState;
    public GetGoin:boolean = false;

    public gamegold = 0;
    videogetmoney = 3;

    RegEvent()
    {
        EventManager.GameStateEventOn(GameState.Nono,this,this.OnFristReadyGame);
        EventManager.GameStateEventOn(GameState.Idle,this,this.OnEnterIdle);
        EventManager.GameStateEventOn(GameState.Queue,this,this.OnEnterQueue);
        EventManager.GameStateEventOn(GameState.Playing,this,this.OnEnterPlaying);
        EventManager.GameStateEventOn(GameState.Die,this,this.OnEnterDie);
        EventManager.GameStateEventOn(GameState.Revive,this,this.OnEnterRevive);
        EventManager.GameStateEventOn(GameState.Win,this,this.OnEnterWin);
        EventManager.GameStateEventOn(GameState.Over,this,this.OnEnterOver);
        EventManager.GameStateEventOn(GameState.End,this,this.OnEnterEnd);
        EventManager.EventOn(EventType.GetMoney,this,this.OnGetMoney);
    }

    public ChangeState(_state:GameState,arges = null)
    {
        if(this.curState != _state)
        {
            this.curState = _state;
            EventManager.GameStateEventTrigger(_state,arges);
            console.log("GameState:"+GameState[_state]);
        }
        else
        {
            console.log("Can not ChangeState:State is curstate...");
        }
    }

    OnFristReadyGame()
    {
        WxApi.Instance.recordAldEvent("初始资源","");
        ResourcesManager.Instance.InitSkinAsset();
        var curstage = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.stage));
        LevelManager.Instance.ReadyLevel(curstage + 1,()=>
        {
            ScenceManager.Instance.GameScence.visible = true;
            ScenceManager.Instance.CreatLevelScene(true);
            WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
            WxApi.Instance.recordAldEvent("关卡展开","");
        })
    }

    OnEnterIdle()
    {
        
        var camera = ScenceManager.Instance.GameCamera;
        var pos = camera.transform.position.clone();
        Player.CanInput = false;
        this.gamegold = 0;
        ScenceManager.Instance.GameCamera.transform.position = new Laya.Vector3(camera.transform.position.x,camera.transform.position.y,camera.transform.position.z - 4);
        GameUtils.TweenMove(ScenceManager.Instance.GameCamera.transform,pos,0.3);
    }

    OnEnterQueue()
    {
        WindowManager.Instance.ShowWindow(WindowType.QueueWindow);
    }

    OnEnterPlaying()
    {
        Player.CanInput = true;
        WindowManager.Instance.ShowWindow(WindowType.InGameWindow);
    }
    OnEnterDie()
    {
        
    }
    OnEnterRevive()
    {
        WindowManager.Instance.ShowWindow(WindowType.ReviveWindow);
    }
    OnEnterWin()
    {
        
    }
    OnEnterOver()
    {

    }
    OnEnterEnd(_win:boolean)
    {
        CameraManager.Instance.StopFollow();
        WindowManager.Instance.ShowWindowWithData(WindowType.SettlementWindow,_win);
    }

    OnGetMoney()
    {
        this.gamegold += 1;
        EventManager.EventTrigger(EventType.MoneyChange,this.gamegold);
    }

    ReviveGame()
    {
        Player.Instance.RevivePlayer();
        WindowManager.Instance.ShowWindow(WindowType.InGameWindow);
    }

    RessetGame()
    {
        ScenceManager.Instance.GameLight.transform.rotationEuler = new Laya.Vector3(-50,-150,0);
        Player.Instance.RessetPlayer();
        AImanager.Instance.RessetAis();
        LevelManager.Instance.RessetLevel();
        GameManager.Instance.ChangeState(GameState.Idle);
    }

    EnterNextLevel()
    {
        ScenceManager.Instance.GameLight.transform.rotationEuler = new Laya.Vector3(-50,-150,0);
        Player.Instance.ClearPlayer();
        AImanager.Instance.ClearAis();
        LevelManager.Instance.ClearLevel();
        LevelManager.Instance.NextLevel();
    }
}