import { _decorator, Component, Node, Event } from "cc";

import BaseUI from "../../scripts/base/BaseUI";
import { GameDetailLayer } from "../layer/GameDetailLayer";
import { Tips } from "../../scripts/mgr/Tips";
import { GameInfo } from "../../scripts/api/UserM";
import Utils from "../../scripts/utils/Utils";
import AB from "../../scripts/base/AB";
import { Sprite } from "cc";
import ConfigM, { ConfigGame } from "../../scripts/api/ConfigM";
import { DebugM } from "../../scripts/mgr/DebugM";
import { CritLayer } from "../layer/CritLayer";

const { ccclass, property } = _decorator;

@ccclass("PlayGameItem")
export class PlayGameItem extends BaseUI {
  protected onLoad(): void {
    super.onLoad();
  }

  private data: GameInfo;
  init(item: GameInfo) {
    this.data = item;

    if (ConfigM.ins.isOpenPlay(item.gameId)) {
      this.FindNode("btn_play").active = true;
    } else {
      this.FindNode("btn_play").active = false;
    }

    let c = ConfigM.ins.getGame(item.gameId);
    this.setText("lbl_name", c.gameName);
    console.log("item.remainPog", item.remainPog);
    if (item.seasonPog > 0) {
      this.setText(
        "lbl_remain_info",
        Utils.formatNumber(item.remainPog, 2) +
          "/" +
          Utils.formatNumber(item.seasonPog, 0)
      );
      this.setText(
        "lbl_my_reward_pog",
        Utils.formatNumber(item.myRewardPog, 0)
      );
    } else {
      this.setText("lbl_remain_info", "Coming soon");
      this.setText(
        "lbl_my_reward_pog",
        Utils.formatNumber(item.myRewardPog, 0)
      );
    }

    this.setIcon(item.gameId);
  }
  async setIcon(gameId: number) {
    let path = "texture/play/icon_" + gameId;
    let spriteFrame = await AB.ins.loadSpriteFrame(path);
    this.FindAs("icon_game", Sprite).spriteFrame = spriteFrame;
  }

  protected simpleOnBtnClick(name: string): void {
    if (name === "btn_play") {
      // if (DebugM.ins.isDebugPeople()) {
      //   CritLayer.show(this.data.gameId, "123", this.data.remainPog);
      //   return;
      // }

      if (!ConfigM.ins.isOpenPlay(this.data.gameId)) {
        Tips.show("Coming soon");
        return;
      }

      GameDetailLayer.show(this.data.gameId);
    }
  }
}
