
export class BJ_Casino_Deeplink {

	//#region public

	public Servertype: any[] = [5, ["不選擇版本", "Web", "Out", "Submit", "Internal_release", "Internal_Dev"]];
	public Hostport: any[] = [1, ["不選擇環境", "內版", "肉乾9006", "肉乾9007", "肉乾9008", "肉乾9009", "賴宏", "40", "送審", "外版", "testing1", "testing2", "testing3"]];
	public Patch: any[] = [1, [
		"不選擇資源路徑",
		"內版",
		"B2B",
		"送審",
		"外版",
		"alex",
		"bowei",
		"建喵-內版",
		"建喵-B2B",
		"建喵-送審",
		"建喵-外版",
		"打包機-內版",
		"打包機-B2B",
		"打包機-送審",
		"打包機-外版",
	]];

	//#endregion

	//#region private

	private _client: any;

	private _u = navigator.userAgent;
	private _isAndroid = this._u.indexOf("Android") > -1 || this._u.indexOf("Adr") > -1;
	private _isiOS = !!this._u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

	//#endregion

	//#region Lifecycle

	/**
	 *
	 */
	constructor(client: any) {
		this._client = client;
		this.onLoad();
	}

	public onLoad() {
		this._client.servertype.value = this.Servertype[0];
		this._client.hostport.value = this.Hostport[0];
		this._client.patch.value = this.Patch[0];
		this._client.servertypeDefault.value = this.Servertype[1][this._client.servertype.value];
		this._client.hostportDefault.value = this.Hostport[1][this._client.hostport.value];
		this._client.patchDefault.value = this.Patch[1][this._client.patch.value];
		// this.GetLanIP();

		if (!this._isAndroid && !this._isiOS) {
			this._client.isBtn_WanOpen.value = true;
		}
	}

	//#endregion

	//#region Get Data

	public SelectServertype(data: any) {
		// data为el-option上:value绑定的对象
		this._client.servertype.value = data;
	}

	public SelectHostport(data: any) {
		// data为el-option上:value绑定的对象
		this._client.hostport.value = data;
	}

	public SelectPatch(data: any) {
		// data为el-option上:value绑定的对象
		this._client.patch.value = data;
	}

	//#endregion

	//#region Get Data

	public GetServerType(): { key: string; value: string; label: string; }[] {
		const servertypes: any[] = this.Servertype[1];
		let options: any[] = [];
		for (let i: number = 0; i < servertypes.length; i++) {
			const servertype = servertypes[i];
			options.push({
				value: i,
				label: servertype,
			});
		}
		return options;
	}

	public GetHostport(): { key: string; value: string; label: string; }[] {
		const hostports: any[] = this.Hostport[1];
		let options: any[] = [];
		for (let i: number = 0; i < hostports.length; i++) {
			const hostport = hostports[i];
			options.push({
				value: i,
				label: hostport,
			});
		}
		return options;
	}

	public GetPatch(): { key: string; value: string; label: string; }[] {
		const patchs: any[] = this.Patch[1];
		let options: any[] = [];
		for (let i: number = 0; i < patchs.length; i++) {
			const patch = patchs[i];
			options.push({
				value: i,
				label: patch,
			});
		}
		return options;
	}

	// public async GetLanIP(): Promise<string> {
	// 	let run: boolean = true;
	// 	let ip: string = "";
	// 	// var url: string = "http://192.168.5.36/page/Net/";
	// 	var url: string = "http://220.134.195.1/public/bonus_casino/html5/jianmiau/Net/";
	// 	var xhr: XMLHttpRequest = new XMLHttpRequest();
	// 	xhr.onreadystatechange = function (): void {
	// 		if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
	// 			ip = xhr.responseText;
	// 			run = false;
	// 		}
	// 	};
	// 	xhr.open("GET", url);
	// 	xhr.timeout = 500; // 超时时间，单位是毫秒
	// 	xhr.ontimeout = function (e) {
	// 		// XMLHttpRequest 超时。在此做某事。
	// 		run = false;
	// 	};
	// 	xhr.send();
	// 	while (run) {
	// 		await Tools.Sleep(1000);
	// 	}
	// 	if (ip !== "") {
	// 		this._isWan = false;
	// 	}
	// 	return ip;
	// }

