import { _decorator, Component, Node } from "cc";
import { BadgeInfo } from "../../scripts/api/UserM";
import ConfigM from "../../scripts/api/ConfigM";
import BaseUI from "../../scripts/base/BaseUI";
import AB from "../../scripts/base/AB";
import { Sprite } from "cc";
const { ccclass, property } = _decorator;

@ccclass("BadgeItem")
export class BadgeItem extends BaseUI {
  async init(item: BadgeInfo) {
    let badge = ConfigM.ins.getBadge(item.id);

    this.setText("lbl_badge_name", badge.desc);
    this.setText("lbl_badge_addition", badge.addition + "%");

    let path = null;
    if (badge.type == 1) {
      path = "texture/common/grade" + badge.id;
    }

    let iconSprite = this.FindAs("icon_badge", Sprite);

    try {
      let spriteFrame = await AB.ins.loadSpriteFrame(path);
      iconSprite.spriteFrame = spriteFrame;
    } catch (error) {
      iconSprite.node.active = false;
    }
  }
}
