import { Global } from "./Global";
import {PanelResDic} from "./PrefabControl/PanelResDic";
import { DispatcherManager } from "./utils/DispatcherManager";

import {ComponentEventData} from "./Data/ComponentEventData";
import {RecConfigs} from "./Config/RecConfigs";
import { TimesTamper } from "./Time/TimesTamper";
import { AssetPath } from "./Config/AssetPath";
import {TimerService} from "./Time/TimerService";
import {GlobalWaiting} from "./GlobalWaiting";
import {PanelBaseControl} from "./Control/PanelBaseControl";
import {GamePool} from "./PrefabControl/GamePool";
import { LayoutControl } from "./Control/LayoutControl";
import { ViewManager } from "./View/ViewManager";
import {ResourceControl} from "./Control/ResourceControl";
import {ResDataControl} from "./Control/ResDataControl";
import {AudioControl} from "./Control/AudioControl";
import { ConnectControl } from "./Control/ConnectControl";
import {pingtaiutils} from "./WXSDK/pingtaiutils";
import {AgencyLogin} from "./Agency/AgencyLogin";
import {AgencyUserMessage} from "./Agency/AgencyUserMessage";
import {AgencyMain} from "./Agency/AgencyMain";
import {AgencyInGame} from "./Agency/AgencyInGame";
import {AgencySkin} from "./Agency/AgencySkin";
import {AgencyTrial} from "./Agency/AgencyTrial";
import {AgencyLottery} from "./Agency/AgencyLottery";

/*
* name;
*/
export class GameContents extends Laya.EventDispatcher
{
    constructor(){
        super();
    }
    /**
     * 初始化游戏
     */
    public initGame():void
    {
        RecConfigs.Instance.init();
        DispatcherManager.init();
        TimerService.Instance.init();
        LayoutControl.Instance.initlayer();
        ViewManager.Instance.init();
        ResourceControl.Instance.init();
        ResDataControl.Instance.init();
        AudioControl.Instance.init();
        ConnectControl.Connnect();
        TimesTamper.Instance.init();
        pingtaiutils.Instance.init();
        this.initPoolObject();
        this.initProxy();
        this.initmapView();
        this.beginLoadHandler();
    }

    /**
     * 初始化对象池
     */
    private initPoolObject():void
    {
        GamePool.Mark( ComponentEventData.ResDicValue,ComponentEventData );
    }

    /**
     * 初始化模块代理类
     */
    private initProxy():void
    {
        var mvcpool:PanelResDic = PanelResDic.Instance;
        mvcpool.AgencyAdd( AgencyLogin );
        mvcpool.AgencyAdd( AgencyUserMessage );
        mvcpool.AgencyAdd( AgencyMain );
        mvcpool.AgencyAdd( AgencyInGame );
        mvcpool.AgencyAdd( AgencySkin );
        mvcpool.AgencyAdd(AgencyTrial );
        mvcpool.AgencyAdd( AgencyLottery );
    }

    private initmapView():void
    {
        var mvcpool:PanelResDic = PanelResDic.Instance;
        // mvcpool.mapView( MapViewType.DailyrewardView,DailyrewardView );
    }

    private beginLoadHandler():void
    {
        var patharr:Array<any> = [];
        patharr.push( { url:AssetPath.getResConfig(), type: Laya.Loader.JSON } );
        patharr.push( { url:AssetPath.getGameVersion(), type: Laya.Loader.JSON } );
        ResourceControl.Instance.Loaders( patharr,this,this.recloadComplete );
    }

    private recloadComplete():void
    {
        var gameversion:any = ResDataControl.Instance.GainResources( AssetPath.getGameVersion(),false );
        if( gameversion )
        {
            Global.gameVersion = gameversion.version;
        }
        else
        {
            Global.gameVersion = "1.0.0";
        }
        var configres:any = ResDataControl.Instance.GainResources( AssetPath.getResConfig(),false );
        RecConfigs.Instance.initRec( configres );
        ResourceControl.Instance.loadGroup("loading",this,this.onPreLoaded,this.onPreprogress );
    }

    private onPreprogress(value:number):void
    {
        
    }

    private onPreLoaded():void
    {
        ResDataControl.Instance.AddViewPackByName( "loading",true,false );
        var globalwaiturl:string = fairygui.UIPackage.getItemURL("loading","globalWaiting");
        PanelBaseControl.viewbgUrl = fairygui.UIPackage.getItemURL("loading","img_black");
        fairygui.UIObjectFactory.setPackageItemExtension(globalwaiturl, GlobalWaiting);
        fairygui.UIConfig.globalModalWaiting = globalwaiturl;
        fairygui.UIConfig.windowModalWaiting = globalwaiturl;
        // CPAUtils.init();
        this.Login();
    }

    public Login():void
    {
        (PanelResDic.Instance.getSingleTon( AgencyLogin ) as AgencyLogin).Login();
    }
}