import {AgencyBaseControl} from "./AgencyBaseControl";
import {ResourceControl} from "../Control/ResourceControl";
import {LoadingPanel} from "../View/LoadingPanel";
import { ViewManager } from "../View/ViewManager";
import {ResDataControl} from "../Control/ResDataControl";
import {MainPanel} from "../View/MainPanel";
import {SceneControl} from "../Control/SceneControl";
import { AssetPath } from "../Config/AssetPath";
import {GameStateControl} from "../Control/GameStateControl";
import {RoleData} from "../Data/RoleData";
import { GameUtils } from "../utils/GameUtils";
import {WXGamePlatform} from "../WXSDK/WXGamePlatform";
import RoleCFG from "../CFG/RoleCFG";
import NpcCFG from "../CFG/NpcCFG";
import {LocalDataControl} from "../Control/LocalDataControl";
import { PlayerAccount, AccountUser } from "../WXSDK/PlayerAccount";
import { NetworkManager, PROTOCOLS } from "../WXSDK/NetworkManager";
import {PropManager} from "../PrefabControl/PropManager";
import { FYHD_Sdk } from "../WXSDK/FYHD_Sdk";
import GiftView from "../View/GiftView";

export class AgencyMain extends AgencyBaseControl 
{
    static key:string = "AgencyMain";
    
    private _loadjd:number = 0;
    private _alljd:number = 0;
    
    constructor() { super(); }
    
    protected initSelf():void
    {

    }

    public StartMain():void
    {
        this._loadjd = 0;
        this._alljd = 20;
        WXGamePlatform.Instance.loadSubpackage( "subpackage",this,this.sub1complete,this.loadprogress,null );
    }

    private sub1complete():void
    {
        this._loadjd = 20;
        this._alljd = 30;
        WXGamePlatform.Instance.loadSubpackage( "subpackage1",this,this.SubpackageOver,this.loadprogress,null );
    }

    /********************************** */
    scene:Laya.Scene3D;
    public onComplete() {
		//添加父级猴子
		var dude = this.scene.addChild(Laya.Loader.getRes("res/threeDimen/skinModel/dude/dude.lh")) as Laya.Sprite3D;
		//缩放
		var scale = new Laya.Vector3(0.1, 0.1, 0.1);
		dude.transform.localScale = scale;
		dude.transform.rotate(new Laya.Vector3( 0, 3.14, 0));
    }
    /*************************************** */
	

    private SubpackageOver():void
    {
        this._loadjd = 50;
        this._alljd = 40;
        var pathArr:Array<any> = [];
        var urlArr:Array<string> = [];
        urlArr.push( AssetPath.get3DModel( "startpanel" ) );
        urlArr.push( AssetPath.get3DModel( "endpanel" ) );
        urlArr.push( AssetPath.get3DModel( "speedupeffect" ) );
        urlArr.push( AssetPath.get3DModel( "color_white" ) );
        urlArr.push( AssetPath.get3DModel( "color_yellow" ) );
        urlArr.push( AssetPath.get3DModel( "winline" ) );
        urlArr.push( AssetPath.get3DModel( "fireworks" ) );
        urlArr.push( AssetPath.get3DModel( "DirectionalLight" ) );
        // urlArr.push( AssetPath.get3DModel( "distantview" ) );
        urlArr.push( AssetPath.get3DRoleModel( this.RoleData.roleCfg.name ) );
        urlArr.push( AssetPath.get3DRoleModel( "Arrow" ) );
        var arr:Array<string> = this.RoleData.levelmapArr;
        var i:number = 0;
        for( i = 0;i < arr.length;i++ )
        {
            if( urlArr.indexOf( AssetPath.get3DModel( arr[i] ) ) == -1 )
            {
                urlArr.push( AssetPath.get3DModel( arr[i] ));
            }
        }
        var npcArr:Array<any> = this.RoleData.levelcfg.npc;
        for( i = 0;i < npcArr.length;i++ )
        {
            if( urlArr.indexOf( AssetPath.get3DRoleModel( NpcCFG.dict[ npcArr[i].id ].name ) ) == -1 )
            {
                urlArr.push( AssetPath.get3DRoleModel( NpcCFG.dict[ npcArr[i].id ].name ) );
            }
        }
        for( i = 0;i < urlArr.length;i++ )
        {
            pathArr.push( { url:urlArr[i] } );
        }
        ResourceControl.Instance.loader3DArr( pathArr,this,this.load3Dcomplete,this.loadprogress );
    }

    private load3Dcomplete():void
    {
        this._loadjd = 90;
        this._alljd = 10;
        ResourceControl.Instance.loadGroup( "main",this,this.loadcomplete,this.loadprogress,false );
    }

    private loadprogress( value:number ):void
    {
        var loadingpanel:LoadingPanel = ViewManager.Instance.getView( LoadingPanel );
        if( loadingpanel )
        {
            value = this._loadjd + Math.ceil( value/100*this._alljd );
            loadingpanel.showWord( value );
        }
    }

    private loadcomplete():void
    {
        fairygui.UIConfig.buttonSound = "ui://lecxynh2iz9y26";
        GameStateControl.Instance.init();
        ResDataControl.Instance.AddViewPackByName( "main" );
        ViewManager.Instance.close( LoadingPanel );
        // FYHD_Sdk.InitSdk();
        ViewManager.Instance.add( MainPanel );
        console.log("新用户FYHD_Sdk.isnewuser---",FYHD_Sdk.isnewuser);

        let newuser = Laya.LocalStorage.getItem("is_newuser");
        if(FYHD_Sdk.isnewuser){
            Laya.timer.once(500,this,()=>{
                if(PlayerAccount.CurrentAccount.CurAccountData.WhiteUser){
                    ViewManager.Instance.add(GiftView);
                }
            })
        }
        else{
            Laya.LocalStorage.setItem("is_newuser","1");
        }
        SceneControl.Instance.SetScene();
        PropManager.Instance.InitEffect();
    }
}