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

import GameDataCenter from "../../../../data/GameDataCenter";
import { ResCollector } from "../../../../frameWork/compment/ResCollector";
import ResSprite from "../../../../frameWork/compment/ResSprite";
import FguiMgr from "../../../../frameWork/fgui/FguiMgr";
import { ListUtil } from "../../../../frameWork/fgui/ListUtil";
import { FguiViewCtrl } from "../../../../frameWork/fgui/mvc/FguiViewCtrl";
import { GameServerConfig } from "../../../../network/GameServerConfig";
import { TsrpcNet } from "../../../../network/TsrpcNet";
import { FightBase } from "../../../../shared/base";
import AssetMgr from "../../../../utils/AssetMgr";
import AssetsBundleMgr from "../../../../utils/AssetsBundleMgr";
import { GridEvent } from "../../../gridMap/GridEvent";
import UEGridMap from "../../../gridMap/UEGridMap";
import UEHomeRole from "../../../gridMap/UEHomeRole";
import UEMergeTip from "../../../gridMap/UEMergeTip";
import { BattleMainView } from "../../Battle/BattleMain/BattleMainView";
import HomeEpsCom from "./HomeEpsCom";
import HomeMergeBottom from "./HomeMergeBottom";
import HomeTaskCom from "./HomeTaskCom";
import { HomeVM } from "./HomeVM";
import { HomeView } from "./HomeView";
import UIHeadTopView from "./UIHeadTopView";
export class HomeCtrl extends FguiViewCtrl<HomeVM> {
    ueGridMap: UEGridMap = null!;
    HeadTopCom: UIHeadTopView;
    HomeEpsCom: HomeEpsCom;
    HomeTaskCom: HomeTaskCom;
    HomeMergeBottom: HomeMergeBottom;
    onCollectRes(resCollector: ResCollector, param: any): void {
        resCollector.AddUEClass([UEGridMap, UEMergeTip, UEHomeRole])
    }

    OnInited(): void {
        this.ueGridMap = AssetMgr.instantiateUE(UEGridMap);
        this.VM.GridRoot.node.addChild(this.ueGridMap.node);
        this.HeadTopCom = new UIHeadTopView(this.VM.HeadTopCom);
        this.HomeEpsCom = new HomeEpsCom(this.VM.EpsCom);
        this.HomeTaskCom = new HomeTaskCom(this.VM.BtnTask);
        this.HomeMergeBottom = new HomeMergeBottom(this.VM.HomeMergeBottom);
        this.ueGridMap.Init();

    }
    async OnShow(intent?: any): Promise<void> {
        this.AddListeners();
        this.HeadTopCom.onEnable();
        this.HomeEpsCom.onEnable();
        this.HomeTaskCom.onEnable();
        this.HomeMergeBottom.onEnable();
        let bgResSprite = this.VM.BgRoot.node.getComponent(ResSprite);
        if (!bgResSprite) {
            bgResSprite = this.VM.BgRoot.node.addComponent(ResSprite);
        }
        bgResSprite.setSpriteFrame('gridMap', 'mapBgs/Img_zjm_changjing01');
    }


    OnHide(): void {
        this.RemoveListeners();
        this.HeadTopCom.onDisable();
        this.HomeEpsCom.onDisable();
        this.HomeTaskCom.onDisable();
        this.HomeMergeBottom.onDisable();
    }
    //#region UI事件
    private AddListeners(): void {
        this.RegisterClick(this.VM.BtnPlays, this.OnClickBtnPlays);
        this.RegisterClick(this.VM.BtnWing, this.OnClickBtnWing);
        this.initEvent(GridEvent.HC_MERGE_RSP, this.OnHcMergeRsp);
    }
    private RemoveListeners(): void {
        // <#UIEventsRemoveArea>
    }

    private OnClickBtnPlays(): void {
        GameDataCenter.battle.SendTestFight(async (fightInfo: FightBase) => {
            GameDataCenter.battle.startFight(fightInfo);
        })
    }

    private OnClickBtnWing(): void {
        GameDataCenter.user.sendChangeName("叫我哥哥");
    }

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


    /** 播放经验飞行效果 */
    private OnHcMergeRsp() {

    }


    private CreateExpItem() {
        // let tipsItem = fgui.UIPackage.createObject("Home", "ExpItem", ShowTipsItem) as ShowTipsItem;;
        // this.showTipsList.push(tipsItem);
        // if (gameMethod.isEmpty(this.VM.Panel) || gameMethod.isEmpty(tipsItem)) {
        //     //还未初始化完成
        //     return null
        // }
        // this.VM.Panel.addChild(tipsItem);
        // tipsItem.x = this.VM.Panel.width / 2;
        // tipsItem.touchable = false;
        // return tipsItem;
    }
}