import { RdDTextEditor } from "../apps/rdd-text-roll-editor.js";
import { Grammar } from "../grammar.js";
import { ITEM_TYPES } from "../constants.js";
import { RdDSheetUtility } from "../rdd-sheet-utility.js";
import { RdDBaseActorSheet } from "./base-actor-sheet.js";

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

  /** @override */
  static get defaultOptions() {
    return foundry.utils.mergeObject(RdDBaseActorSheet.defaultOptions, {
      width: 550
    });
  }

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

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

    this.html.find('.button-encaissement').click(async event => this.actor.encaisser())
    this.html.find('.roll-carac').click(async event => {
      this.actor.rollCarac(Grammar.toLowerCaseNoAccent(event.currentTarget.attributes['data-carac-name'].value))})
    this.html.find('.roll-competence').click(async event => this.actor.rollCompetence(RdDSheetUtility.getItemId(event)));
    this.html.find('.endurance-plus').click(async event => this.actor.santeIncDec("endurance", 1));
    this.html.find('.endurance-moins').click(async event => this.actor.santeIncDec("endurance", -1));

    if (game.user.isGM) {
      this.html.find('.button-remise-a-neuf').click(async event => this.actor.remiseANeuf())
      this.html.find('.delete-active-effect').click(async event => this.actor.removeEffect(this.html.find(event.currentTarget).parents(".active-effect").data('effect')));
      this.html.find('.enlever-tous-effets').click(async event => await this.actor.removeEffects());
    }
    this.html.find('.competence-add').click(async event =>
      await this.actor.createEmbeddedDocuments("Item", [{
        type: ITEM_TYPES.competencecreature,
        name: 'Nouvelle competence',
        img: 'systems/foundryvtt-reve-de-dragon/icons/compcreature-serres.webp',
        system: {
          carac_value: this.actor.getForce(),
        }
      }], { renderSheet: true })
    )
    this.html.find('.roll-text').click(async event => await RdDTextEditor.rollText(event, this.actor))
    this.html.find('.chat-roll-text').click(async event => await RdDTextEditor.chatRollText(event))


    if (this.options.vueDetaillee) {
      // On carac change
      this.html.find('.carac-value').change(async event => {
        let caracName = event.currentTarget.name.replace(".value", "").replace("system.carac.", "");
        this.actor.updateCarac(caracName, parseInt(event.target.value));
      });
      // On competence change
      this.html.find('.competence-value').change(async event => {
        let compName = event.currentTarget.attributes.compname.value;
        //console.log("Competence changed :", compName);
        this.actor.updateCompetence(compName, parseInt(event.target.value));
      });
    }
  }

}