import { ViewManager } from "../View/ViewManager";
import {LoadingPanel} from "../View/LoadingPanel";

import {AgencyUserMessage} from "./AgencyUserMessage";
import {WECHATEVENT} from "../WXSDK/WECHATEVENT";
import {AgencyBaseControl} from "./AgencyBaseControl";
import {AgencyMain} from "./AgencyMain";
import RoleCFG from "../CFG/RoleCFG";
import LevelCFG from "../CFG/LevelCFG";
import BuildCFG from "../CFG/BuildCFG";
import NpcCFG from "../CFG/NpcCFG";
import GameCFG from "../CFG/GameCFG";
import {AgencySkin} from "./AgencySkin";
import { WxApi } from "../WXSDK/Sdk";
import { PlayerAccount } from "../WXSDK/PlayerAccount";
import { NetworkManager } from "../WXSDK/NetworkManager";
import { MainConfig } from "../MainConfig";
import { Global } from "../Global";
import LotteryCFG from "../CFG/LotteryCFG";
import {AgencyLottery} from "./AgencyLottery";
import { FYHD_Sdk } from "../WXSDK/FYHD_Sdk";
import { ShareUtil } from "../WXSDK/ShareUtil";
import { GuangGao } from "../WXSDK/GuangGao";
import { ttad } from "../WXSDK/ttad";

export class AgencyLogin extends AgencyBaseControl 
{
    static key:string = "AgencyLogin";
    
    constructor() { super(); }
    
    protected initSelf():void
    {
        this.DisOn( WECHATEVENT.WECHATEVENT_loginSucceed,this,this.Success );
    }

    public Login():void
    {
        ViewManager.Instance.add( LoadingPanel );
        this.loginprogress(0);
        // if(Laya.Browser.onWeiXin)
        // {
        //     this.LoginWX();
        // }
        // else
        // {
            this.LoginLocal();
        // }
    }
    LoginWX()
    {
        WxApi.recordAldEvent("开始微信登录");
        WxApi.WXLogin((res) => 
            {
                if (res != "error") 
                {
                    WxApi.recordAldEvent("微信登录成功");
                    console.log("hplogin:" + "querychannel->"+ WxApi.querychannel + " referrerInfoappId->" + WxApi.referrerInfoappId);
                    this.LoginLocal();
                }
                else 
                {
                    WxApi.recordAldEvent("微信登录失败");
                    this.LoginWX();
                }
            });
    }

    LoginLocal(res = null) 
    {
        WxApi.recordAldEvent("开始本地登录");
        if(res)
        {
            new PlayerAccount(res.code, "", "", 0, "", "", "", "", WxApi.shareOpenId, WxApi.shareOpenId,Global.gameVersion);
        }
        NetworkManager.Instance.login(null, Laya.Handler.create(this, this.LoginComplete), null);
        //this.LoadSbp();
    }

    LoginComplete() 
    {
        WxApi.recordAldEvent("本地服务器登录完成...");
        
        // ShareUtil.Instance.onRegister();
        // GuangGao.InitVideo();
        WxApi.aldSendOpenid(PlayerAccount.CurrentAccount.CurAccountData.openid);
        this.Success();
    }

    private loginprogress( value:number ):void
    {
        var loadingPanel:LoadingPanel = ViewManager.Instance.getView( LoadingPanel );
        if( loadingPanel )
        {
            loadingPanel.showWord( value );
        }
    }

    private Success():void
    {
        this.ResolveData( );
        this.initData();
        this.StartMain();
        ttad.InitAD();
    }

    private StartMain():void
    {
        console.log( "当前配置表总关卡：",LevelCFG.vec.length );
        
        ( this.getSingleTon( AgencyMain ) as AgencyMain ).StartMain();
    }

    public static configClass:any =
    {
        RoleCFG:RoleCFG,
        LevelCFG:LevelCFG,
        BuildCFG:BuildCFG,
        NpcCFG:NpcCFG,
        GameCFG:GameCFG,
        LotteryCFG:LotteryCFG
    }

    /**
     * 解析配置数据
     */
    private ResolveData():boolean 
    {
        let byteData:any = PlayerAccount.CurrentAccount.nowConfig.gameConfig[0].config;
        if( byteData && byteData != "" )
        {
            var arr:Array<string> = byteData.split("%file%");
            var i:number;
            var nums:number = arr.length;
            for( i=0;i<nums;i++ )
            {
                var itemarr:Array<string> = arr[i].split("%cfg%");
                var cla: any = AgencyLogin.configClass[itemarr[0]];
                if (cla) {
                    cla.initCfg(itemarr[1]);
                }
                else {
                    
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 初始化游戏数据
     */
    private initData():void
    {
        ( this.getSingleTon( AgencyUserMessage ) as AgencyUserMessage ).init();
        ( this.getSingleTon( AgencySkin ) as AgencySkin ).init();
        ( this.getSingleTon( AgencyLottery ) as AgencyLottery ).init();
    }

   
}