import {
  _decorator,
  Component,
  Node,
  tween,
  Vec3,
  Color,
  UIOpacity,
  RichText,
} from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import UserM, { GoodInfo } from "../../scripts/api/UserM";
import Utils from "../../scripts/utils/Utils";
import { Label } from "cc";
import { Event } from "cc";
import { Tips } from "../../scripts/mgr/Tips";
import { GemRechargeLayer } from "../layer/GemRechargeLayer";
import { Hall } from "./Hall";
import { RewardLayer } from "../layer/RewardLayer";
import { GoodsId } from "../../scripts/api/GoodsId";
import ItemsM from "../../scripts/mgr/ItemsM";
import EV, { EV_TYPE } from "../../scripts/mgr/EV";
import { PageShop } from "./PageShop";
import { GamePassItem } from "../item/GamePassItem";
const { ccclass, property } = _decorator;

@ccclass("HallTitleBar")
export class HallTitleBar extends BaseUI {
  private t: Label;

  private _pog: number;
  private _gem: number;

  onLoad() {
    super.onLoad();

    this.setText("lbl_title_player_name", UserM.ins.getUserName());

    this._pog = UserM.ins.getGoodsCount(GoodsId.POG);
    this._gem = UserM.ins.getGoodsCount(GoodsId.GEM);
    let self = this;
    setInterval(() => {
      self.setText(
        "lbl_hall_title_season_time",
        UserM.ins.getSeasonEndTimeText()
      );
    }, 1000);

    ItemsM.ins.on(GoodsId.POG, this.onPogChange, this);
    ItemsM.ins.on(GoodsId.GEM, this.onGemChange, this);

    EV.ins.on(EV_TYPE.USER_GOOD_REFRESH, this.onUserGoodRefresh, this);

    this.refreshUI();
  }

  onUserGoodRefresh(goodList: GoodInfo[]) {
    this._pog = UserM.ins.getGoodsCount(GoodsId.POG);
    this._gem = UserM.ins.getGoodsCount(GoodsId.GEM);
    this.refreshUI();
  }

  onPogChange(count: number) {
    this._pog += count;
    if (this._pog < 0) {
      this._pog = 0;
    }
    this.refreshUI();
  }
  onGemChange(count: number) {
    this._gem += count;
    if (this._gem < 0) {
      this._gem = 0;
    }
    this.refreshUI();
  }

  refreshUI() {
    this.setText(
      "lbl_hall_title_season_time",
      UserM.ins.getSeasonEndTimeText()
    );
    this.setText("lbl_pog", Utils.formatNumber(this._pog, 0));
    this.setText("lbl_gem", Utils.formatNumber(this._gem, 0));

    let gamePassCount = UserM.ins.getGamePassCount();
    this.FindNode("lbl_game_pass_count").active = gamePassCount > 0;
    if (gamePassCount > 0) {
      this.setText("lbl_game_pass_count", "x" + gamePassCount);
    }
  }

  protected async simpleOnBtnClick(name: string): Promise<void> {
    switch (name) {
      case "btn_gem":
        GemRechargeLayer.show();
        break;
      case "btn_title_game_pass":
        // Tips.show("Game Pass");
        Hall.ins.switchPage(Hall.PageShop);
        // RewardLayer.mock();
        break;
      case "btn_game_pass":
        let page = await Hall.ins.switchPage(Hall.PageShop);
        let shop = page.getComponent(PageShop);
        shop.switchTab(PageShop.TAB_GAME_PASS);
        if (UserM.ins.getGamePassCount() <= 0) {
          return;
        }

        setTimeout(() => {
          let ts = page.node.getComponentsInChildren(GamePassItem);
          for (let i = 0; i < ts.length; i++) {
            if (ts[i].node.parent.name == "content") {
              ts[i].showAnim(this.FindNode(name));
              break;
            }
          }
        }, 500);

        break;
    }
  }
}
