import { AssetPath } from "../Config/AssetPath";
import { GameUtils } from "../utils/GameUtils";

export class PropManager
{
    static scene3D:Laya.Scene3D;
    static startPlane:Laya.MeshSprite3D;
    static carma:Laya.Camera;
    static mainRole:Laya.Sprite3D;
    static nowmapDict:any = {};
    static diePosY:number = 0;
    static endPlane:Laya.MeshSprite3D;

    static gamewinPosZ:number = 0;

    static patternDict:any = {};

    static npcDict:any = {};
    static npcscriptDict:any = {};
    static npcnum:number = 0;

    static iscarmanpc:boolean = false;

    static pro_jindu:fairygui.GProgressBar;

    static txt_mingci:fairygui.GTextField;

    static liziDict:any = {};

    static jiantouArr:Array<Laya.MeshSprite3D>=[];

    // static bgsprite3d:Laya.MeshSprite3D;

    private static ins:PropManager;
    constructor(){
        PropManager.ins = this;
    }

    public static get Instance():PropManager
    {
        if( PropManager.ins == null )
        {
            return new PropManager();
        }
        return PropManager.ins;
    }

    static getPosVecbyScene( sceneName:string,buildname:string ):Laya.Vector3
    {
        if( PropManager.patternDict[ sceneName ] )
        {
            return PropManager.patternDict[ sceneName ][buildname];
        }
        return null;
    }

    // /**
    //  * 判断是否爬到顶端
    //  */
    // static checkisClimbTop( sceneName:string,buildname:string, pos:Laya.Vector3 ):boolean
    // {
    //     var vec:Laya.Vector3 = PropManager.getPosVecbyScene( sceneName,buildname );
    //     if( vec && Math.abs(vec.y - pos.y) < 1.5 )
    //     {
    //         return true;
    //     }
    //     return false;
    // }

    static actionnums:number = 575;

    static actionDict:any = { 
        "climbend":{ s:550,e:575,islooping:false },
        "stand":{ s:0,e:80,islooping:true },
        "run":{ s:90,e:106,islooping:true },
        "jump":{ s:110,e:132,islooping:true },
        "kongzhong":{ s:132,e:145,islooping:true },
        "luodi1":{ s:145,e:160,islooping:true },
        "luodi2":{ s:170,e:202,islooping:true },
        "datiao1":{ s:210,e:235,islooping:true },
        "datiao2":{ s:240,e:273,islooping:true },
        "huachan":{ s:280,e:310,islooping:true },
        "kualan":{ s:310,e:346,islooping:true },
        "cefan1":{ s:350,e:391,islooping:true },
        "cefan2":{ s:400,e:434,islooping:true },
        "paoqiangleft":{ s:440,islooping:true,e:456 },
        "rightjump":{ s:460,e:477,islooping:true },
        "paoqiangright":{ s:480,e:496,islooping:true },
        "leftjump":{ s:500,e:517,islooping:true },
        "paqiang":{ s:520,e:542,islooping:true }
     };
    /**
     * @param animator 
     * @param animatorDict 
     */
    static setAnimator( animator:Laya.Animator ):void
    {
        var chiddict:any;
        for( var s in PropManager.actionDict )
        {
            if( !animator.getControllerLayer( ).getAnimatorState( s ) )
            {
                chiddict = PropManager.actionDict[s];
                //创建一个动画动作状态
                var state1 = new Laya.AnimatorState();
                //设置动作状态的名称
                state1.name = s;
                //设置动作状态播放的起始时间（起始时间与结束时间的设置为0-1的百分比数值）  要截取的时间点 / 动画的总时长
                state1.clipStart = chiddict.s/PropManager.actionnums;
                //设置动作状态播放的结束时间
                state1.clipEnd = chiddict.e/PropManager.actionnums;
                //得到默认动画赋值给Clip（getDefaultState默认动画为Unity中animation的数组顺序0下标的动画）
                state1.clip = animator.getDefaultState().clip;
                //动画播放是否循环
                state1.clip.islooping = chiddict.islooping;
                //添加动画状态到动画组件里
                animator.addState(state1);
            }
        }
    }

    public InitEffect():void
    {
        var lizi:Laya.ShuriKenParticle3D = PropManager.getlizibyName( "speedupeffect" );
        lizi.particleSystem.play();
        lizi.active = false;
        var lizi:Laya.ShuriKenParticle3D = PropManager.getlizibyName( "color_white" );
        lizi.particleSystem.play();
        lizi.active = false;
        var lizi:Laya.ShuriKenParticle3D = PropManager.getlizibyName( "color_yellow" );
        lizi.particleSystem.play();
        lizi.active = false;
    }

    private _tanselizi:Laya.ShuriKenParticle3D;
    public SetEffectColor( name:string,pos:Laya.Vector3 ):void
    {
        var closetime:number = 0;
        Laya.timer.clear( this,this.EffectEnd );
        this.EffectEndColor();
        if( name == "color_white" )
        {
            var sort:number = GameUtils.randRangeint( 0,100 );
            if( sort > 50 )
            {
                name = "color_yellow";
            }
        }
        closetime = 500;
        this._tanselizi = PropManager.getlizibyName( name );
        this._tanselizi.transform.position = pos;
        this._tanselizi.particleSystem.play();
        if( closetime != 0 )
        {
            Laya.timer.once( closetime,this,this.EffectEndColor );
        }
    }

