import Dictionary from "../GameUtils/Dictionary";
import { LevelManager } from "../Manager/LevelManager";
import { CameraManager } from "../Manager/CameraManager";
import { DispatcherManager, EventState } from "../GameUtils/EventManager";
import { ViewManager, ViewType } from "../View/ViewManager";
import { NodeUtil } from "../GameUtils/NodeUtil";
import { MateMove } from "../Game/MateMove";
import WallBehav from "../Game/WallBehav";
import { ResourcesPath} from "../Resources/ResourcesPath";
import CoinBehav from "../Game/ConiBehav";
import { AccountManager } from "../NetWork/AccountManager";
import { GameManager } from "../Manager/GameManager";
export class AssetManager{
    private static ins:AssetManager;
    constructor(){
        AssetManager.ins = this;
        DispatcherManager.EventOn(EventState.LoadComplete,this,this.Init);
    }
    public static get Instance():AssetManager{
        if(AssetManager.ins == null){
            return new AssetManager();
        }
        return AssetManager.ins;
    }
    public Prop:Dictionary = new Dictionary();
    public Character:Dictionary = new Dictionary();
    public mainscene:Laya.Scene3D;
    public maincamera:Laya.Camera;
    public camerapos:Laya.Vector3;
    public camerarotate:Laya.Vector3;
    public root:Laya.Sprite3D;

    public curLevel:Laya.Sprite3D;
    public playerpos:Laya.Sprite3D;
    public finishpoint:Laya.Vector3;

    Init(){
        this.mainscene.enableFog = true;
        this.mainscene.fogStart = 25;
        this.mainscene.fogRange = 40;
        this.mainscene.fogColor = new Laya.Vector3(138/255,78/255,188/255);

        Laya.stage.addChild(this.mainscene);
        this.maincamera = this.mainscene.getChildByName("Main Camera") as Laya.Camera;
        this.maincamera.clearColor = new Laya.Vector4(138/255,78/255,188/255,1);
        this.maincamera.enableHDR = false;
        CameraManager.Instance.camera = this.maincamera;
        this.camerapos = this.maincamera.transform.position.clone();
        this.camerarotate = this.maincamera.transform.rotationEuler.clone();

        let _sprite = new Laya.Sprite3D();
        this.mainscene.addChild(_sprite);
        LevelManager.Instance.spritepool = _sprite;
        this.SetCharacterPos();
        LevelManager.Instance.RegEvent();
        LevelManager.Instance.GameStart();

    }
    /**传入名字获取预制 */
    public getPref(name: string): Laya.Sprite3D {
        return this.Prop.get<Laya.Sprite3D>(name);
    }
    /**传入序号获取人物预制 */
    public getRolePref(index:number):Laya.Sprite3D{
        return this.Character.get<Laya.Sprite3D>(index);
    }
    curstage:Laya.Sprite3D;

    public SetCharacterPos(){
        GameManager.STAGE = -1;

        let _level = this.mainscene.getChildByName("levelgg") as Laya.Sprite3D;
        let stage = AccountManager.Instance.curplayerData.STAGE;
        let levelname = "level"+ stage;
        for (let i = 0; i < _level.numChildren; i++) {
            let item = _level.getChildAt(i) as Laya.Sprite3D;
            if(item.name != levelname){
                item.active = false;
            }
            else{
                item.active = true;
                this.curstage = item;
            }
        }
        for (let i = 0; i < this.curstage.numChildren; i++) {// 1脚下踩的类型  2前方碰到的类型  3金币  4引导  100同伴  200终点 300Boss
            let item = this.curstage.getChildAt(i) as Laya.Sprite3D;
            if(item.name == "player"){
                this.playerpos = Laya.Sprite3D.instantiate(item);
            }
            else if(item.name == "barrier"){
                let _pref = this.getPref(ResourcesPath.Nail2)
                let _cube = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_cube);
                _cube.transform.position = item.transform.position.clone();
                _cube.transform.rotation = item.transform.rotation.clone();
                _cube.transform.scale = item.transform.scale.clone();
                _cube.name = ResourcesPath.Nail2;

                NodeUtil.SetColliderGroup(_cube,2)
            }
            else if(item.name == "role"){
                let _pref = this.getRolePref(1) as Laya.Sprite3D;
                let _player = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_player);
                _player.transform.position = item.transform.position.clone();
                _player.transform.rotation = item.transform.rotation.clone();
                let _mate = _player.addComponent(MateMove) as MateMove;
                NodeUtil.SetColliderGroup(_player,100);
            }
            else if(item.name == "speed"){
                let _pref = this.getPref(ResourcesPath.Speed)
                let _cube = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_cube);
                _cube.transform.position = item.transform.position.clone();
                _cube.transform.rotation = item.transform.rotation.clone();
                _cube.transform.scale = item.transform.scale.clone();
                _cube.name = ResourcesPath.Speed;

                NodeUtil.SetColliderGroup(item,1);
            }
            else if(item.name == "Ramp"){
                let _pref = this.getPref(ResourcesPath.Ramp)
                let _cube = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_cube);
                _cube.transform.position = item.transform.position.clone();
                _cube.transform.rotation = item.transform.rotation.clone();
                _cube.transform.scale = item.transform.scale.clone();
                _cube.name = ResourcesPath.Ramp;

                NodeUtil.SetColliderGroup(item,1)
            }
            else if(item.name == "bridge"){//down类型
                NodeUtil.SetColliderGroup(item,1)
            }
            else if( item.name == "Coin"){
                let _pref = this.getPref(ResourcesPath.Coin)
                let _cube = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_cube);
                _cube.transform.position = item.transform.position.clone();
                _cube.transform.rotation = item.transform.rotation.clone();
                _cube.transform.scale = item.transform.scale.clone();
                _cube.name = ResourcesPath.Coin;
                let coinbehav = _cube.addComponent(CoinBehav) as CoinBehav;
                NodeUtil.SetColliderGroup(_cube,3)
            }
            else if(item.name == "FinishCircle"){
                let _pref = this.getPref(ResourcesPath.FinishCircle)
                let _cube = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_cube);
                _cube.transform.position = item.transform.position.clone();
                _cube.transform.rotation = item.transform.rotation.clone();
                _cube.transform.scale = item.transform.scale.clone();
                _cube.name = ResourcesPath.FinishCircle;

                this.finishpoint = item.transform.position.clone();
                NodeUtil.SetColliderGroup(_cube,200)
            }
            else if(item.name == "BossPosition"){
                let _pref = this.getPref(ResourcesPath.Boss)
                let _cube = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_cube);
                _cube.transform.position = item.transform.position.clone();
                _cube.transform.rotation = item.transform.rotation.clone();
                _cube.transform.scale = item.transform.scale.clone();
                _cube.name = ResourcesPath.FinishCircle;

                this.finishpoint = item.transform.position.clone();
                NodeUtil.SetColliderGroup(_cube,300)
            }
            else if(item.name == "roleguide" || item.name == "rotateguide" || item.name == "roleguideend" || item.name == "rotateguideend"){
                NodeUtil.SetColliderGroup(item,4)
            }
        }
    }
}