import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import PoolM, { PoolType } from "../../scripts/mgr/PoolM";
import Utils from "../../scripts/utils/Utils";
import UIUtils from "../../scripts/base/UIUtils";
import { GoodInfo } from "../../scripts/api/UserM";
import { GoodsId } from "../../scripts/api/GoodsId";
import { Vec3 } from "cc";
import { instantiate } from "cc";
const { ccclass, property } = _decorator;

export enum FlyType {
  POG = 1,
  GEM = 2,
}

@ccclass("FlyContainer")
export class FlyContainer extends BaseUI {
  private slot_gem: Node;
  private slot_pog: Node;
  private slot_storage: Node;
  protected onLoad(): void {
    super.onLoad();
    FlyContainer._ins = this;
    this.slot_gem = this.FindNode("slot_gem");
    this.slot_pog = this.FindNode("slot_pog");
    this.slot_storage = this.FindNode("slot_storage");
  }

  private static _ins: FlyContainer = null;
  public static get ins(): FlyContainer {
    return FlyContainer._ins;
  }

  public onReward(RewardItem: Node, good: GoodInfo) {
    let nP = null;
    if (good.id == GoodsId.AIR) {
      return;
    }
    switch (good.id) {
      case GoodsId.GEM:
      case GoodsId.DIAMOND_10:
        nP = this.slot_gem;
        break;
      case GoodsId.POG:
      case GoodsId.POG_10:
        nP = this.slot_pog;
        break;
    }

    if (nP == null) {
      nP = this.slot_storage;
    }

    let a = RewardItem.getChildByName("icon_layout");
    UIUtils.moveToNewParent(nP, a);
    UIUtils.AnimBezierMoveAndTranslate(a, new Vec3(0, 0, 0), 1, 1, () => {
      a.destroy();
    });
  }

  public async fly(type: FlyType) {
    let toPosition = null;

    if (type == FlyType.POG) {
      toPosition = this.FindNode("slot_pog");
    } else if (type == FlyType.GEM) {
      toPosition = this.FindNode("slot_gem");
    } else {
      toPosition = this.slot_storage;
    }

    if (!toPosition) {
      return;
    }

    for (let i = 0; i < 3; i++) {
      this.flyOne(i, type, toPosition);
    }
  }
  async flyOne(i: number, type: FlyType, toPosition: Node) {
    //@ts-ignore
    let node: Node = await PoolM.ins.getGem();
    node.parent = this.node;
    node.setPosition(0, 0, 0);

    UIUtils.AnimBezierMoveAndTranslate(
      node,
      toPosition.position,
      1,
      0.5,
      () => {
        //@ts-ignore
        PoolM.ins.put(type, node as Node);
      }
    );
  }
}
