
export class LP2_Casino_Deeplink {

	//#region public

	public Linetype: any[] = [4, ["外版", "B2B", "Test", "QA", "內版"]];
	public Servertype: any[] = [5, ["不選擇版本", "外版", "B2B", "Test", "QA", "內版"]];
	public Hostport: any[] = [5, ["不選擇版本", "外版", "B2B", "Test", "QA", "內版"]];
	public Patch: any[] = [5, [
		"不選擇版本",
		"外版",
		"B2B",
		"Test",
		"QA",
		"內版",
		"建喵-內版",
		"建喵-Test",
		"建喵-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.linetype.value = this.Linetype[0];
		this._client.servertype.value = this.Servertype[0];
		this._client.hostport.value = this.Hostport[0];
		this._client.patch.value = this.Patch[0];
		this._client.linetypeDefault.value = this.Linetype[1][this._client.linetype.value];
		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 SelectLinetype(data: any) {
		// data为el-option上:value绑定的对象
		this._client.linetype.value = data;

		let select: string = this.Linetype[1][data];

		if (select) {
			let servertype: number;
			for (let i = 0; i < this.Servertype[1].length; i++) {
				const servertypetName = this.Servertype[1][i];
				if (servertypetName === select) {
					servertype = i;
				}
			}
			this._client.servertype.value = servertype;
			this._client.servertypeDefault.value = this.Servertype[1][this._client.servertype.value];

			let hostport: number;
			for (let i = 0; i < this.Hostport[1].length; i++) {
				const hostportName = this.Hostport[1][i];
				if (hostportName === select) {
					hostport = i;
				}
			}
			this._client.hostport.value = hostport;
			this._client.hostportDefault.value = this.Hostport[1][this._client.hostport.value];

			let patch: number;
			for (let i = 0; i < this.Patch[1].length; i++) {
				const patchName = this.Patch[1][i];
				if (patchName === select) {
					patch = i;
				}
			}
			this._client.patch.value = patch;
			this._client.patchDefault.value = this.Patch[1][this._client.patch.value];
		}
	}

	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 GetLineType(): { key: string; value: string; label: string; }[] {
		const linetypes: any[] = this.Linetype[1];
		let options: any[] = [];
		for (let i: number = 0; i < linetypes.length; i++) {
			const linetype = linetypes[i];
			options.push({
				value: i,
				label: linetype,
			});
		}
		return options;
	}

	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;
	}

	//#endregion

	//#region Custom

	public OnclickOpen(isWan: boolean = false, obj: Object = {}) {
		let hostport = this._client.hostport.value;
		let linetype = +this._client.linetype.value;
		let servertype = +this._client.servertype.value;
		let patch = +this._client.patch.value;
		let gameUrl = "https://liff.line.me/2000222870-MOkW3vgO?showtype=2";
		let E_iscustom: any = document.getElementById("iscustom");
		let iscustom = E_iscustom.checked;
		let URLscheme: any = { v: Date.now() };

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

		switch (servertype) {

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

			// 外版
			case 1: {
				URLscheme["servertype"] = 2;
				break;
			}

			// B2B
			case 2: {
				URLscheme["servertype"] = 6;
				break;
			}

			// Test
			case 3: {
				URLscheme["servertype"] = 8;
				break;
			}

			// QA
			case 4: {
				URLscheme["servertype"] = 7;
				break;
			}

			// 內版
			case 5: {
				URLscheme["servertype"] = 5;
				break;
			}

			default: {
				break;
			}
		}

		switch (hostport) {

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

			// 外版
			case 1: {
				URLscheme["host"] = "https://game.song-yy.com";
				URLscheme["port"] = "9005";
				break;
			}

			// B2B
			case 2: {
				URLscheme["host"] = "https://b2b.song-yy.com";
				URLscheme["port"] = "9005";
				break;
			}

			// Test
			case 3: {
				URLscheme["host"] = "https://testing.song-yy.com";
				URLscheme["port"] = "9005";
				break;
			}

			// QA
			case 4: {
				URLscheme["host"] = "https://qa.song-yy.com";
				URLscheme["port"] = "9005";
				break;
			}

			// 內版
			case 5: {
				URLscheme["host"] = "https://dev.song-yy.com";
				URLscheme["port"] = "9005";
				break;
			}

			default: {
				break;
			}
		}

		switch (patch) {
			case 0: {
				break;
			}

			// 外版
			case 1: {
				URLscheme["patch"] = "https://patch.song-yy.com/";
				break;
			}

			// B2B
			case 2: {
				URLscheme["patch"] = "https://patch-b2b.song-yy.com/";
				break;
			}

			// Test
			case 3: {
				URLscheme["patch"] = "https://patch-testing.song-yy.com/";
				break;
			}

			// QA
			case 4: {
				URLscheme["patch"] = "https://patch-qa.song-yy.com/";
				break;
			}

			// 內版
			case 5: {
				URLscheme["patch"] = "https://patch-dev.song-yy.com/";
				break;
			}

			default: {
				break;
			}
		}

		switch (linetype) {
			// 外版
			case 0: {
				gameUrl = "2000222941-8ORVa7oZ";
				break;
			}

			// B2B
			case 1: {
				gameUrl = "2000222883-BvlaG5PA";
				break;
			}

			// Test
			case 2: {
				gameUrl = "1657864500-N3YEgz6p";
				break;
			}

			// QA
			case 3: {
				gameUrl = "2000773712-eY3J97pd";
				break;
			}

			// 內版
			case 4: {
				gameUrl = "2000222870-MOkW3vgO";
				break;
			}

			default: {
				break;
			}
		}

		let url = "";
		if (iscustom) {
			let URLscheme = new URL(location.href);
			if (this._isiOS || this._isAndroid) {
				url = `https://line.me/R/app/${gameUrl}/${URLscheme.search}`;
			} else {
				url = `https://liff.line.me/${gameUrl}/${URLscheme.search}`;
			}
		} else {
			let _URLSearchParams = new URLSearchParams(URLscheme).toString();
			let _decodeURI = decodeURIComponent(_URLSearchParams);
			if (this._isiOS || this._isAndroid) {
				url = `https://line.me/R/app/${gameUrl}/${_decodeURI ? `?${_decodeURI}` : ""}`;
			} else {
				url = `https://liff.line.me/${gameUrl}/${_decodeURI ? `?${_decodeURI}` : ""}`;
			}
		}
		if (this._isiOS || this._isAndroid) {
			window.open(url, "_self");
		} else {
			window.open(url, "_blank");
		}
	}

	//#endregion
}
