import Gamecfg from "../../../../common/gameCfg";
import GameDataCenter from "../../../../data/GameDataCenter";
import { ItemEvent, UserEvent, ZhanLiEvent } from "../../../../data/const/EventConst";
import { ConstItem, HeadBgType } from "../../../../data/const/TypeConst";
import EventMng from "../../../../manager/EventMng";
import ClickAuEffect from "../../../../utils/ClickAuEffect";
import GameMath from "../../../../utils/GameMath";
import { GridEvent } from "../../../gridMap/GridEvent";
import ItemCount, { ItemCountExtraParam } from "../../Common/ItemCount";
import UserHead from "../../Common/UserHead";

export default class UIHeadTopView {
    private _HeadBtn: fairygui.GObject;
    private _LevelFont: fairygui.GObject;
    private _NameFont: fairygui.GObject;
    private _ZhanLiFont: fairygui.GObject;
    private _UserHead: UserHead;
    private _PbLv: fairygui.GProgressBar;
    ItemGold: ItemCount;
    ItemDiamond: ItemCount;
    ItemSp: fairygui.GProgressBar;
    public constructor(vm: fgui.GComponent) {
        this._HeadBtn = vm.getChild("HeadBtn")
        this._NameFont = vm.getChild("NameFont")
        this._LevelFont = vm.getChild("LevelFont")
        this._ZhanLiFont = vm.getChild("ZhanLiFont")
        this._UserHead = vm.getChild("UserHead") as UserHead;
        this._PbLv = vm.getChild("PbLv") as fgui.GProgressBar;
        this.ItemGold = new ItemCount(vm.getChild("ItemGold") as fgui.GButton);
        this.ItemDiamond = new ItemCount(vm.getChild("ItemDiamond") as fgui.GButton);
        this.ItemSp = vm.getChild("ItemSp") as fgui.GProgressBar;
    }

    public onEnable(): void {
        this.ItemGold.onEnable();
        this.ItemDiamond.onEnable();
        GameDataCenter.item.sendItemInfo();
        EventMng.on(ItemEvent.UP_ITEM, this.onUpItem, this)
        EventMng.on(GridEvent.HC_SP_UP, this.onUpSp, this)
        EventMng.on(ZhanLiEvent.ZHANLI_UP_HOME_ZHANLI, this.onUpZhanLi, this)
        EventMng.on(UserEvent.SET_NAME_SUCC, this.onUpUser, this);
        EventMng.on(UserEvent.UP_USER, this.onUpUser, this);
        this._HeadBtn.onClick(this.OnClickHeadBtn, this)
        this.onUpUser()
        this.onUpZhanLi();
        this.onUpRed();
    }

    public onDisable(): void {
        this.ItemGold.onDisable();
        this.ItemDiamond.onDisable();
        EventMng.off(ItemEvent.UP_ITEM, this.onUpItem, this)
        EventMng.off(GridEvent.HC_SP_UP, this.onUpSp, this)
        EventMng.off(ZhanLiEvent.ZHANLI_UP_HOME_ZHANLI, this.onUpZhanLi, this)
        EventMng.off(UserEvent.SET_NAME_SUCC, this.onUpUser, this);
        EventMng.off(UserEvent.UP_USER, this.onUpUser, this);
        this._HeadBtn.offClick(this.OnClickHeadBtn, this)
    }

    @ClickAuEffect()
    private OnClickHeadBtn() {
        
    }

    onUpUser() {
        let user = GameDataCenter.user?.userInfo;
        this._NameFont.text = user?.name
        let lv = user?.level
        this._LevelFont.text = "Lv." + lv;
        let curLvCfg = Gamecfg.levelLevel.getItem(lv.toString());
        this._PbLv.max = curLvCfg?.exp;
        this._PbLv.value = user?.exp;
        this._UserHead.setHead({ userInfo: user, colorBg: HeadBgType.purple, isFlip: false });
    }

    onUpItem() {
        let param: ItemCountExtraParam = {
            showAddBtn: true,
            fullNumberShow: true,
            isBuyItem: true,
            notEnoughRed: false
        }
        let goldInfo = GameDataCenter.item.getKindItem(ConstItem.coin);
        this.ItemGold.setData(goldInfo, null, param, "ui://Home/Btn_zjm_jinbi");
        let gemInfo = GameDataCenter.item.getKindItem(ConstItem.gem);
        this.ItemDiamond.setData(gemInfo, null, param, "ui://Home/Btn_zjm_zuanshi");
    }

    onUpSp() {
        //体力单独读取
        let hcInfo = GameDataCenter.gridMap?.hcInfo;
        this.ItemSp.max = hcInfo?.maxTili;
        this.ItemSp.value = hcInfo?.tili;
    }

    onUpZhanLi() {
        this._ZhanLiFont.asTextField.text = GameMath.showNum(GameDataCenter.user?.userInfo?.power)
    }

    /** 更新红点 */
    onUpRed() {

    }
}
