import { RdDItem } from "../item.js";
import { RdDBaseActorSheet } from "./base-actor-sheet.js";

/**
 * Extend the basic ActorSheet with some very simple modifications
 * @extends {ActorSheet}
 */
export class RdDCommerceSheet extends RdDBaseActorSheet {

  /** @override */
  static get defaultOptions() {
    return foundry.utils.mergeObject(super.defaultOptions, {
      template: "systems/foundryvtt-reve-de-dragon/templates/actor/commerce-actor-sheet.hbs",
      width: 600, height: 720,
      tabs: []
    }, { inplace: false })
  }
  get title() {
    if (this.actor.token && this.actor.token != this.actor.prototypeToken) {
      return this.actor.token.name;
    }
    return super.title
  }

  async getData() {
    const formData = await super.getData();
    if (this.actor.token && this.actor.token != this.actor.prototypeToken) {
      foundry.utils.mergeObject(formData,
        {
          title: this.actor.token.name,
          token: {
            img: this.actor.token.texture.src
          }
        },
        { overwrite: true });

    }
    return formData;
  }
  /* -------------------------------------------- */
  /** @override */
  activateListeners(html) {
    super.activateListeners(html);

    if (!this.options.editable) return;

    this.html.find('input.item-quantite').change(async event => {
      const newQuantite = Math.max(0, Number.parseInt(this.html.find(event.currentTarget).val()));
      await this.getItem(event)?.update({ "system.quantite": newQuantite });
    })
    this.html.find('input.item-cout').change(async event => {
      const newCout = Math.max(0, Number(this.html.find(event.currentTarget).val()));
      await this.getItem(event)?.update({ "system.cout": newCout });
    })
  }

  getTypesInventaire() {
    return RdDItem.getItemTypesInventaire('all');
  }
}