	//#endregion

	//#region Custom

	public Install_Debug() {
		if (this._isAndroid) {
			window.open("https://dl.dropboxusercontent.com/s/aoh4p9vs1lrofy2/BJ_Casino-debug.apk?v=" + Date.now(), "_self");
		} else if (this._isiOS) {
			window.open("itms-services://?action=download-manifest&url=https://dl.dropboxusercontent.com/s/z390yysswqiiii4/BJ_Casino-debug.plist", "_self");
		} else {
			window.open("https://dl.dropboxusercontent.com/s/aoh4p9vs1lrofy2/BJ_Casino-debug.apk?v=" + Date.now(), "_blank");
			window.open("https://dl.dropboxusercontent.com/s/4ki0rglk4w01ian/BJ_Casino-debug.ipa", "_blank");
		}
	}

	public Install_Release() {
		if (this._isAndroid) {
			window.open("https://dl.dropboxusercontent.com/s/6mjrw913u6ma5w8/BJ_Casino-release.apk?v=" + Date.now(), "_self");
		} else if (this._isiOS) {
			window.open("itms-services://?action=download-manifest&url=https://dl.dropboxusercontent.com/s/9qckj6t66jgb6rw/BJ_Casino-release.plist", "_self");
		} else {
			window.open("https://dl.dropboxusercontent.com/s/6mjrw913u6ma5w8/BJ_Casino-release.apk?v=" + Date.now(), "_blank");
			window.open("https://dl.dropboxusercontent.com/s/hyyswyun3m42qbk/BJ_Casino-release.ipa?v=" + Date.now(), "_blank");
		}
	}

	public Install_Release_Debug() {
		if (this._isAndroid) {
			window.open("https://dl.dropboxusercontent.com/s/16m2b5sncymj6f9/BJ_Casino-release-debug.apk?v=" + Date.now(), "_self");
		} else if (this._isiOS) {
			window.open("itms-services://?action=download-manifest&url=https://dl.dropboxusercontent.com/s/wixnsxbc7ctk87f/BJ_Casino-release-debug.plist", "_self");
		} else {
			window.open("https://dl.dropboxusercontent.com/s/16m2b5sncymj6f9/BJ_Casino-release-debug.apk?v=" + Date.now(), "_blank");
			window.open("https://dl.dropboxusercontent.com/s/f6qjm1gk8jjvmhx/BJ_Casino-release-debug.ipa?v=" + Date.now(), "_blank");
		}
	}

	public Install_B2B() {
		if (this._isAndroid) {
			window.open("https://dl.dropboxusercontent.com/s/g66xrcdfj9e093i/BJ_Casino-B2B.apk?v=" + Date.now(), "_self");
		} else if (this._isiOS) {
			window.open("itms-services://?action=download-manifest&url=https://dl.dropboxusercontent.com/s/rrs1ev3llogrm0a/BJ_Casino-B2B.plist", "_self");
		} else {
			window.open("https://dl.dropboxusercontent.com/s/g66xrcdfj9e093i/BJ_Casino-B2B.apk?v=" + Date.now(), "_blank");
			window.open("https://dl.dropboxusercontent.com/s/fm79uc9ygrnzone/BJ_Casino-B2B.ipa?v=" + Date.now(), "_blank");
		}
	}

	public Install_JM() {
		if (this._isAndroid) {
			window.open("https://dl.dropboxusercontent.com/s/9zlj6og4octmim8/BJ_Casino-JM.apk?v=" + Date.now(), "_self");
		} else if (this._isiOS) {
			window.open("itms-services://?action=download-manifest&url=https://dl.dropboxusercontent.com/s/23mx7l7cchbvjcv/BJ_Casino-JM.plist", "_self");
		} else {
			window.open("https://dl.dropboxusercontent.com/s/9zlj6og4octmim8/BJ_Casino-JM.apk?v=" + Date.now(), "_blank");
			window.open("https://dl.dropboxusercontent.com/s/dwlil2rbfe1wi13/BJ_Casino-JM.ipa?v=" + Date.now(), "_blank");
		}
	}

