import {AgencyBaseControl} from "./AgencyBaseControl";
import {LocalDataControl} from "../Control/LocalDataControl";
import {UserDataInfo} from "../Data/UserDataInfo";
import {SkinData} from "../Data/SkinData";
import RoleCFG from "../CFG/RoleCFG";
import { ViewManager } from "../View/ViewManager";
import {SkinPanel} from "../View/SkinPanel";
import { GlobalTip } from "../utils/GlobalTip";
import {GameState} from "../utils/GameState";
import { PlayerAccount, AccountUser } from "../WXSDK/PlayerAccount";
import { NetworkManager, PROTOCOLS } from "../WXSDK/NetworkManager";
import {AgencyUserMessage} from "./AgencyUserMessage";

export class AgencySkin extends AgencyBaseControl 
{
    static key:string = "AgencySkin";
    static instance:AgencySkin;
    
    localdata:any;
    pifuArr:Array<SkinData>;
    pifuDict:any;

    constructor() 
    { 
        super(); 
        AgencySkin.instance = this;

    }

    protected initSelf():void
    {

    }

    public init():void
    {
        this.localdata = LocalDataControl.Instance.GetLocalCaCheData( UserDataInfo.skin );
        var pifuData:any;
        if( !this.localdata.hasOwnProperty( "pifu" ) )
        {
            this.localdata["pifu"] = {};
        }
        pifuData = this.localdata["pifu"];
        this.pifuArr = [];
        this.pifuDict = {};
        var i:number = 0;
        var nums:number = RoleCFG.vec.length;
        for( i = 0;i < nums;i++ )
        {
            var pifuvo:SkinData = new SkinData;
            var cfg:RoleCFG = RoleCFG.vec[i];
            pifuvo.roleCFG = cfg;
            var d:any = pifuData[ cfg.id ];
            if( d )
            {
                pifuvo.state = Number( d.state );
                pifuvo.hasnum = Number( d.hasnum );
            }
            else
            {
                pifuvo.state = 0;
                pifuvo.hasnum = 0;
            }
            if( cfg.getState == 0 )
            {
                pifuvo.state = 1;
            }
            this.pifuArr.push( pifuvo );
            this.pifuDict[ pifuvo.roleCFG.id ] = pifuvo;
        }

    }

    public openview():void
    {
        ViewManager.Instance.add( SkinPanel );
    }

    public setRoleState( cfg:RoleCFG,value:number ):void
    {
        var pifuvo:SkinData = this.pifuDict[ cfg.id ];
        if( pifuvo )
        {
            pifuvo.state = 1;
            this.AddLocalSkinData(pifuvo.roleCFG.id,pifuvo.state,pifuvo.hasnum);
        }
    }

    public  AddLocalSkinData( id:any,state:number,hasnum:number ):void
    {
        this.localdata["pifu"][id] = {};
        this.localdata["pifu"][id]["state"] = state;
        this.localdata["pifu"][id]["hasnum"] = hasnum;
        LocalDataControl.Instance.AddData( UserDataInfo.skin,this.localdata );
    }

    /****************************************** */
    private rect:Laya.Sprite;

		private createCantralRect(): void {
			this.rect = new Laya.Sprite();
			this.rect.graphics.drawRect(-100, -100, 200, 200, "gray");
			Laya.stage.addChild(this.rect);

			this.updateRectPos();
		}

		private updateRectPos(): void {
			this.rect.x = Laya.stage.width / 2;
			this.rect.y = Laya.stage.height / 2;
		}
    /****************************************** */

    public GainPifu( _skinitem:SkinData ):void
    {
        if( _skinitem.state == 1 )
        {
            this.RoleData.CharacterID = _skinitem.roleCFG.id;
            this.DisTrigger( GameState.updateSKinView );
        }
        else
        {
            switch( _skinitem.roleCFG.getState )
            {
                case 1://视频获取
                _skinitem.hasnum++;
                    if( _skinitem.roleCFG.need <= _skinitem.hasnum )
                    {
                        _skinitem.state = 1;
                    }
                    this.AddLocalSkinData(_skinitem.roleCFG.id,_skinitem.state,_skinitem.hasnum);
                    this.DisTrigger( GameState.updateSKinView );
                break;
                case 2://金币购买
                    if( this.RoleData.Money >= _skinitem.roleCFG.need )
                    {
                        this.RoleData.Money -= _skinitem.roleCFG.need;
                        _skinitem.state = 1;
                        this.AddLocalSkinData(_skinitem.roleCFG.id,_skinitem.state,_skinitem.hasnum);
                        this.DisTrigger( GameState.updateSKinView );
                        GlobalTip.Instance.showWordTips( "购买成功" );
                    }
                    else
                    {
                        GlobalTip.Instance.showWordTips("金币不足");
                    }
                break;
                case 3://活动获取
                break;
            }
        }
    }

    

    
    public GainNoHaveSkin():Array<SkinData>
    {
        var arr:Array<SkinData> = [];
        for( var s in this.pifuDict )
        {
            if( (this.pifuDict[s] as SkinData).state == 0 )
            {
                arr.push( this.pifuDict[s] );
            }
        }
        return arr;
    }
}