    private EffectEndColor():void
    {
        if( this._tanselizi )
        {
            this._tanselizi.particleSystem.stop();
            this._tanselizi.active = false;
        }
    }

    private _engamelizi4:Laya.ShuriKenParticle3D;
    private _outQUaternion4:Laya.Quaternion = new Laya.Quaternion;
    public SpeedUpEffect1(name:string,pos:Laya.Vector3):void
    {
        if( !this._engamelizi4 || (this._engamelizi4 && !this._engamelizi4.active ) )
        {
            this._engamelizi4 = PropManager.getlizibyName( "speedupeffect" );
            this._engamelizi4.particleSystem.looping = true;
            this._engamelizi4.transform.scale = new Laya.Vector3( 20,20,20 );
            Laya.Quaternion.createFromAxisAngle( new Laya.Vector3( 1,0,0 ),-50,this._outQUaternion4 );
            this._engamelizi4.transform.rotation = this._outQUaternion4;
            this._engamelizi4.particleSystem.play();
        }
        var lzpos:Laya.Vector3 = pos.clone();
        lzpos.y += 5;
        lzpos.z -= 10;
        this._engamelizi4.transform.position = lzpos;
    }

    public CloseSpeedUpEffect1():void
    {
        if( this._engamelizi4 && this._engamelizi4.active )
        {
            this._engamelizi4.active = false;
        }
    }

    private _engamelizi3:Laya.ShuriKenParticle3D;
    private _outQUaternion:Laya.Quaternion = new Laya.Quaternion;
    public SpeedUpEffect(name:string,pos:Laya.Vector3):void
    {
        if( !this._engamelizi3 || (this._engamelizi3 && !this._engamelizi3.active ) )
        {
            this._engamelizi3 = PropManager.getlizibyName( "speedupeffect" );
            this._engamelizi3.particleSystem.looping = true;
            this._engamelizi3.transform.scale = new Laya.Vector3( 20,20,20 );
            Laya.Quaternion.createFromAxisAngle( new Laya.Vector3( 1,0,0 ),-50,this._outQUaternion );
            this._engamelizi3.transform.rotation = this._outQUaternion;
            this._engamelizi3.particleSystem.play();
        }
        var lzpos:Laya.Vector3 = pos.clone();
        lzpos.y += 5;
        this._engamelizi3.transform.position = lzpos;
    }

    public CloseSpeedUpEffect():void
    {
        if( this._engamelizi3 && this._engamelizi3.active )
        {
            this._engamelizi3.active = false;
        }
    }

    private _engamelizi1:Laya.ShuriKenParticle3D;
    private _engamelizi2:Laya.ShuriKenParticle3D;
    public GameOverEffecf(name:string,pos:Laya.Vector3,iswin:boolean):void
    {
        this._engamelizi1 = PropManager.getlizibyName( "fireworks" );
        this._engamelizi1.transform.position = pos;
        this._engamelizi1.particleSystem.play();
        if(iswin)
        {
            this._engamelizi2 = PropManager.getlizibyName( "winline" );
            this._engamelizi2.transform.position = pos;
            this._engamelizi2.particleSystem.play();
        }
    }

    public CloseGameOverEffect():void
    {
        if(this._engamelizi1)
        {
            this._engamelizi1.particleSystem.stop();
            this._engamelizi1.active = false;
        }
        if(this._engamelizi2)
        {
            this._engamelizi2.particleSystem.stop();
            this._engamelizi2.active = false;
        }
    }

    // public setliziPlay( name:string,pos:Laya.Vector3 ):void
    // {
    //     var closetime:number = 0;
    //     Laya.timer.clear( this,this.liziend );
    //     switch( name )
    //     {
    //         case "tanse_bai":
    //             if( name == "tanse_bai" )
    //             {
    //                 var sort:number = GameUtils.randRangeint( 0,100 );
    //                 if( sort > 50 )
    //                 {
    //                     name = "tanse_huang";
    //                 }
    //             }
    //             closetime = 500;
    //         break;
    //         case "jiasulizi":
    //         break;
    //         case "win_xian":
    //             closetime = 0;
    //         break;
    //         case "yanhua":
    //             closetime = 0;
    //         break;
    //     }
    //     var lizi:Laya.ShuriKenParticle3D = PropManager.getlizibyName( name );
    //     lizi.transform.position = pos;
    //     lizi.particleSystem.play();
    //     if( closetime != 0 )
    //     {
    //         Laya.timer.once( closetime,this,this.liziend,[lizi] );
    //     }
    // }

    private EffectEnd(lizi:Laya.ShuriKenParticle3D):void
    {
        if( lizi )
        {
            lizi.particleSystem.stop();
            lizi.active = false;
        }
    }

    static getlizibyName( name:string ):Laya.ShuriKenParticle3D
    {
        var lizisp:Laya.ShuriKenParticle3D;
        if( PropManager.liziDict[name] )
        {
            lizisp = PropManager.liziDict[name];
        }
        else
        {
            lizisp = Laya.Sprite3D.instantiate(Laya.Loader.getRes(AssetPath.get3DModel(name))) as Laya.ShuriKenParticle3D;
            PropManager.liziDict[name] = lizisp;
            PropManager.scene3D.addChild( lizisp );
        }
        lizisp.active = true;
        return lizisp;
    }
}