import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import UIUtils from "../../scripts/base/UIUtils";
import { Vec3 } from "cc";
import { GoodInfo } from "../../scripts/api/UserM";
import { GoodsId } from "../../scripts/api/GoodsId";
import ConfigM from "../../scripts/api/ConfigM";
import { isValid } from "cc";
const { ccclass, property } = _decorator;


export interface OnItemClick {
  (item: GoodInfo): void;
}

@ccclass("RewardItem")
export class RewardItem extends BaseUI {
  private onItemClick: OnItemClick = null;
  private item: GoodInfo = null;

  public getItem(): GoodInfo {
    return this.item;
  }
  init(delay: number, item: GoodInfo, onItemClick: OnItemClick = null) {
    let self = this;
    self.onItemClick = onItemClick;
    self.item = item;
    this.setText("lbl_count", "x" + item.count);
    let itemName = "id:" + item.id;
    let parent = this.FindNode("icon_layout");

    parent.children.forEach((child) => {
      child.active = false;
    });

    switch (item.id) {
      case GoodsId.POG:
      case GoodsId.POG_10:
        parent.getChildByName("icon_pog").active = true;
        // itemName = "POG";
        break;
      case GoodsId.GEM:
      case GoodsId.DIAMOND_10:
        parent.getChildByName("icon_gem").active = true;
        itemName = "DIAMOND";
        break;
      case GoodsId.GAME_PASS_SHARD:
        parent.getChildByName("icon_puzzle").active = true;
        itemName = "GAME PASS SHARD";
        break;
      case GoodsId.GAME_PASS:
        parent.getChildByName("game_pass").active = true;
        itemName = "GAME PASS";
        break;
      case GoodsId.POG_CRITICAL_CARD:
        parent.getChildByName("icon_crit").active = true;
        itemName = "POG CRIT";
        break;

      case GoodsId.FREE_ITEM_BOX:
      case GoodsId.ITEM_BOX:
        parent.getChildByName("icon_item_box").active = true;
        itemName = "ITEM BOX";
        break;
      case GoodsId.FREE_POG_BOX:
      case GoodsId.POG_BOX:
        parent.getChildByName("icon_pog_box").active = true;
        itemName = "POG BOX";
        break;
      case GoodsId.AIR:
        parent.getChildByName("icon_nothing").active = true;
        itemName = "WHIFF";
        this.FindNode("lbl_count").active = false;
        break;
      case GoodsId.TOG:
        parent.getChildByName("icon_tog").active = true;
        itemName = "TOG";
        break;
      case GoodsId.ES_TOG:
        parent.getChildByName("icon_estog").active = true;
        itemName = "esTOG";
        break;
    }

    let configItemName = ConfigM.ins.getGoodName(item.id);
    this.setText("lbl_name", configItemName);

    if (delay <= 0) {
      self.node.active = true;
    } else {
      this.anim(delay, item);
    }
  }
  anim(delay: number, item: GoodInfo) {
    let time = delay;
    let self = this;
    self.node.setPosition(0, -700, 0);
    self.node.scale = new Vec3(0.5, 0.5, 0.5);
    setTimeout(() => {
      if (isValid(self.node)) {
        self.node.active = true;
        UIUtils.AnimBezierMoveAndScale(this.node, new Vec3(0, 0, 0), 1, 0.5);
      }
    }, time);
  }

  protected simpleOnBtnClick(name: string): void {
    if (this.onItemClick) {
      this.onItemClick(this.item);
    }
  }
}
