import ListModule from "..";
import { DialogItem, ShowItem } from "../objects/item";
let lastDialogTime = Date.now();
let lastDialogId = "";
export default ListModule.action({
  // addDialog(e: any, index: number) {
  //   const item = this.store.items[index];

  //   this.actions.popDialog(e, item, index);
  // },
  popDialog(e: any, item: ShowItem, index = 0) {
    if (this.store.dialogs.length >= 4) {
      return;
    }
    if (Date.now() - lastDialogTime < 300 && lastDialogId == item.id) {
      return;
    }
    lastDialogTime = Date.now();
    lastDialogId = item.id;

    const height = this.store.canvas.height;
    const width = this.store.canvas.width;
    let x = e.clientX;
    let y = e.clientY;
    const conNum = 800 / 4 + 120;
    if (x - conNum <= 0) {
      x = Math.round(x + (conNum - x));
    }
    if (x + conNum > width) {
      x = Math.round(x - (x + conNum - width));
    }
    if (y + conNum > height) {
      y = Math.round(y - (y + conNum - height));
    }
    const dialog = {
      id: Date.now() + "",
      show: true,
      width: 800,
      height: 800,
      x: x,
      y: y,
      dataId: item.id,
      itemIndex: index,
    };
    this.store.dialogs.push(dialog);
  },
  deleteDialog(dialog: DialogItem) {
    const i = this.store.dialogs.indexOf(dialog);
    if (i != -1) {
      this.store.dialogs.splice(i, 1);
    }
  },
  closingDialog(dialg: DialogItem) {
    dialg.show = false;
  },
  moveDialog(dialogId: string, offx: number, offy: number) {
    const d = this.store.dialogs.find(
      (item: DialogItem) => item.id == dialogId
    );
    if (!d) return;
    d.x = Math.round(offx + d.x);
    d.y = Math.round(offy + d.y);
  },
});
