import { AssetManager, ResourcesPath } from "../Tools/AssetManager";
import GamePool from "../Tools/GamePool";
import { EventManager, EventState, StageState } from "../Tools/EventManager";
import { PlayerControl } from "./PlayerControl";
import { ViewManager, ViewType } from "../View/ViewManager";
import { GameTools } from "../Tools/GameTools";
import { NpcControl } from "./NpcControl";
import { StageControl } from "./StageControl";
import { ChaseNpc } from "./ChaseNpc";
import { Hostage } from "./Hostage";
import { Boss } from "./Boss";
import { NpcRunner } from "./NpcRunner";
import { Runner } from "./Runner";
import InGameView from "../View/InGameView";
import { AccountManager } from "../Net/AccountManager";
import { AccountUser } from "../Net/PlayerData";


export class GameManager{
    private static ins:GameManager;
    
    //游戏开始
    public started:boolean = false;
    //新手指引关是否打到敌人
    public GuideKill:number = 0;
    //所杀敌人数量
    public KillNum:number = 0;
    //复活次数
    public ReviveNum:number = 1;
    //人物数组
    public CharacterArray:Array<Laya.Sprite3D> = [];
    //UI数组
    public UIArray:any[] = [];

    public mainscene:Laya.Scene3D;

    //瞄准线位置
    private raypos:Laya.Sprite3D;
    //弓位置
    private archerpos:Laya.Sprite3D;
    //箭位置
    private arrowpos:Laya.Sprite3D;
    //帽子位置
    private cappos:Laya.Sprite3D;

    //栅栏左边界
    public fenceleftpos:number = 0;
    //栅栏右边界
    public fencerightpos:number = 0;
    //树左边界
    public treeleftpos:number = 0;
    //树右边界
    public treerightpos:number = 0;

