import  LoadView  from "./View/LoadView";
import { CheckMain } from "./Tools/CheckMain";
import { WXSDK } from "./Net/WXSDK";
import { AccountManager } from "./Net/AccountManager";
import { GameTools } from "./Tools/GameTools";
import GameConfig from "./GameConfig";
export class Main {
	static Ver = "1.0.0" ;
	static CFG = "123" ;
	static SceneNum;
	
	constructor() {
		if(CheckMain.Creat)return
		CheckMain.Creat = true;
		//根据IDE设置初始化引擎		
		if (window["Laya3D"]) Laya3D.init(GameConfig.width, GameConfig.height);
		else Laya.init(GameConfig.width, GameConfig.height, Laya["WebGL"]);
		Laya["Physics"] && Laya["Physics"].enable();
		Laya["DebugPanel"] && Laya["DebugPanel"].enable();
		Laya.stage.scaleMode = GameConfig.scaleMode;
		Laya.stage.screenMode = GameConfig.screenMode;
		Laya.stage.alignV = GameConfig.alignV;
		Laya.stage.alignH = GameConfig.alignH;
		//兼容微信不支持加载scene后缀场景
		Laya.URL.exportSceneToJson = GameConfig.exportSceneToJson;
		
		//打开调试面板（通过IDE设置调试模式，或者url地址增加debug=true参数，均可打开调试面板）
		if (GameConfig.debug || Laya.Utils.getQueryString("debug") == "true") Laya.enableDebugPanel();
		if (GameConfig.physicsDebug && Laya["PhysicsDebugDraw"]) Laya["PhysicsDebugDraw"].enable();
		if (GameConfig.stat) Laya.Stat.show();
		Laya.alertGlobalError = true;

		if(Laya.Browser.onWeiXin){
			Laya.Browser.window.wx.showShareMenu({
				withShareTicket: true
			});
			Laya.Browser.window.wx.onShareAppMessage(function () {
				// var share = AccountManager.Instance.curplayerData.ShareContent;
				// var message = share.split(";");
				// var sharemessage = message[GameTools.RandomANumber(0,message.length)];
				return {
					title:"有人@你，一起骑马仗箭走天涯" ,
					imageUrl: "Game/res/textrue/share.jpg",
				}
			});
			Laya.Browser.window.wx.onShow((option) => {
				var shareTicket = option.shareTicket;
				var userId = option.query.userId;
				var shareTitle = option.query.shareTitle;
				
				var scenevalue = Laya.LocalStorage.getItem("SceneValue");
				if(scenevalue){
					Main.SceneNum = scenevalue;
				}
				else{
					Laya.LocalStorage.setItem("SceneValue",option.scene);
					Main.SceneNum = option.scene;
				}

				WXSDK.querychannel = option.query.channel;
				WXSDK.shareOpenId = option.query.userId;
				console.log("scene:", option.scene);
				if (WXSDK.querychannel != null){
					console.log("onShow channel is:", WXSDK.querychannel);
				}
				if(option.referrerInfo)
				{
					WXSDK.referrerInfoappId = option.referrerInfo.appId;
					console.log("onShow appId is:", option.referrerInfo.appId);
				}
			});

			

		if (!Laya.Browser.onWeiXin) return;
        const updateManager = Laya.Browser.window.wx.getUpdateManager();
			updateManager.onCheckForUpdate(function (res) {
				console.log("最新版本:" + res.hasUpdate);
			}
			);
			updateManager.onUpdateReady(function () {
				Laya.Browser.window.wx.showModal({
					showCancel: false,
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						updateManager.applyUpdate();
					},
					fail(res?) {
						updateManager.applyUpdate();
					}
				});
			});

		}

		//激活资源版本控制，version.json由IDE发布功能自动生成，如果没有也不影响后续流程
		Laya.ResourceVersion.enable("version.json", Laya.Handler.create(this, this.onVersionLoaded), Laya.ResourceVersion.FILENAME_VERSION);
	}

	onVersionLoaded(): void {
		//激活大小图映射，加载小图的时候，如果发现小图在大图合集里面，则优先加载大图合集，而不是小图
		Laya.AtlasInfoManager.enable("fileconfig.json", Laya.Handler.create(this, this.onConfigLoaded));
	}

	onConfigLoaded(): void {
		//加载IDE指定的场景
		// GameConfig.startScene && Laya.Scene.open(GameConfig.startScene);
		new LoadView();
	}
}
//激活启动类
new Main();
