import { _decorator, Component, Node } from "cc";
import { Hall } from "../hall/Hall";
import { ScrollView } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import UIUtils from "../../scripts/base/UIUtils";
import AB from "../../scripts/base/AB";
import { Prefab } from "cc";
import { instantiate } from "cc";
import { UITransform } from "cc";
import { Sprite } from "cc";
import { GoodInfo } from "../../scripts/api/UserM";
import { TapClose } from "../../scripts/base/TapClose";
import { RewardItem } from "../item/RewardItem";
import { GoodsId } from "../../scripts/api/GoodsId";
import { FlyContainer } from "../hall/FlyContainer";
import { isValid } from "cc";

const { ccclass, property } = _decorator;

export interface OnCloseCallback {
  (): void;
}
@ccclass("RewardLayer")
export class RewardLayer extends BaseUI {
  static mock() {
    if (1 > 1) {
      RewardLayer.show([
        {
          id: 1,
          count: 100,
        },
      ]);
      return;
    }

    RewardLayer.show([
      {
        id: 1,
        count: 100,
      },
      {
        id: 2,
        count: 600,
      },
      {
        id: 4,
        count: 300,
      },
      {
        id: 1,
        count: 100,
      },
      {
        id: 1,
        count: 100,
      },
      {
        id: 1,
        count: 100,
      },
      {
        id: 2,
        count: 200,
      },
      {
        id: 1,
        count: 300,
      },
      {
        id: 1,
        count: 300,
      },
      {
        id: 1,
        count: 100,
      },
    ]);
  }
  static async show(items: GoodInfo[], onClose: OnCloseCallback = null) {
    let layer = await Hall.ins.showLayer("prefab/layer/RewardLayer");
    layer.getComponent(RewardLayer).init(items, onClose);
  }

  protected onLoad(): void {
    super.onLoad();
  }
  private items: GoodInfo[] = [];
  private onClose: OnCloseCallback;
  init(items: GoodInfo[], onClose: OnCloseCallback) {
    this.items = items;
    this.onClose = onClose;
    let bgTr = this.FindAs("bg", UITransform);

    if (items.length == 1) {
      bgTr.node.getComponent(Sprite).enabled = false;
    }

    this.FindNode("lbl_nothing").active = false;
    if (!items || items.length == 0) {
      this.FindNode("lbl_nothing").active = true;
      return;
    }

    this.ui();
  }

  

  protected closePage(): void {
    this.onClose?.();

    try {
      this.getComponentsInChildren(RewardItem).forEach((item) => {
        let good = item.getItem();
         FlyContainer.ins.onReward(item.node, good);
      });

      
      this.node.destroy();
    } catch (error) {
      console.log("error", error);
    }

    super.closePage();
  }
  async ui() {
    this.canClose = false;
    let tap = this.node.getComponentInChildren(TapClose);
    tap.node.active = false;

    let tpl: Prefab = await AB.ins.loadPrefab("prefab/item/RewardItem");

    let animTime = 200;

    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      let itemNode = instantiate(tpl);

      itemNode.active = false;
      itemNode.setPosition(0, 0, 0);
      itemNode.parent = this.FindNode("slot" + (i + 1));

      let delay = 200 + i * 200;
      animTime = delay;
      itemNode.getComponent(RewardItem).init(delay, item);
    }

    let self = this;
    setTimeout(() => {
      self.canClose = true;
      if (isValid(self.node)) {
        tap.node.active = true;
      }
    }, this.items.length * 200 + 2000);
  }
}
