import {
  _decorator,
  Component,
  instantiate,
  Label,
  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 { TaskItem } from "../ui_items/BallUITaskItem";
import { Lobby } from "./Lobby";
import { CommonTabsTitle } from "../ui_base/CommonTabsTitle";
const { ccclass, property } = _decorator;

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

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

  private static TAB_DAILY = 0;
  private static TAB_SEASON = 1;

  private _curTab: number = BallUITask.TAB_DAILY;

  private _ScrollView: ScrollView;
  private _tabs: CommonTabsTitle;
  protected onLoad(): void {
    super.onLoad();
    let self = this;
    this._ScrollView = this.FindAs("ScrollView", ScrollView);
    this._tabs = this.getComponentInChildren(CommonTabsTitle);
    this._tabs.init(["DAILY TASK", "SEASON TASK"], {
      onTabClick(index) {
        self.tabSelect(index);
      },
    });
  }

  protected start(): void {
    this.tabSelect(BallUITask.TAB_DAILY);
  }

  async refreshUI() {
    let self = this;

    this._ScrollView.content.removeAllChildren();
    let tpl = this.FindNode("TaskItem");
    tpl.active = false;
    let list = [];
    if (this._curTab == BallUITask.TAB_DAILY) {
      list = [
        { title: "1", isClaimed: false },
        { title: "2", isClaimed: true },
        { title: "3", isClaimed: false },
        { title: "4", isClaimed: false },
      ];
    } else if (this._curTab == BallUITask.TAB_SEASON) {
      list = [
        { title: "1", isClaimed: false },
        { title: "2", isClaimed: false },
        { title: "3", isClaimed: false },
      ];
    } else {
      list = [];
    }
    for (let i = 0; i < list.length; i++) {
      let item = instantiate(tpl);
      item.active = true;
      item.setPosition(0, 0, 0);
      this._ScrollView.content.addChild(item);
      let taskItem = item.getComponent(TaskItem);
      taskItem.init(list[i], () => {
        self.closePage();
      });
    }
  }

  private tabSelect(tab: number) {
    this._curTab = tab;
    this._tabs.setSelect(tab);

    this.refreshUI();
  }
}
