import { RewardLayer } from "../../prefab/layer/RewardLayer";
import { GoodsId } from "../api/GoodsId";
import UserM, { GoodInfo, UserInfo } from "../api/UserM";
import { EventTarget } from "cc";

export default class ItemsM {
  private static _ins: ItemsM;
  public static get ins(): ItemsM {
    return (ItemsM._ins ??= new ItemsM());
  }

  constructor() {
    this.e = new EventTarget();
  }

  private e: EventTarget;

  public on(event: number, callback: (...args: any[]) => void, target: any) {
    this.e.on(event, callback, target);
  }
  public off(event: number, callback: (...args: any[]) => void, target: any) {
    this.e.off(event, callback, target);
  }

  emit(event: number, ...args: any[]) {
    this.e.emit(event, ...args);
  }

  public itemChange(changes: GoodInfo[], finals: GoodInfo[]) {
    let addItems = changes.filter((item) => item.count > 0||item.id==GoodsId.AIR);

    RewardLayer.show(addItems, () => {
      // this.emitSubItems(addItems);
      UserM.ins.refreshGoods(finals);
    });

    // let subItems = changes.filter((item) => item.count < 0);

    // this.emitSubItems(subItems);

    
  }
  // emitSubItems(subItems: GoodInfo[]) {
  //   for (let i = 0; i < subItems.length; i++) {
  //     let item = subItems[i];
  //     this.emit(item.id, item.count);
  //   }
  // }
}
