import { _decorator, Component, Node } from "cc";
import { Hall } from "../hall/Hall";
import BaseUI from "../../scripts/base/BaseUI";
import WalletM from "../../scripts/api/WalletM";
import UserM from "../../scripts/api/UserM";
import { instantiate } from "cc";
import { ScrollView } from "cc";
import AB from "../../scripts/base/AB";
import { Prefab } from "cc";
import { BadgeItem } from "../item/BadgeItem";
const { ccclass, property } = _decorator;

@ccclass("BadgeDetailLayer")
export class BadgeDetailLayer extends BaseUI {
  static TYPE_RANK = 1;
  static TYPE_GAME_PASS = 2;
  static async show(type: number) {
    let layer = await Hall.ins.showLayer("prefab/layer/BadgeDetailLayer");
    layer.getComponent(BadgeDetailLayer).init(type);
  }

  private scrollView: ScrollView = null;

  protected onLoad(): void {
    super.onLoad();
    this.scrollView = this.FindAs("ScrollView", ScrollView);
  }
  private _type: number = 0;
  async init(type: number) {
    this._type = type;
    let tpl: Prefab = await AB.ins.loadPrefab("prefab/item/BadgeItem");

    let all = await UserM.ins.getBadgeList();
    let list = [];
    for (let i = 0; i < all.badgeList.length; i++) {
      if (all.badgeList[i].type == type) {
        list.push(all.badgeList[i]);
      }
    }
    if (list.length == 0) {
      this.FindNode("NoData").active = true;
      return;
    }

    this.FindNode("NoData").active = false;

    for (let i = 0; i < list.length; i++) {
      let item = list[i];
      let itemNode = instantiate(tpl);
      itemNode.getComponent(BadgeItem).init(item);
      this.scrollView.content.addChild(itemNode);
    }
  }
}
