import {
  _decorator,
  Component,
  instantiate,
  Node,
  Prefab,
  ScrollView,
} from "cc";
import BaseUI from "../base/BaseUI";
import { ModuleDef } from "../../scripts/ModuleDef";
import { GameUILayers } from "../../scripts/GameUILayers";
import { AB } from "../base/AB";
import { RankingPlayerItem } from "../ui_items/RankingPlayerItem";
import { Lobby } from "./Lobby";
import { CommonTabsTitle } from "../ui_base/CommonTabsTitle";
const { ccclass, property } = _decorator;

@ccclass("BallUIRanking")
export class BallUIRanking extends BaseUI {
  static show() {
    tgx.UIMgr.inst.show(
      ModuleDef.BALL,
      "ui_lobby/ui_ranking",
      GameUILayers.POPUP
    );
  }

  protected onDestroy(): void {
    Lobby.ins.enter();
  }

  private _ScrollView: ScrollView;
  private _tpl: Node;
  private _tabs: CommonTabsTitle;
  protected onLoad(): void {
    super.onLoad();
    this._ScrollView = this.FindAs("ScrollView", ScrollView);
    this._tpl = this.FindNode("RankingPlayerItem");
    this._tpl.active = false;
    let self = this;
    this._tabs = this.getComponentInChildren(CommonTabsTitle);
    this._tabs.init(["RANKING", "SEASON RANKING"], {
      onTabClick(index) {
        self.tabSelect(index);
      },
    });
  }

  private tabSelect(tab: number) {
    this._tabs.setSelect(tab);
    this.refreshUI();
  }

  protected start(): void {
    this.tabSelect(0);
  }
  async refreshUI() {

    tgx.UIWaiting.show();
    await new Promise((resolve) => setTimeout(resolve, 500));

    let start_time = Date.now();
    this._ScrollView.content.removeAllChildren();
    this.initMyInfo();
    for (let i = 0; i < 100; i++) {
      let item = instantiate(this._tpl);
      item.active = true;
      let rankingPlayerItem = item.getComponent(RankingPlayerItem);
      let itemData = {
        rank: i + 1,
        name: "Player " + (i + 1),
        score: 1000 + i * 100,
      };
      item.setPosition(0, 0, 0);
      this._ScrollView.content.addChild(item);
      rankingPlayerItem.init(itemData);
    }
    await new Promise((resolve) => setTimeout(resolve, 500));

    tgx.UIWaiting.hide();
  }
  initMyInfo() {
    this.setText("lbl_player_name_my", "my little dog");
    this.setText("lbl_rank_my", "999+");
    this.setText("lbl_score_my", "0");
  }
}
