import { RdDUtility } from "../rdd-utility.js";
import { Misc } from "../misc.js";
import { DialogSplitItem } from "../dialog-split-item.js";
import { RdDSheetUtility } from "../rdd-sheet-utility.js";
import { Monnaie } from "../item-monnaie.js";
import { ITEM_TYPES } from "../constants.js";
import { RdDItem } from "../item.js";
import { RdDItemCompetenceCreature } from "../item-competencecreature.js";
import { RdDTextEditor } from "../apps/rdd-text-roll-editor.js";
import { ItemAction } from "../item/item-actions.js";

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

  /** @override */
  static get defaultOptions() {
    return foundry.utils.mergeObject(ActorSheet.defaultOptions, {
      classes: ["rdd", "sheet", "actor"],
      tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "carac" }],
      dragDrop: [{ dragSelector: ".item-list .item", dropSelector: undefined }],
      vueDetaillee: false
    }, { inplace: false })
  }

  /* -------------------------------------------- */
  async getData() {
    Monnaie.validerMonnaies(this.actor)

    this.actor.computeEtatGeneral();
    let formData = {
      title: this.title,
      id: this.actor.id,
      type: this.actor.type,
      img: this.actor.img,
      name: this.actor.name,
      system: this.actor.system,
      description: await RdDTextEditor.enrichHTML(this.actor.system.description, this.actor),
      notesmj: await RdDTextEditor.enrichHTML(this.actor.system.notesmj, this.actor),
      options: RdDSheetUtility.mergeDocumentRights(this.options, this.actor, this.isEditable),
      effects: this.actor.effects
    }

    RdDUtility.filterItemsPerTypeForSheet(formData, this.actor.itemTypes);
    formData.calc = {
      fortune: Monnaie.toSolsDeniers(this.actor.getFortune()),
      prixTotalEquipement: this.actor.computePrixTotalEquipement(),
      encTotal: await this.actor.computeEncTotal(),
    }

    this.objetVersConteneur = RdDUtility.buildArbreDeConteneurs(formData.conteneurs, formData.inventaires);
    this._appliquerRechercheObjets(formData.conteneurs, formData.inventaires);
    formData.conteneurs = RdDUtility.conteneursRacine(formData.conteneurs);
    formData.competences.filter(it => it.type == ITEM_TYPES.competencecreature)
      .forEach(it => it.isdommages = RdDItemCompetenceCreature.isDommages(it))

    return formData;
  }

  _appliquerRechercheObjets(conteneurs, inventaires) {
    if (this.options.recherche?.text) {
      const recherche = this.options.recherche;
      const allVisible = inventaires.filter(it => it.isNomTypeLike(recherche.text)).map(it => it.id);
      let addVisible = conteneurs.filter(it => it.isNomTypeLike(recherche.text)).map(it => it.id)
      do {
        allVisible.push(...addVisible)
        const parentsIds = conteneurs.filter(it => it.system.contenu.find(id => allVisible.includes(id))).map(it => it.id)
        addVisible = parentsIds.filter(id => !allVisible.includes(id))
      }
      while (addVisible.length > 0)
      inventaires.forEach(it => it.system.isHidden = !allVisible.includes(it.id))
      conteneurs.forEach(it => it.system.isHidden = !allVisible.includes(it.id))
    }
    else {
      inventaires.forEach(it => it.system.isHidden = false)
      conteneurs.forEach(it => it.system.isHidden = false)
    }
  }

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

    this.html.find('.actionItem').click(async event => await ItemAction.onActionItem(event, this.actor, this.options))
    this.html.find('.item-edit').click(async event => await this.itemActionEdit(event))
    this.html.find('.conteneur-name a').click(async event => {
      RdDUtility.toggleAfficheContenu(this.getItemId(event))
      this.render(true)
    })

    this.html.find('.actor-montrer').click(async event => await this.actor.postActorToChat());

    this.html.find('.recherche')
      .each((index, field) => {
        this._rechercheSelectArea(field);
      })
      .keyup(async event => this._rechercherKeyup(event))
      .change(async event => this._rechercherKeyup(event))

    this.html.find('.recherche').prop("disabled", false)

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

    this.html.find('.item-equip-armure').click(async event => await this.actor.equiperObjet(this.getItem(event)))
    this.html.find('.item-delete').click(async event => await RdDUtility.confirmActorItemDelete(this.getItem(event), this.actor));
    this.html.find('.item-quantite-plus').click(async event => await this.actor.itemQuantiteIncDec(this.getItemId(event), 1));
    this.html.find('.item-quantite-moins').click(async event => await this.actor.itemQuantiteIncDec(this.getItemId(event), -1));

    this.html.find('.creer-un-objet').click(async event => await this.selectObjetTypeToCreate())
    this.html.find('.nettoyer-conteneurs').click(async event => await this.actor.nettoyerConteneurs())

    this.html.find('.vue-detaillee').click(async event => {
      this.options.vueDetaillee = !this.options.vueDetaillee
      this.render(true)
    });
  }

  itemActionEdit(event) {
    const item = this.getItem(event);
    return item?.sheet.render(true);
  }

  _rechercherKeyup(event) {
    const currentTarget = event.currentTarget;
    const nouvelleRecherche = this._optionRecherche(currentTarget);
    if (this.options.recherche?.text != nouvelleRecherche?.text) {
      this.options.recherche = nouvelleRecherche;
      if (this.timerRecherche) {
        clearTimeout(this.timerRecherche);
      }
      this.timerRecherche = setTimeout(() => {
        this.timerRecherche = undefined;
        this.render(true);
      }, 500);
    }
  }

  _rechercheSelectArea(field) {
    if (this.options.recherche) {
      field.focus();
      field.setSelectionRange(this.options.recherche.start, this.options.recherche.end);
    }
  }

  getItemId(event) {
    return RdDSheetUtility.getItemId(event);
  }

  getItem(event) {
    return RdDSheetUtility.getItem(event, this.actor);
  }

  _optionRecherche(target) {
    if (!target.value?.length) {
      return undefined;
    }
    return {
      text: target.value,
      start: target.selectionStart,
      end: target.selectionEnd,
    };
  }
  /* -------------------------------------------- */
  _getHeaderButtons() {
    let buttons = super._getHeaderButtons();
    buttons.unshift({
      class: "montrer",
      icon: "fas fa-comment",
      onclick: ev => this.actor.postActorToChat()
    });
    return buttons
  }

  /* -------------------------------------------- */
  async _onDropItem(event, dragData) {
    const destItemId = this.html.find(event.target)?.closest('.item').attr('data-item-id')
    const dropParams = await RdDSheetUtility.prepareItemDropParameters(destItemId, this.actor, dragData, this.objetVersConteneur)
    if (dropParams) {
      const callSuper = await this.actor.processDropItem(dropParams)
      if (callSuper) {
        await super._onDropItem(event, dragData)
      }
    }
  }

  /* -------------------------------------------- */
  async selectObjetTypeToCreate() {
    let types = this.getTypesInventaire().sort(Misc.ascending(type => Misc.typeName('Item', type)));
    let content = `<span class="generic-label">Selectionnez le type d'équipement</span><select class="item-type">`;
    for (let typeName of types) {
      content += `<option value="${typeName}">${Misc.typeName('Item', typeName)}</option>`
    }
    content += '</select>';
    let d = new Dialog({
      title: "Créer un équipement",
      content: content,
      buttons: {
        create: {
          icon: '<i class="fas fa-check"></i>',
          label: "Créer l'objet",
          callback: () => this.actor.createItem($(".item-type").val())
        }
      }
    });
    d.render(true);
  }

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

  /** @override */
  setPosition(options = {}) {
    const position = super.setPosition(options);
    const sheetHeader = this.element.find(".sheet-header");
    const sheetTabs = this.element.find(".sheet-tabs");
    const sheetBody = this.element.find(".sheet-body");
    let bodyHeight = position.height - sheetHeader[0].clientHeight;
    if (sheetTabs.length > 0) {
      bodyHeight -= sheetTabs[0].clientHeight;
    }
    sheetBody.css("height", bodyHeight);
    return position;
  }


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

  async splitItem(item) {
    const dialog = await DialogSplitItem.create(item, (item, split) => this._onSplitItem(item, split));
    dialog.render(true);
  }

  async _onSplitItem(item, split) {
    if (split >= 1 && split < item.system.quantite) {
      await item.diminuerQuantite(split);
      const splitItem = foundry.utils.duplicate(item);
      splitItem.system.quantite = split;
      await this.actor.createEmbeddedDocuments('Item', [splitItem])
    }
  }

  vendre(item) {
    item?.proposerVente(this.actor.getQuantiteDisponible(item));
  }

}