import { DispatcherManager } from "../utils/DispatcherManager";
import {UserInfoEvent} from "../Data/UserInfoEvent";
import {LocalDataControl} from "../Control/LocalDataControl";
import {UserDataInfo} from "../Data/UserDataInfo";
import RoleCFG from "../CFG/RoleCFG";
import LevelCFG from "../CFG/LevelCFG";
import { GameUtils } from "../utils/GameUtils";
import { PlayerAccount, AccountUser } from "../WXSDK/PlayerAccount";
import { DataInfoBase } from "./DataInfoBase";


import EventData = Laya.EventData;
	import Skeleton  = Laya.Skeleton;
	import Templet   = Laya.Templet;
	import Sprite    = Laya.Sprite;
	import Event     = Laya.Event;
	import Browser   = Laya.Browser;
	import Handler   = Laya.Handler;
	import Stat      = Laya.Stat;
	import Tween     = Laya.Tween;
    import WebGL     = Laya.WebGL;
    
export class RoleData extends DataInfoBase 
{
    private static ins:RoleData;
    /**
     * 金币
     */
    private _Gold:number=0;

    private _roleid:number = 0;
    public roleCfg:RoleCFG;

    private _leveid:number = 0;
    public levelcfg:LevelCFG;
    public levelmapArr:Array<string>;

    
    constructor() 
    { 
        super(); 
        RoleData.ins = this;
    }

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

    public init():void
    {
        let localdatacontrol:LocalDataControl = LocalDataControl.Instance;
        // var nowGold:number = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money));
        // this.Gold = nowGold;
        this.Money = localdatacontrol.getnum( UserDataInfo.gold );
        this._roleid = localdatacontrol.getnum( UserDataInfo.roleid );
        if( this.CharacterID == 0 )
        {
            this.CharacterID = 1;
        }
        else
        {
            this.InitCharacterInfo();
        }
       
        this._leveid = localdatacontrol.getnum( UserDataInfo.levelid );
        if( this._leveid == 0 )
        {
            this._leveid = 1;
        }
        this.RefreshLevel();
    }

    public get nowlevel():number
    {
        return this._leveid;
    }

    public getNowServerLevel():number
    {
        return this._leveid;
    }

    LevelUp():void
    {
        this._leveid++;
        LocalDataControl.Instance.addstr( UserDataInfo.levelid,this._leveid.toString() );
        this.RefreshLevel();
    }

    /*********************************************************** */
    public startFun():void
		{
			this.mAniPath = "res/spine/spineRes6/alien.sk";
			this.mFactory = new Templet();
			this.mFactory.on(Event.COMPLETE, this, this.parseComplete);
			this.mFactory.on(Event.ERROR, this, this.onError);
			this.mFactory.loadAni(this.mAniPath);
		}
		private onError():void
		{
			console.log("error");
		}
		
		private parseComplete():void {
			//创建模式为1，可以启用换装
			this.mArmature = this.mFactory.buildArmature(1);
			this.mArmature.x = this.mStartX;
			this.mArmature.y = this.mStartY;
			this.mArmature.scale(0.5, 0.5);
			Laya.stage.addChild(this.mArmature);
			this.mArmature.on(Event.LABEL, this, this.onEvent);
			this.play();
        }
/******************************************************************** */

    private RefreshLevel():void
    {
        var lv = this._leveid;
        var nowcfglv:number = 0;
        var allnums:number = LevelCFG.vec.length;
        if( lv > allnums )
        {
            var value:number = (lv-allnums)%( allnums-2 );
            if(  value == 0 )
            {
                nowcfglv = allnums;
            }
            else
            {
                nowcfglv = value + 2;
            }
        }
        else
        {
            nowcfglv = lv;
        }
        this.levelcfg = LevelCFG.dict[ nowcfglv ];
        this.levelmapArr = GameUtils.cutstrtoArr(this.levelcfg.road);
        DispatcherManager.dispatchEvent( UserInfoEvent.userinfoEvent_levelid );
    }

