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

import { Layout } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import Utils from "../../scripts/utils/Utils";
import { Tips } from "../../scripts/mgr/Tips";
import UserM from "../../scripts/api/UserM";
import { GoodsId } from "../../scripts/api/GoodsId";

const { ccclass, property } = _decorator;

export enum PayType {
  Gem = 1,
  TOG = 2,
  USDT = 3,
  KAIA = 4,
}

export interface IPayItem {
  type: PayType;
  price: number;
}

export interface IPayListener {
  onPay(type: PayType, price: number);
}

@ccclass("PayItems")
export class PayItems extends BaseUI {
  public static getPayTypeName(type: PayType): string {
    switch (type) {
      case PayType.Gem:
        return "Sunshine";
      case PayType.TOG:
        return "ZP";
      case PayType.USDT:
        return "USD";
      case PayType.KAIA:
        return "KAIA";
    }
  }

  public onLoad() {
    super.onLoad();
  }

  private _payListener: IPayListener;
  private _items: IPayItem[];
  public init(items: IPayItem[], listener: IPayListener) {
    this._payListener = listener;
    this._items = items;

    for (let item of this.node.children) {
      item.active = false;
    }

    let padding = 10;
    switch (items.length) {
      case 1:
        padding = 30;
        break;
      case 2:
        padding = 20;
        break;
      case 3:
        padding = 20;
        break;
      case 4:
        padding = 10;
        break;
    }

    for (let item of items) {
      this.addPayType(item.type, item.price, padding);
    }
  }

  private addPayType(type: PayType, price: number, padding) {
    switch (type) {
      case PayType.Gem:
        let btnGem = this.FindNode("btn_gem");
        btnGem.active = true;
        btnGem.getComponent(Layout).paddingLeft = padding/2;
        btnGem.getComponent(Layout).spacingX = 10;
        btnGem.getComponent(Layout).paddingRight = padding+padding*0.3;
        this.setText("lbl_gem", Utils.formatNumber(price));

        break;
      case PayType.TOG:
        let btnZp = this.FindNode("btn_tog");
        btnZp.active = true;
        btnZp.getComponent(Layout).paddingLeft = padding;
        btnZp.getComponent(Layout).paddingRight = padding;
        btnZp.getComponent(Layout).spacingX = padding / 2;
        this.setText("lbl_zp", Utils.formatNumber(price, 1));
        break;
      case PayType.USDT:
        let btnUsdt = this.FindNode("btn_usdt");
        btnUsdt.active = true;
        btnUsdt.getComponent(Layout).paddingLeft = padding * 1.5;
        btnUsdt.getComponent(Layout).paddingRight = padding * 1.5;
        btnUsdt.getComponent(Layout).spacingX = padding;
        console.log("price", price);
        this.setText("lbl_usdt", "$" + Utils.formatNumber(price,2));
        break;
      case PayType.KAIA:
        let btnKaia = this.FindNode("btn_kaia");
        btnKaia.active = true;
        btnKaia.getComponent(Layout).paddingLeft = padding;
        btnKaia.getComponent(Layout).paddingRight = padding;
        btnKaia.getComponent(Layout).spacingX = padding / 2;
        this.setText("lbl_kaia", Utils.formatNumber(price));
        break;
    }
  }

  private onPay(type: PayType, price: number) {
    let item = this._items.find((item) => item.type === type);
    if (item) {
      this._payListener.onPay(item.type, item.price);
    }
  }

  private getPrice(type: PayType): number {
    let item = this._items.find((item) => item.type === type);
    if (item) {
      return item.price;
    }
    return 0;
  }

  protected onBtnClick(name: string, event: Event, customEventData: any): void {
    super.onBtnClick(name, event, customEventData);

  
    let price = 0;



    switch (name) {
      case "btn_gem":
        let gemPrice = this.getPrice(PayType.Gem);
        if ((this.getUserGem() || 0) < gemPrice) {
          Tips.show(Utils.setI18nLabel("Common.NoGem"));
          return;
        }
        this.onPay(PayType.Gem, gemPrice);
        break;
      case "btn_zp":
        let zpPrice = this.getPrice(PayType.TOG);
        // let zpBalance = WalletData.ins.getZpBalance();
        // if (zpBalance < zpPrice) {
        //   MsgHints.show(Utils.setI18nLabel("Title20"));
        //   return;
        // }
        this.onPay(PayType.TOG, zpPrice);
        break;
      case "btn_usdt":
        let usdtPrice = this.getPrice(PayType.USDT);
        this.onPay(PayType.USDT, usdtPrice);
        break;
      case "btn_kaia":
        let kaiaPrice = this.getPrice(PayType.KAIA);
        this.onPay(PayType.KAIA, kaiaPrice);
        break;
    }
  }
  getUserGem(): number {
    return UserM.ins.getGoodsCount(GoodsId.GEM);
  }
}
