import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { GameTaskInfo } from "../../scripts/api/UserM";
import GameM from "../../scripts/api/GameM";

const { ccclass, property } = _decorator;

export interface GameTaskItemCallback {
  onClaim(taskId: number, times: number, node: Node): void;
}
@ccclass("GameTaskItem")
export class GameTaskItem extends BaseUI {
  private callback: GameTaskItemCallback;
  private gameId: number;
  private task: GameTaskInfo;
  init(gameId: number, task: GameTaskInfo, callback: GameTaskItemCallback) {
    this.gameId = gameId;
    this.task = task;
    this.callback = callback;

    this.initUI();
  }
  initUI() {

    this.setText("lbl_exp","x"+this.task.xp)
    this.setText("lbl_title", this.task.desc);
    this.setText("lbl_spin_times", "x" + this.task.onceSpin + "");
    this.FindNode("btn_claim").active = this.task.spinCount > 0;
    if (this.task.spinCount > 0) {
      this.setText(
        "lbl_claim_spin_times",
        "Claim(" + this.task.spinCount + ")"
      );
    } else {
      this.setText("lbl_claim_spin_times", "Claim");
    }
  }

  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_claim":
        this.claimSpin();
        break;
    }
  }
  async claimSpin() {
    if (this.task.spinCount <= 0) {
      return;
    }

    let result = await GameM.ins.claimSpinTimes(this.gameId, this.task.taskId);
    if (!result) {
      return;
    }
    this.task.spinCount =0;
    this.initUI();

    this.callback.onClaim(
      this.task.taskId,
      result.currentSpin,
      this.FindNode("icon_spin")
    );
  }
}
