import GameConfig from "./GameConfig";
import ViewManager, { ViewType } from "./game_logic/ViewManager";
import ResourceLoad from "./game_module/ResourceLoad";
import GameDefined from "./game_module/GameDefined";
import { WXSdk } from "./game_module/WXSdk";
import { FYHD_Sdk } from "./game_module/FYHD_Sdk";
import AssetController from "./game_logic/AssetController";
import GameSceneController from "./game_logic/GameSceneController";
export class Main {

	public static nowratio: number;
	public static IsWx: boolean = false;
	public static VRE = "1.0.5";
	public static CFG = "CFG";
	public static wxValue = "";
	constructor() {
		//根据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;
		//兼容微信不支持加载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;
		Main.nowratio = Laya.stage.width / Laya.stage.height;

		Main.IsWx = Laya.Browser.onWeiXin;

		if (Main.IsWx) {
			WXSdk.Instance.init((shareTicket: string, shareid: string, scene: string, appid: string) => {
				this.onShowHandler(shareTicket, shareid, scene, appid);
			});
			FYHD_Sdk.InitSdk();
		}
		//激活资源版本控制，version.json由IDE发布功能自动生成，如果没有也不影响后续流程
		Laya.ResourceVersion.enable("version.json", Laya.Handler.create(this, this.onVersionLoaded), Laya.ResourceVersion.FILENAME_VERSION);
		this.IsNeedUpdate();
	}
	private IsNeedUpdate() {
		if (!Main.IsWx) 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();
				}
			});
		});
	}

	private onShowHandler(shareTicket: string, shareId: string, scene: string, appid: string) {
		console.log("[LOG]: 分享进入: shareTicket:", shareTicket, ", openId=", shareId, ", scene=", scene);
		// if (!Main.mSharePeopleScene) {
		// 	Main.mSharePeopleScene = scene;
		// }
		// if (!Main.appid) {
		// 	Main.appid = appid;
		// }
		if (scene != null && scene != "") {
			WXSdk.Instance.ShareScene = scene;
		}
		if (shareId != null && shareId != "") {
			console.log("设置分享id" + shareId);
			WXSdk.Instance.setShareId(shareId);
		}
	}

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

	onConfigLoaded(): void {
		this.LoadUI();
	}
	LoadUI() {
		ViewManager.Instance.OpenView(ViewType.game_ui);
		GameSceneController.Instance.CreateScene();
			//this.test();
	}

	test() {
		var v1=new Laya.Vector3(-3,0,1);
		var v2=new Laya.Vector3(0,0,-1);
		var v3=new Laya.Vector3(0,0,0);
		Laya.Vector3.cross(v1,v2,v3);
		console.log(v3);
	}
}
//激活启动类
new Main();