	public Install_Store() {
		if (this._isAndroid) {
			window.open("https://play.google.com/store/apps/details?id=org.vicking.bj_casino", "_self");
		} else if (this._isiOS) {
			window.open("https://apps.apple.com/tw/app/爆機娛樂城/id1593328573", "_self");
		} else {
			window.open("https://play.google.com/store/apps/details?id=org.vicking.bj_casino", "_blank");
			window.open("https://apps.apple.com/tw/app/爆機娛樂城/id1593328573", "_blank");
		}
	}

	public Install_Bowei() {
		if (this._isAndroid) {
			window.open("http://220.134.195.1/public/bonus_casino/html5/bowei/BJ_Casino-debug.apk?v=" + Date.now(), "_self");
		} else if (this._isiOS) {
			alert("iOS不提供");
		} else {
			window.open("http://220.134.195.1/public/bonus_casino/html5/bowei/BJ_Casino-debug.apk?v=" + Date.now(), "_blank");
		}
	}

	public Install_InternalTest(InternalTest: number): void {
		// let InternalTest = document.getElementById("InternalTest").value;
		// let InternalTest = "";
		if (this._isAndroid && InternalTest) {
			window.open(`https://play.google.com/apps/test/org.vicking.bj_casino/${InternalTest}`, "_self");
		} else if (this._isiOS) {
			alert("iOS不提供");
		} else {
			window.open(`https://play.google.com/apps/test/org.vicking.bj_casino/${InternalTest}`, "_blank");
		}
	}

	public OnclickWanOpen() {
		this._client.PopupVisible.value = true;
		// this.OnclickOpen(true);
	}

