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";
import { Tips } from "../../scripts/mgr/Tips";
import GameM from "../../scripts/api/GameM";
import ItemsM from "../../scripts/mgr/ItemsM";
import UserM from "../../scripts/api/UserM";
import UIUtils from "../../scripts/base/UIUtils";
import { Vec3 } from "cc";
import { isValid } from "cc";
const { ccclass, property } = _decorator;

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

@ccclass("CritLayer")
export class CritLayer extends BaseUI {
  static async show(gameId: number, spinId: string, pogValue: number) {
    if (pogValue <= 0) {
      Tips.show("Invalid pog value");
      return;
    }
    let layer = await Hall.ins.showLayer("prefab/layer/CritLayer");
    layer.getComponent(CritLayer).init(gameId, spinId, pogValue);
  }

  private pogValue: number;
  private critRewardPog: number = 0;
  private spinId: string = "";
  private gameId: number = 0;
  async init(gameId: number, spinId: string, pogValue: number) {
    this.pogValue = pogValue;
    this.gameId = gameId;
    this.spinId = spinId;

    let result = await GameM.ins.getCritReward(gameId, spinId);
    if (!result) {
      this.playAnim();

      return;
    }

    UserM.ins.refreshGoods(result.goodList);

    this.critRewardPog = result.critPog;

    this.setText("lbl_crit_pog1", "" + Utils.formatNumber(pogValue, 2));
    this.setText(
      "lbl_crit_pog2",
      "" + Utils.formatNumber(pogValue + result.critPog, 2)
    );
    this.playAnim();


  }

  playAnim() {
    let self=this;
    this.a1.active = true;
    this.a1.setPosition(0, -500, 0);
    this.a2.setPosition(0, -500, 0);
    this.a3.setPosition(0, -500, 0);
    this.a3.setScale(0.5, 0.5, 0.5);
    UIUtils.AnimBezierMoveAndScale(
      this.a1,
      new Vec3(0, 0, 0),
      0.7,
      0.5,
      () => {}
    );
    setTimeout(() => {
      this.a2.active = true;
      UIUtils.AnimBezierMoveAndScale(this.a2, new Vec3(0, 0, 0), 1, 1, () => {});
    }, 300);
    setTimeout(() => {
      if (!isValid(self.node)) {
        return;
      }
      this.a3.active = true;
      UIUtils.AnimBezierMoveAndScale(
        self.a3,
        new Vec3(0, 0, 0),
        1.4,
        1,
        () => {}
      );
    }, 500);
   
  }
  private a1: Node;
  private a2: Node;
  private a3: Node;
  protected onLoad(): void {
    super.onLoad();
    this.a1 = this.FindNode("anim1").children[0];
    this.a2 = this.FindNode("anim2").children[0];
    this.a3 = this.FindNode("anim3").children[0];
    this.a1.active = false;
    this.a2.active = false;
    this.a3.active = false;
  }
  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_claim":
        break;
      case "btn_claim_critical":
        break;
    }

    this.closePage();
  }
}
