import { RdDItemSheet } from "./item-sheet.js";
import { Misc } from "./misc.js";
import { RdDUtility } from "./rdd-utility.js";
import { SystemCompendiums } from "./settings/system-compendiums.js";
import { DialogItemAchat } from "./dialog-item-achat.js";
import { RdDItem } from "./item.js";
import { RdDItemService } from "./item-service.js";

export class RdDServiceItemSheet extends RdDItemSheet {

  static get ITEM_TYPE() { return "service" };

  async getData() {
    const formData = await super.getData();
    formData.disabled = formData.isGM || formData.isOwned ? '' : 'disabled';
    return formData;
  }

  activateListeners(html) {
    super.activateListeners(html);

    this.html.find('a.rdd-world-content-link').click(async event => {
      const itemRef = this.getItemRef(event);
      game.items.get(itemRef.id)?.sheet.render(true)
    });

    this.html.find('a.sub-item-acheter').click(async event => {
      const subItem = this.item.findRefItem(this.getItemRef(event));
      await this.item.acheter(RdDUtility.getSelectedActor(), subItem);
    });

    if (!this.options.editable) return;

    this.html.find('a.sub-item-vendre').click(async event => {
      const subItem = this.item.findRefItem(this.getItemRef(event));
      await this.item.vendre(subItem);
    });

    this.html.find('a.sub-item-delete').click(async event => {
      await this.item.removeRefItem(this.getItemRef(event));
    });

    this.html.find('a.sub-item-quantite-moins').click(async event => await this.item.increaseRefItemQuantite(this.getItemRef(event), -1))
    this.html.find('a.sub-item-quantite-plus').click(async event => await this.item.increaseRefItemQuantite(this.getItemRef(event), 1))
    this.html.find('input.sub-item-quantite').change(async event => {
      const newQuantite = Math.max(0, Number.parseInt(this.html.find(event.currentTarget).val()));
      await this.item.updateRefItem(this.getItemRef(event), it => it.system.quantite = newQuantite);
    })
    this.html.find('input.sub-item-cout').change(async event => {
      const newCout = Math.max(0, Number(this.html.find(event.currentTarget).val()));
      await this.item.updateRefItem(this.getItemRef(event), it => it.system.cout = newCout);
    })
    this.html.find('a.sub-item-info-add').click(__ =>
      ui.notifications.info(`Utiliser le glisser-déposer pour ajouter des objets depuis un compendium ou les objets du monde`)
    );

  }

  async _onDropItem(event, dragData) {
    let linkedItem = fromUuidSync(dragData.uuid);
    const existing = this.item.system.items.find(it => it.pack == linkedItem.pack && it.id == linkedItem.id && it.type == linkedItem.type);
    if (existing) {
      ui.notifications.warn(`${this.item.name} contient déjà un ${existing.name}`);
      return;
    }
    if (linkedItem.pack) {
      linkedItem = await SystemCompendiums.loadDocument(linkedItem);
    }
    if (linkedItem.isInventaire()) {
      await this.item.addRefItem(RdDItemService.createSubItem(linkedItem));
    }
    else {
      ui.notifications.warn(`${this.item.name} ne peut pas proposer à la vente de ${Misc.typeName('Item', linkedItem.type)}: ${linkedItem.name}`);
    }
  }

  getItemRef(event) {
    const itemRow = this.html.find(event.currentTarget)?.parents('.item.service-item');
    return { id: itemRow?.data("item-id"), pack: itemRow?.data("pack") ?? undefined }
  }
}
