import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { Hall } from "../hall/Hall";
import { instantiate } from "cc";
import { DiamondRechargeItem } from "../item/DiamondRechargeLayerItem";
import RechargeM, { RechargeDto } from "../../scripts/api/RechargeM";
import { Event } from "cc";
import UserM from "../../scripts/api/UserM";
import { GoodsId } from "../../scripts/api/GoodsId";
import Utils from "../../scripts/utils/Utils";
import EV, { EV_TYPE } from "../../scripts/mgr/EV";

const { ccclass, property } = _decorator;

@ccclass("GemRechargeLayer")
export class GemRechargeLayer extends BaseUI {
  static async show() {
    let layer = await Hall.ins.showLayer("prefab/layer/GemRechargeLayer");
    layer.getComponent(GemRechargeLayer).init();
  }
  protected onLoad(): void {
    super.onLoad();
    EV.ins.on(EV_TYPE.USER_GOOD_REFRESH, this.refreshGemValue, this);

  }

  private refreshGemValue() {
    this.setText(
      "lbl_gem_balance",
      Utils.formatNumber(UserM.ins.getGoodsCount(GoodsId.GEM), 0)
    );
  }
  async init() {
   this.refreshGemValue();

    let content = this.FindNode("content");
    let tpl = content.children[0];
    tpl.active = false;
    let list = await this.getDataList();
    for (let i = 0; i < list.length; i++) {
      let item = instantiate(tpl);
      item.parent = content;
      item.active = true;
      item.getComponent(DiamondRechargeItem).init(list[i]);
    }
  }
  async getDataList(): Promise<RechargeDto[]> {
    let result = await RechargeM.ins.getRechargeList();
    // result.push(...(await RechargeM.ins.getRechargeList()));
    return result;
  }
  protected onBtnClick(name: string, event: Event, customEventData: any): void {
    super.onBtnClick(name, event, customEventData);
    switch (name) {
    }
  }
}
