import CSMessage from "../../Base/CSMessage";
import { INetResponse } from "../../Engine/CatanEngine/NetManagerV2/Core/INetResponse";
import { MainControl } from "../MainControl";

export default class MainControlData {
	private _disconnetErrorType: number = null;

	constructor() {
		MainControl.DataReceivedEvent.AddCallback(this._dataReceivedEvent, this);
	}
	private _dataReceivedEvent(param: any[] = null): void {
		let type: MainControl.DataType = param[0];
		let data: any = param[1];
		switch (type) {
			case MainControl.DataType.ServerData:
				this._serverData(data);
				break;
			case MainControl.DataType.NetDisconnected:
				this._netDisconnected();
				break;
			default:
				break;
		}
	}
	// =======================================================================================
	/** SERVER主動通知 */
	private _serverData(resp: INetResponse<any>): void {
		if (resp.IsValid) {
			switch (resp.Method) {
				case "sys.disconnect":
					let error: number = +resp.Data["c"];
					if (error > 100) {
						if (error != 201) {
							this._disconnetErrorType = +resp.Data["c"];
						} else {
							MainControl.Instance.IsLogout = true;
						}
					}
					break;
				default:
					break;
			}
		}
	}
	// =======================================================================================
	/** SOCKET斷線 */
	private _netDisconnected(): void {
		// 斷線不會收到回傳值
		let CommonStringNum: number = 9;
		if (this._disconnetErrorType != null) {
			if (this._disconnetErrorType === 101) {
				CSMessage.CreateYesNoMsg("帳號重複登入，請重新刷新網頁", () => {
					window.location.reload();
				});
				MainControl.Instance.IsLogout = true;
			}
			this._disconnetErrorType = null;
		}
		if (!MainControl.Instance.IsLogout) {
			// //遊戲維護中字串
			// let showStr: string = ""
			// if (CommonStringNum != 14) {
			// 	showStr = CSSettingsV3.prototype.CommonString(CommonStringNum);
			// } else {
			// 	showStr = Loading.maintainStr;
			// }
			// CSMessage.CreateYesMsg(
			// 	showStr,
			// 	this._type2GoLogin
			// );
		}
		MainControl.Instance.IsLogout = false;
		// MainControl.Instance.LastPlayGameID = 0;
		// MainControl.Instance.IsInGame = false;
		// AwardsNotifyManager.Instance?.Clear();
	}
}
