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

export interface GradeClickCallback {
  (data: ConfigGrade): void;
}
@ccclass("GradeItem")
export class GradeItem extends BaseUI {
  setSelected(selected: boolean) {
    this.FindNode("select").active = selected;
  }

  public get data(): ConfigGrade {
    return this._data;
  }

  private _data: ConfigGrade;
  private _callback: GradeClickCallback;

  onLoad(): void {
    super.onLoad();
    this.setSelected(false);
  }

  async init(data: ConfigGrade, callback: GradeClickCallback) {
    this._data = data;
    this._callback = callback;
    this.setText("lbl_grade_name", data.name);
    let a = await AB.ins.loadSpriteFrame("texture/common/grade" + data.id + "");
    let f = a as SpriteFrame;
    this.FindAs("icon", Sprite).spriteFrame = f;

    this.setText("lbl_add_value", data.addition + "%");
  }

  protected simpleOnBtnClick(name: string): void {
    this._callback(this._data);
  }
}
