/**
 * Extend the basic ActorSheet with some very simple modifications
 * @extends {ActorSheet}
 */

import { YggdrasillUtility } from "./yggdrasill-utility.js";

/* -------------------------------------------- */
export class YggdrasillFigurantSheet extends foundry.appv1.sheets.ActorSheet {

  /** @override */
  static get defaultOptions() {

    return foundry.utils.mergeObject(super.defaultOptions, {
      classes: ["yggdrasill", "sheet", "actor"],
      template: "systems/fvtt-yggdrasill/templates/figurant-sheet.html",
      width: 640,
      height: 720,
      tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "stats" }],
      dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
      editScore: false
    });
  }

  /* -------------------------------------------- */
  async getData() {
    const objectData = foundry.utils.duplicate(this.object)

    let formData = {
      title: this.title,
      id: objectData.id,
      type: objectData.type,
      img: objectData.img,
      name: objectData.name,
      editable: this.isEditable,
      cssClass: this.isEditable ? "editable" : "locked",
      data: foundry.utils.deepClone(this.object.system),
      limited: this.object.limited,
      equipements: this.actor.getToutEquipements(),
      effetsmagiques: this.actor.getEffetsMagiques(),
      encTotal: this.actor.getEncTotal(),
      monnaies: this.actor.getMonnaies(),
      optionsAttr: Array.fromRange(41, 1),
      optionsBase: YggdrasillUtility.createDirectOptionList(0, 20),
      description:    await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.object.system.description, {async: true}),
      notes:    await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.object.system.notes, {async: true}),
      options: this.options,
      owner: this.document.isOwner,
      editScore: this.options.editScore,
      isGM: game.user.isGM,
      config: game.system.config
    }

    console.log("FIGURANT : ", formData);
    return formData;
  }

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

    // Everything below here is only needed if the sheet is editable
    if (!this.options.editable) return;

    // Update Inventory Item
    html.find('.item-edit').click(ev => {
      const li = $(ev.currentTarget).parents(".item");
      const item = this.actor.items.get(li.data("item-id"));
      item.sheet.render(true);
    });
    // Delete Inventory Item
    html.find('.item-delete').click(ev => {
      const li = $(ev.currentTarget).parents(".item");
      YggdrasillUtility.confirmDelete(this, li);
    });

    html.find('.equipement-moins').click(event => {
      const li = $(event.currentTarget).parents(".item");
      this.actor.decrementeQuantite( li.data("item-id") );
    } );
    html.find('.equipement-plus').click(event => {
      const li = $(event.currentTarget).parents(".item");
      this.actor.incrementeQuantite( li.data("item-id") );
    } );

    html.find('.attribut-roll').click((event) => {
      const li = $(event.currentTarget).parents(".item");
      let attrKey = li.data("attr-key");
      let attrSubKey = $(event.currentTarget).data("attr-sub-key");
      this.actor.rollAttribute(attrKey, attrSubKey);
    });
    html.find('.lock-unlock-sheet').click((event) => {
      this.options.editScore = !this.options.editScore;
      this.render(true);
    });
    html.find('.item-link a').click((event) => {
      const itemId = $(event.currentTarget).data("item-id");
      const item = this.actor.items.get(itemId);
      item.sheet.render(true);
    });
    html.find('.item-equip').click(ev => {
      const li = $(ev.currentTarget).parents(".item");
      this.actor.equiperObject( li.data("item-id") );
      this.render(true);
    });

  }

  /* -------------------------------------------- */
  /** @override */
  setPosition(options = {}) {
    const position = super.setPosition(options);
    const sheetBody = this.element.find(".sheet-body");
    const bodyHeight = position.height - 192;
    sheetBody.css("height", bodyHeight);
    return position;
  }

  /* -------------------------------------------- */
  /** @override */
  _updateObject(event, formData) {
    // Update the Actor
    return this.object.update(formData);
  }
}
