import { _decorator, Component, Node } from "cc";
import Utils from "../../scripts/utils/Utils";
import { Hall } from "../hall/Hall";
import BaseUI from "../../scripts/base/BaseUI";
import { RewardLayer } from "./RewardLayer";
import { GoodsId } from "../../scripts/api/GoodsId";
const { ccclass, property } = _decorator;

export interface POGClaimLayerCallback {
  onClaim(isCritical: boolean): void;
}

@ccclass("POGClaimLayer")
export class POGClaimLayer extends BaseUI {
  static async show(pogValue: number, cb: POGClaimLayerCallback) {
    if (!pogValue || pogValue <= 0) {
      pogValue = 500;
    }

    let layer = await Hall.ins.showLayer("prefab/layer/POGClaimLayer");
    layer.getComponent(POGClaimLayer).init(pogValue, cb);
  }

  private pogValue: number;
  private cb: POGClaimLayerCallback;

  init(pogValue: number, cb: POGClaimLayerCallback) {
    this.pogValue = pogValue;
    this.cb = cb;

    this.setText("lbl_count1", "x" + Utils.formatNumber(this.pogValue, 0));
    this.setText("lbl_count2", "x" + Utils.formatNumber(this.pogValue, 0));
  }

  protected simpleOnBtnClick(name: string): void {
    let isCritical = false;
    switch (name) {
      case "btn_claim":
        isCritical = false;
        break;
      case "btn_claim_critical":
        isCritical = true;
        break;
    }

    RewardLayer.show([
      {
        id: GoodsId.POG,
        count: isCritical ? this.pogValue * 2 : this.pogValue,
      },
    ]);
    this.cb.onClaim(isCritical);
    this.closePage();
  }
}