	public OnclickOpen(isWan: boolean = false, obj: Object = {}) {
		let hostport = this._client.hostport.value;
		let servertype = +this._client.servertype.value;
		let patch = +this._client.patch.value;
		let E_iscustom: any = document.getElementById("iscustom");
		let iscustom = E_iscustom.checked;
		let URLscheme: any = {};

		if (obj["deviceid"]) {
			URLscheme["deviceid"] = obj["deviceid"];
		}

		switch (servertype) {
			case 0: {
				break;
			}
			default: {
				URLscheme["servertype"] = servertype;
				break;
			}
		}

		switch (hostport) {

			// 不選擇環境
			case 0: {
				break;
			}

			// 外網連內版
			case 1: {
				URLscheme["host"] = "http://220.134.195.1";
				URLscheme["port"] = "19005";
				break;
			}

			// 肉乾9006
			case 2: {
				URLscheme["host"] = "http://192.168.5.230";
				URLscheme["port"] = "9006";
				break;
			}

			// 肉乾9007
			case 3: {
				URLscheme["host"] = "http://192.168.5.230";
				URLscheme["port"] = "9007";
				break;
			}

			// 肉乾9008
			case 4: {
				URLscheme["host"] = "http://192.168.5.230";
				URLscheme["port"] = "9008";
				break;
			}

			// 肉乾9009
			case 5: {
				URLscheme["host"] = "http://192.168.5.230";
				URLscheme["port"] = "9009";
				break;
			}

			// 賴宏
			case 6: {
				URLscheme["host"] = "http://192.168.5.134";
				URLscheme["port"] = "9005";
				break;
			}

			// 40
			case 7: {
				URLscheme["host"] = "http://192.168.5.12";
				URLscheme["port"] = "9487";
				break;
			}

			// 送審
			case 8: {
				URLscheme["host"] = "https://submit.online-bj.com";
				URLscheme["port"] = "9005";
				break;
			}

			// 外版
			case 9: {
				URLscheme["host"] = "https://game.online-bj.com";
				URLscheme["port"] = "9005";
				break;
			}

			// testing1
			case 10: {
				URLscheme["host"] = "http://bj-testing1.casino.catan.com.tw";
				URLscheme["port"] = "9005";
				break;
			}

			// testing2
			case 11: {
				URLscheme["host"] = "http://bj-testing2.casino.catan.com.tw";
				URLscheme["port"] = "9005";
				break;
			}

			// testing3
			case 12: {
				URLscheme["host"] = "http://bj-testing3.casino.catan.com.tw";
				URLscheme["port"] = "9005";
				break;
			}

			default: {
				break;
			}
		}

		switch (patch) {
			//   "https://patch.online-bj.com/game/",
			//   "https://patch-submit.online-bj.com/game/",
			//   "http://220.134.195.1/public/bonus_casino/html5/alex/APPBundleTest/",
			//   "http://220.134.195.1/public/bonus_casino/html5/jianmiau/Bundle/",
			//   "http://patch-release.online-bj.com/",
			//   "http://patch-dev.online-bj.com/",

			case 0: {
				break;
			}

			// Dev
			case 1: {
				URLscheme["patch"] = "http://patch-dev.online-bj.com/";
				break;
			}

			// B2B
			case 2: {
				URLscheme["patch"] = "http://patch-release.online-bj.com/";
				break;
			}

			// 送審
			case 3: {
				URLscheme["patch"] = "https://patch-submit.online-bj.com/game/";
				break;
			}

			// 外版
			case 4: {
				URLscheme["patch"] = "https://patch.online-bj.com/game/";
				break;
			}

			// alex
			case 5: {
				URLscheme["patch"] = "http://220.134.195.1/public/bonus_casino/html5/alex/Bundle/";
				break;
			}

			// bowei
			case 6: {
				URLscheme["patch"] = "http://220.134.195.1/public/bonus_casino/html5/bowei/Bundle/";
				break;
			}

			// jianmiau-Dev
			case 7: {
				// URLscheme["patch"] = "http://220.134.195.1/public/bonus_casino/html5/jianmiau/Bundle/";
				URLscheme["patch"] = "http://192.168.5.36/Source/_BJ_Source/BJ-Internal-Dev/";
				break;
			}

			// jianmiau-Release
			case 8: {
				URLscheme["patch"] = "http://192.168.5.36/Source/_BJ_Source/BJ-Internal-Release/";
				break;
			}

			// jianmiau-Submit
			case 9: {
				URLscheme["patch"] = "http://192.168.5.36/Source/_BJ_Source/BJ-Submit/";
				break;
			}

			// jianmiau-Out
			case 10: {
				URLscheme["patch"] = "http://192.168.5.36/Source/_BJ_Source/BJ-Out/";
				break;
			}

			// 108-Dev
			case 11: {
				URLscheme["patch"] = "http://192.168.5.108/BJ-Internal-Dev/";
				break;
			}

			// 108-Release
			case 12: {
				URLscheme["patch"] = "http://192.168.5.108/BJ-Internal-Release/";
				break;
			}

			// 108-Submit
			case 13: {
				URLscheme["patch"] = "http://192.168.5.108/BJ-Submit/";
				break;
			}

			// 108-Out
			case 14: {
				URLscheme["patch"] = "http://192.168.5.108/BJ-Out/";
				break;
			}

			default: {
				break;
			}
			// case 0: {
			//   break;
			// }
			// default: {
			//   URLscheme["patch"] = document.getElementById("patch").options[patch].label;
			//   break;
			// }
		}

		let url = "";
		let gameUrl = "220.134.195.1:17456";
		if (!isWan) {
			gameUrl = "192.168.5.108:7456";
		}
		if (iscustom) {
			let URLscheme = new URL(location.href);
			if (this._isiOS || this._isAndroid) {
				url = `bjcasino://get/message${URLscheme.search}`;
			} else {
				url = `http://${gameUrl}/phone.html${URLscheme.search}`;
			}
		} else {
			let _URLSearchParams = new URLSearchParams(URLscheme).toString();
			let _decodeURI = decodeURIComponent(_URLSearchParams);
			if (this._isiOS || this._isAndroid) {
				url = `bjcasino://get/message${_decodeURI ? `?${_decodeURI}` : ""}`;
			} else {
				url = `http://${gameUrl}/phone.html${_decodeURI ? `?${_decodeURI}` : ""}`;
			}
		}
		if (this._isiOS || this._isAndroid) {
			window.open(url, "_self");
		} else {
			window.open(url, "_blank");
		}
	}

	//#endregion
}
