/// #pkgName FGUI包名
/// #prefabName ui预制名字
/// #viewName uiview名字
/// #UIName ui的名字，prefabName首字母转大写
/// ui逻辑处理类
/// created by chenwb 2024

import Config from "../../../../Config";
import GameController from "../../../../GameController";
import { PlayerLogin, PlayerLoginPram } from "../../../../common/Xyc";
import { SevBack } from "../../../../common/Xys";
import GameDataCenter from "../../../../data/GameDataCenter";
import { GameEvent, LoadEvent } from "../../../../data/const/EventConst";
import FguiMgr from "../../../../frameWork/fgui/FguiMgr";
import { FguiViewCtrl } from "../../../../frameWork/fgui/mvc/FguiViewCtrl";
import EventMng from "../../../../manager/EventMng";
import { FormulaCom } from "../../../../utils/Formula";
import UIHelp from "../../../ui/UIHelp";
import { LoginVM } from "./LoginVM";
import { LoginView } from "./LoginView";

export class LoginCtrl extends FguiViewCtrl<LoginVM> {
    OnInited(): void {
    }
    OnShow(intent?: any): void {
        this.AddListeners();
        if (GameDataCenter.login.getAccount().length == 0) {
            this.VM.AccountTxt.text = "ZH" + FormulaCom.uuid(3, 8)
        } else {
            this.VM.AccountTxt.text = GameDataCenter.login.getAccount()
        }
    }
    OnHide(): void {
        this.RemoveListeners();
    }
    //#region UI事件
    private AddListeners(): void {
        this.RegisterClick(this.VM.LoginBtn, this.OnClickLoginBtn);
        EventMng.on(GameEvent.LOGIN_ACCOUNT_SUCC, this.onLoginAccountSuccess, this);

    }
    private RemoveListeners(): void {
        // <#UIEventsRemoveArea>
        EventMng.off(GameEvent.LOGIN_ACCOUNT_SUCC, this.onLoginAccountSuccess, this);
    }
    private OnClickLoginBtn(): void {
        let account = this.VM.AccountTxt.text
        if (account.length == 0) {
            UIHelp.ShowTips("请输入账号")
            return
        }
        let loginParam: PlayerLoginPram = {
            pid: Config.pid,
            param: [account]
        }
        // EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.1, "login")
        // GameController.network.send(PlayerLogin.url, loginParam, (result: SevBack) => {
        //     if (result.type == 1) {
        //         EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.5, "login")
        //         GameDataCenter.plat.instance.openId = account
        //         GameDataCenter.plat.instance.loginParams = [result.platBack.token]
        //         GameDataCenter.plat.instance.login()
        //     }
        // })

        GameDataCenter.login.sendPlatLoginGame(loginParam);
    }

    onLoginAccountSuccess() {
        GameDataCenter.login.saveAccount(this.VM.AccountTxt.text);
        this.Close()
    }

    private Close(): void {
        FguiMgr.Instance.closeUI(LoginView, true);
    }
    //#endregion
}