    constructor(){
        GameManager.ins = this;
        EventManager.EventOn(EventState.StageInit,this,this.CreatStage);
        EventManager.EventOn(EventState.StageCreateComplete,this,this.Init);
    }
    public static get Instance():GameManager{
        if(GameManager.ins){
            return GameManager.ins
        }
        else{
            return new GameManager();
        }
    }
    Init(){
        this.CreatCharacter();
        this.mainscene = AssetManager.Instance.mainscene;
        Laya.stage.addChildAt(this.mainscene,0);
        ViewManager.Instance.ShowView(ViewType.MainView);
    }
    CreatStage(){
        // console.log(StageControl.Instance.curstagedic);
        StageControl.Instance.RessetStage();
    }
    CreatCharacter(){
        this.CreatPlayer();
    }
    //创建玩家
    CreatPlayer(){
            var equiphorseid = Number(AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["2"]);
            var equipcap = Number(AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["1"]);
        
            var playermodel = GamePool.Instance.GetModel(AssetManager.Instance.Player.get(1),"");
            var horsemodel = GamePool.Instance.GetModel(AssetManager.Instance.Horse.get(equiphorseid),"");
            playermodel.addChild(horsemodel);
            playermodel.transform.position = new Laya.Vector3(0,0,-9);
            horsemodel.transform.localScale = new  Laya.Vector3(1,1,1);
            horsemodel.transform.localPosition = new Laya.Vector3(0,0,0);
            this.raypos = playermodel.getChildByName("raypos") as Laya.Sprite3D;
            this.archerpos = playermodel.getChildByName("archerpos") as Laya.Sprite3D;
            this.arrowpos = playermodel.getChildByName("arrowpos") as Laya.Sprite3D;
            this.cappos = playermodel.getChildByName("cappos") as Laya.Sprite3D;
            if(equipcap){
                var capmodel = GamePool.Instance.GetModel(AssetManager.Instance.Cap.get(equipcap),"");
                this.cappos.addChild(capmodel);
                capmodel.transform.localScale = new Laya.Vector3(0.5,0.5,0.5);
                capmodel.transform.localPosition = new Laya.Vector3(0,0,0);
                capmodel.transform.localRotationEuler = new Laya.Vector3(-90,0,0);
            }
            let ray = this.raypos.getChildAt(0);
            if(!ray){
                this.CreatRay(this.raypos.transform.position.clone(),this.raypos.transform.rotationEuler.clone(),this.raypos);
            }
            let archer = this.archerpos.getChildAt(0);
            if(!archer){
                this.CreatArcher(this.archerpos.transform.position.clone(),this.archerpos.transform.rotationEuler.clone(),this.archerpos);
            }
            // this.CreatArrow(this.arrowpos.transform.position.clone(),this.arrowpos.transform.rotationEuler.clone(),this.arrowpos);
            PlayerControl.Instance.Init(playermodel);
            AssetManager.Instance.mainscene.addChild(playermodel);
            this.CharacterArray.push(playermodel);
            // this.CharacterArray.push(horsemodel);
            GameTools.Instance.PlayerSetCollider(playermodel,200);
    }
    //创建敌人npc
    CreatEnemy(enemy:Laya.Sprite3D){
        enemy.transform.rotationEuler = new Laya.Vector3(0,180,0);
        NpcControl.Instance.Init(enemy);
        GameTools.Instance.EnemySetCollider(enemy,100);
        this.CharacterArray.push(enemy);
    }
    //克隆追逐的玩家以及npc
    CreatChaseCharacter(_node:Laya.Sprite3D){
        let enemy = _node.getChildByName("enemy").getChildAt(0) as Laya.Sprite3D;
        let hostage = _node.getChildByName("player").getChildAt(0) as Laya.Sprite3D;
        hostage.transform.localPosition = new Laya.Vector3(0,0,0);
        hostage.transform.localRotationEuler = new Laya.Vector3(0,0,0);

        enemy.transform.localPosition = new Laya.Vector3(0,0,-2);
        enemy.transform.localRotationEuler = new Laya.Vector3(0,0,0);

        GameTools.Instance.EnemySetCollider(enemy,100);
        GameTools.Instance.HostageSetCollider(hostage,200);
        let chase = enemy.getComponent(ChaseNpc) as ChaseNpc;
        if(chase){
            chase.destroy();
        }
        else{
            let _ChaseNpc = enemy.addComponent(ChaseNpc) as ChaseNpc;
        }
        let host = hostage.getComponent(Hostage) as Hostage;
        if(host){
            host.destroy();
        }
        else{
            let _Hostage = hostage.addComponent(Hostage) as Hostage;
        }
        this.CharacterArray.push(_node);
    }
    //创建boss
    CreatBoss(_pos:Laya.Vector3){
        var bossmodel = GamePool.Instance.GetModel(AssetManager.Instance.Boss.get(1),"");
        var horsemodel = GamePool.Instance.GetModel(AssetManager.Instance.Horse.get(50),"");
        let bos = bossmodel.getComponent(Boss) as Boss;
        if(bos){
            let _boss = bos;
        }
        else{
            let _boss = bossmodel.addComponent(Boss) as Boss;
        }
        // bossmodel.addChild(horsemodel);
        horsemodel.addChild(bossmodel);
        bossmodel.transform.localPosition = new Laya.Vector3(0,0,0);
        bossmodel.transform.localScale = new  Laya.Vector3(1,1,1);
        horsemodel.transform.position = _pos.clone();
        AssetManager.Instance.mainscene.addChild(horsemodel);
        this.CharacterArray.push(horsemodel);
        // this.CharacterArray.push(bossmodel);
    }
    /**回收人物模型 */
    RecoveryCharacter(){
        for (let i = 0; i < this.CharacterArray.length; i++) {
            let model = this.CharacterArray[i];
            switch (model.name) {
                case "npc":
                    let npcrunner = model.getComponent(NpcRunner) as NpcRunner;
                    if (npcrunner)npcrunner.destroy();
                    break;
                case "pursue":
                    let hostage = model.getChildByName("player").getChildAt(0).getComponent(Hostage) as Hostage;
                    let chasenpc = model.getChildByName("enemy").getChildAt(0).getComponent(ChaseNpc) as ChaseNpc; 
                    if(hostage)hostage.destroy();
                    if(chasenpc)chasenpc.destroy();
                    break;
                case "horse1":
                    let boss = model.getChildByName("boss") as Laya.Sprite3D;
                    boss.active = false;
                    boss.removeSelf();
                    GamePool.Instance.RecoveryModel(boss);
                    let _bs = boss.getComponent(Boss) as Boss;
                    if(_bs){
                        for (let i = 0; i < _bs.arrowarray.length; i++) {
                            let arrow = _bs.arrowarray[i];
                            GamePool.Instance.RecoveryModel(arrow);
                            _bs.arrowarray[i] = null;
                        }
                        _bs.arrowarray.splice(0,_bs.arrowarray.length);
                        _bs.destroy();
                    }
                    break;
                case "player":
                    let runner = model.getComponent(Runner) as Runner;
                    let horse = model.getChildAt(model.numChildren-1) as Laya.Sprite3D;
                    let cap = model.getChildByName("cappos").getChildAt(0) as Laya.Sprite3D;
                    if(cap){
                        cap.removeSelf();
                        GamePool.Instance.RecoveryModel(cap);
                    }
                    horse.active = false;
                    horse.removeSelf();
                    GamePool.Instance.RecoveryModel(horse);
                    if (runner){
                        for (let i = 0; i < runner.arrowarray.length; i++) {
                            let arrow = runner.arrowarray[i];
                            GamePool.Instance.RecoveryModel(arrow);
                            runner.arrowarray[i] = null;
                        }
                        runner.arrowarray.splice(0,runner.arrowarray.length);
                        runner.destroy();
                    } 
                    break;
            }
            GamePool.Instance.RecoveryModel(model);  
            this.CharacterArray[i] = null;
        }
        this.CharacterArray.splice(0,this.CharacterArray.length);
        
        for (let i = 0; i < this.UIArray.length; i++) {
            let UI = this.UIArray[i];
            UI.removeSelf();
            UI.destroy();
        }
        this.UIArray.splice(0,this.UIArray.length)
    }
    //创建瞄准线
    CreatRay(pos:Laya.Vector3,euler:Laya.Vector3,raypos:Laya.Sprite3D){
        var ray = this.CreatRayPrefab();
        raypos.addChild(ray);
        ray.transform.position = pos;
        ray.transform.rotationEuler = euler;
        // let scale = ray.transform.scale.clone();
        // scale.z = 0.1;
        // scale.y = 3;
        // scale.x = 2;
        // ray.transform.scale = scale;
    }
    //创建瞄准线预设
    CreatRayPrefab():Laya.Sprite3D{
        var ray = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(18),"");
        return ray
    }
    //创建弓箭预设
    CreatArcher(_pos:Laya.Vector3,_euler:Laya.Vector3,archerpos:Laya.Sprite3D){
        var archer = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(16),"");
        archerpos.addChild(archer);
        archer.transform.position = _pos;
        archer.transform.rotationEuler = _euler;
        var scale = archer.transform.scale.clone();
        scale.x = 0.3;
        scale.y = 0.3;
        scale.z = 0.6;
        archer.transform.scale = scale;
    }
    Revive(){
        PlayerControl.Instance.Revive();
        ViewManager.Instance.ShowView(ViewType.IngameView);
    }
    //换马
    SetHorse(_id:number){
        let horsemodel = PlayerControl.Instance.Player.getChildAt(PlayerControl.Instance.Player.numChildren-1) as Laya.Sprite3D;
        horsemodel.removeSelf();
        GamePool.Instance.RecoveryModel(horsemodel);
        var newhorsemodel = GamePool.Instance.GetModel(AssetManager.Instance.Horse.get(_id),"");
        PlayerControl.Instance.Player.addChild(newhorsemodel);
        newhorsemodel.transform.localScale = new  Laya.Vector3(1,1,1);
        newhorsemodel.transform.localPosition = new Laya.Vector3(0,0,0);
        PlayerControl.Instance._runner.m_horseanimator = newhorsemodel.getComponent(Laya.Animator) as Laya.Animator;
    }
    //换帽子
    SetCap(_id:number){
        let cappos = PlayerControl.Instance._runner.m_player.getChildByName("cappos") as Laya.Sprite3D;
        let cap = cappos.getChildAt(0) as Laya.Sprite3D;
        if(cap){
            cap.removeSelf();
            GamePool.Instance.RecoveryModel(cap);
        }
        let newcap = GamePool.Instance.GetModel(AssetManager.Instance.Cap.get(_id),"");
        cappos.addChild(newcap);
        newcap.transform.localPosition = new Laya.Vector3(0,0,0);
        newcap.transform.localScale = new Laya.Vector3(0.5,0.5,0.5);
        newcap.transform.localRotationEuler = new Laya.Vector3(-90,0,0);
    }
    
}