import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { IPayListener, PayItems, PayType } from "../common/PayItems";
import RechargeM, { RechargeDto } from "../../scripts/api/RechargeM";
import ConfigM, { ConfigRecharge } from "../../scripts/api/ConfigM";
import { Label } from "cc";
const { ccclass, property } = _decorator;

@ccclass("DiamondRechargeItem")
export class DiamondRechargeItem extends BaseUI implements IPayListener {
  configRecharge: ConfigRecharge;
  async onPay(type: PayType, price: number) {
    switch (type) {
      case PayType.USDT:
        let url = await RechargeM.ins.createOrder("USD", this._data.id);
        // window.open(url, "_blank");
        break;
    }
  }
  payItems: PayItems;
  private _data: RechargeDto;
  init(dto: RechargeDto) {
    this.payItems = this.node.getComponentInChildren(PayItems);

    this.configRecharge = ConfigM.ins.getRechargeItem(dto.id);

    this._data = dto;

    this.payItems.init(
      [
        {
          type: PayType.USDT,
          price: this.configRecharge.usd,
        },
      ],
      this
    );

    let extra_value = 0;

    if (dto.firstRecharge > 0) {
      extra_value = this.configRecharge.firstRechargeList[0].count;
    } else {
      if (
        this.configRecharge.bonusList &&
        this.configRecharge.bonusList.length > 0
      ) {
        extra_value = this.configRecharge.bonusList[0].count;
      }
    }

    this.FindNode("bg_extra").active = extra_value > 0;
    if (extra_value) {
      this.setText("lbl_extra_value", "EXTRA x" + extra_value + "");
    }
    let gem = this.configRecharge.goodList[0].count;

    this.setText("lbl_item_title", "Diamond x" + gem);

    let game_pass_extra = 0;
    game_pass_extra = Math.floor(gem * 0.15);
    if (game_pass_extra > 1) {
      this.setText(
        "lbl_game_pass_extra_value",
        "Game Pass Extra +" + game_pass_extra + ""
      )
    } else {
      this.FindNode("lbl_game_pass_extra_value").active = false;
    }

    // let f = this.FindAs("lbl_first_recharge", Label);
    // f.string = "first recharge extra x" + this.configRecharge + "";
    // f.node.active = this._data.firstRecharge > 0;
    this.SetSprite(
      "icon_item_gem",
      "texture/recharge/gem" + this.configRecharge.id + ""
    );
  }
  start() {}

  update(deltaTime: number) {}
}