    /****************************************************** */
        private mAniPath:string;
		private mStartX:number = 400;
		private mStartY:number = 500;
		private mFactory:Templet;
		private mActionIndex:number = 0;
		private mCurrIndex:number = 0;
		private mArmature:Skeleton;
		private mCurrSkinIndex:number = 0;
		private mFactory2:Templet;
		private mLabelSprite:Sprite;

    private play():void
		{
			this.mCurrIndex++;
			if (this.mCurrIndex >= this.mArmature.getAnimNum())
			{
				this.mCurrIndex = 0;
			}
			this.mArmature.play(this.mCurrIndex,false);
			
		}
		
		private onEvent(e):void
		{
			Laya.stage.addChild(this.mLabelSprite);
			this.mLabelSprite.x = this.mStartX;
			this.mLabelSprite.y = this.mStartY;
			this.mLabelSprite.graphics.clear();
        }
        
/****************************************************** */



    movespeed:number = 0;
    frontjump:Laya.Vector3 = new Laya.Vector3;
    leftjump:Laya.Vector3 = new Laya.Vector3;
    rightjump:Laya.Vector3 = new Laya.Vector3;
    upclimb:number = 0;
    slideclimb:Laya.Vector3 = new Laya.Vector3;
    private InitCharacterInfo():void
    {
        this.roleCfg = RoleCFG.dict[ this._roleid ];
        this.movespeed = this.roleCfg.speed;
        
        this.frontjump = this.roleCfg.frontjump;
        this.leftjump = this.roleCfg.leftjump;
        this.rightjump = this.roleCfg.rightjump;
        this.upclimb = Number( this.roleCfg.upclimb );
        this.slideclimb = this.roleCfg.slideclimb;

        // this.frontjump.x = Number( this.roleCfg.frontjump.x );
        // this.frontjump.y = Number( this.roleCfg.frontjump.y );
        // this.frontjump.z = Number( this.roleCfg.frontjump.z );
        
        // this.leftjump.x = Number( this.roleCfg.leftjump.x );
        // this.leftjump.y = Number( this.roleCfg.leftjump.y );
        // this.leftjump.z = Number( this.roleCfg.leftjump.z );
        
        // this.rightjump.x = Number( this.roleCfg.rightjump.x );
        // this.rightjump.y = Number( this.roleCfg.rightjump.y );
        // this.rightjump.z = Number( this.roleCfg.rightjump.z );

        // this.slideclimb.x = Number( this.roleCfg.slideclimb.x );
        // this.slideclimb.y = Number( this.roleCfg.slideclimb.y );
        // this.slideclimb.z = Number( this.roleCfg.slideclimb.z );
    }

    // public set leveid( value:number )
    // {
    //     if( this._leveid != value )
    //     {
    //         this._leveid = value;
    //         this.levelcfg = LevelCFG.dict[ this._leveid ];
    //         this.levelmapArr = GameUtils.cutstrtoArr(this.levelcfg.road);
    //         CacheManager.getInstance().addstr( UserDataInfo.levelid,this._leveid.toString() );
    //         DispatcherManager.dispatchEvent( UserInfoEvent.userinfoEvent_updateroleid );
    //     }
    // }

    public get LevelID():number
    {
        return this._leveid;
    }

    public set CharacterID( value:number )
    {
        if( this._roleid != value )
        {
            this._roleid = value;
            this.InitCharacterInfo();
            LocalDataControl.Instance.addstr( UserDataInfo.roleid,this._roleid.toString() );
            DispatcherManager.dispatchEvent( UserInfoEvent.userinfoEvent_updateroleid );
        }
    }

    public get CharacterID():number
    {
        return this._roleid;
    }

    public set Money( value:number )
    {
        if( this._Gold != value )
        {
            this._Gold = value;
            LocalDataControl.Instance.addstr( UserDataInfo.gold,this._Gold.toString() );
            DispatcherManager.dispatchEvent( UserInfoEvent.UserInfoEvent_updateUserData );
        }
    }

    public get Money():number
    {
        return this._Gold;
    }

}