import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { IPayListener, PayItems, PayType } from "../common/PayItems";
import { Tips } from "../../scripts/mgr/Tips";
import ADManager from "../../scripts/mgr/ADManager";
import ShopM, { ShopDto } from "../../scripts/api/ShopM";
import ConfigM, { ConfigShopItem } from "../../scripts/api/ConfigM";
import { Event } from "cc";
import UserM from "../../scripts/api/UserM";
import PushMsgM from "../../scripts/mgr/PushMsgM";
import { RewardLayer } from "../layer/RewardLayer";
import ItemsM from "../../scripts/mgr/ItemsM";
import { Sprite } from "cc";
import PogAd from "../../scripts/mgr/PogAd";
import { TipsData, TipsLayer } from "../layer/TipsLayer";
import { GoodsId } from "../../scripts/api/GoodsId";
const { ccclass, property } = _decorator;

@ccclass("PogBoosterItem")
export class PogBoosterItem extends BaseUI implements IPayListener {
  public static readonly TYPE_GAME_PASS = 1;
  public static readonly TYPE_POG_BOOSTER = 2;
  public static readonly TYPE_ITEM_BOX = 3;
  public static readonly TYPE_POG_BOX = 4;

  public type: number;

  onPay(type: PayType, price: number) {
    switch (type) {
      case PayType.Gem:
        this.buyUseGem();
        break;
    }
  }
  async buyUseGem() {
    let shopItemId = this._configItem.id;
    let num = this._configItem.num;
    let gem = this._configItem.diamondPrice;
    // UserM.ins.subtractGoods(GoodsId.GEM, gem);

    let result = await ShopM.ins.buyUseGem(shopItemId, num, gem);

    if (result) {
      result.changeList.forEach(async (item) => {
        if (item.count > 0 && ConfigM.ins.GoodIsBox(item.id)) {
          let newResult = await ShopM.ins.openBox(item.id, item.count);
          if (newResult) {
            ItemsM.ins.itemChange(newResult.changeList, newResult.goodList);
          }
        } else {
          ItemsM.ins.itemChange(result.changeList, result.goodList);
        }
      });
    }
  }

  private _data: ShopDto;
  private _configItem: ConfigShopItem;
  private _payItems: PayItems;
  init(type: number, item: ShopDto) {
    this.type = type;
    this._data = item;
    this._configItem = ConfigM.ins.getConfigShopIteByShopId(type, item.id);

    this.setText("lbl_times", "x" + this._configItem.num);
    this.setText("lbl_title", this.getName());

    this.setIcon();
    this._payItems = this.getComponentInChildren(PayItems);
    let btnAD = this.FindNode("btn_ad");

    this._payItems.node.active = true;

    let showAd = this._configItem.adPrice > 0;

    this._payItems.node.active = !showAd;
    btnAD.active = showAd;

    if (showAd) {
      //
      if (this._data.dailyUserAdSell >= this._configItem.dailyUserAdLimit) {
        this.FindAs("btn_ad", Sprite).grayscale = true;
      }
    } else {
      this._payItems.init(
        [
          {
            type: PayType.Gem,
            price: this._configItem.diamondPrice,
          },
        ],
        this
      );
    }
  }
  setIcon() {
    let icons = this.FindNode("icons");
    icons.children.forEach((child) => {
      child.active = false;
    });
    switch (this.type) {
      case PogBoosterItem.TYPE_GAME_PASS:
        break;
      case PogBoosterItem.TYPE_POG_BOOSTER:
        icons.getChildByName("icon_crit").active = true;
        break;
      case PogBoosterItem.TYPE_ITEM_BOX:
        icons.getChildByName("icon_item_box").active = true;
        break;
      case PogBoosterItem.TYPE_POG_BOX:
        icons.getChildByName("icon_pog_box").active = true;
    }
  }
  getName(): string {
    switch (this.type) {
      case PogBoosterItem.TYPE_GAME_PASS:
        return "Game Pass";
      case PogBoosterItem.TYPE_POG_BOOSTER:
        return "POG Crit Tickets";
      case PogBoosterItem.TYPE_ITEM_BOX:
        return "Item Box";
      case PogBoosterItem.TYPE_POG_BOX:
        return "Pog Box";
    }
    return "Unknown";
  }

  private async buyUseAD() {
    // Tips.show("AD Coming soon");
    if (this._data.dailyUserAdSell >= this._configItem.dailyUserAdLimit) {
      this.FindAs("btn_ad", Sprite).grayscale = true;
      Tips.show("AD Limit");
      return;
    }

    let a = await TipsLayer.confirm(
      "",
      "Click GO to finish your ad task for rewards"
    );

    // alert(a)

    let showAdResult = await PogAd.ins.showAd();
    if (!showAdResult) {
      return;
    }

    Tips.show("AD Success");

    let result = await ShopM.ins.buyUseAD(
      this._configItem.id,
      this._configItem.num,
      this._configItem.adPrice
    );

    if (result) {
      ItemsM.ins.itemChange(result.changeList, result.goodList);
      this._data.dailyUserAdSell += 1;
      this.init(this.type, this._data);
    }
  }

  protected onBtnClick(name: string, event: Event, customEventData: any): void {
    if (name == "btn_ad") {
      this.buyUseAD();
    }
  }
}
