import { ExperienceLog, XP_TOPIC } from "../actor/experience-log.js";
import { ChatUtility } from "../chat-utility.js";
import { renderTemplate } from "../constants.js";
import { Poetique } from "../poetique.js";
import { RdDDice } from "../rdd-dice.js";
import { ReglesOptionnelles } from "../settings/regles-optionnelles.js";
import { TMRUtility } from "../tmr-utility.js";

export class EffetsRencontre {

  static messager = async (dialog, context) => {
    dialog.setRencontreState('messager', TMRUtility.getTMRPortee(context.tmr.coord, context.rencontre.system.force));
  }

  static passeur = async (dialog, context) => {
    dialog.setRencontreState('passeur', TMRUtility.getTMRPortee(context.tmr.coord, context.rencontre.system.force));
  }

  static teleportation_typecase = async (dialog, context) => {
    dialog.setRencontreState('changeur', TMRUtility.getCasesType(context.tmr.type));
  }

  static rencontre_persistante = async (dialog, context) => {
    dialog.setRencontreState('persistant', []);
  }

  static reve_plus_force = async (dialog, context) => { await EffetsRencontre.$reve_plus(context.actor, context.rencontre.system.force) }
  static reve_plus_1 = async (dialog, context) => { await EffetsRencontre.$reve_plus(context.actor, 1) }
  static reve_moins_force = async (dialog, context) => { await EffetsRencontre.$reve_plus(context.actor, -context.rencontre.system.force) }
  static reve_moins_1 = async (dialog, context) => { await EffetsRencontre.$reve_plus(context.actor, -1) }
  static $reve_plus = async (actor, reve) => { await actor.reveActuelIncDec(reve) }

  static vie_moins_1 = async (dialog, context) => { await EffetsRencontre.$vie_plus(context.actor, -1) }
  static vie_moins_force = async (dialog, context) => { await EffetsRencontre.$vie_plus(context.actor, -context.rencontre.system.force) }
  static $vie_plus = async (actor, valeur) => { await actor.santeIncDec("vie", valeur) }

  static moral_plus_1 = async (dialog, context) => { await EffetsRencontre.$vie_plus(context.actor, 1) }
  static moral_moins_1 = async (dialog, context) => { await EffetsRencontre.$vie_plus(context.actor, -1) }
  static $moral_plus = async (actor, valeur) => { await actor.moralIncDec(valeur) }

  static end_moins_1 = async (dialog, context) => { await EffetsRencontre.$vie_plus(context.actor, -1) }
  static end_moins_force = async (dialog, context) => { await EffetsRencontre.$vie_plus(context.actor, -context.rencontre.system.force) }
  static $end_plus = async (actor, valeur) => { await actor.santeIncDec("endurance", valeur) }

  static fatigue_plus_1 = async (dialog, context) => { await EffetsRencontre.$fatigue_plus(context.actor, 1) }
  static fatigue_plus_force = async (dialog, context) => { await EffetsRencontre.$fatigue_plus(context.actor, context.rencontre.system.force) }
  static fatigue_moins_1 = async (dialog, context) => { await EffetsRencontre.$fatigue_plus(context.actor, -1) }
  static fatigue_moins_force = async (dialog, context) => { await EffetsRencontre.$fatigue_plus(context.actor, -context.rencontre.system.force) }
  static $fatigue_plus = async (actor, valeur) => { await actor.santeIncDec("fatigue", valeur) }

  static perte_chance = async (dialog, context) => {
    const perte = context.rolled.isETotal ? context.rencontre.system.force : 1;
    await context.actor.chanceActuelleIncDec("fatigue", -perte);
  }

  static xp_sort_force = async (dialog, context) => {
    let competence = context.competence;
    if (competence) {
      const fromXpSort = Number(competence.system.xp_sort);
      const toXpSort = fromXpSort + context.rencontre.system.force;
      await this.updateEmbeddedDocuments("Item", [{ _id: competence.id, 'system.xp_sort': toXpSort }]);
      await ExperienceLog.add(this, XP_TOPIC.XPSORT, fromXpSort, toXpSort, `${competence.name} - ${context.rencontre.name} en TMR`);
    }
  }

  static stress_plus_1 = async (dialog, context) => {
    await context.actor.addCompteurValue('stress', 1, `Rencontre d'un ${context.rencontre.name} en TMR`);
  }

  static reinsertion = async (dialog, context) => {
    await EffetsRencontre.$reinsertion(dialog, context.actor, it => true)
  }

  static teleportation_aleatoire_typecase = async (dialog, context) => {
    await EffetsRencontre.$reinsertion(dialog, context.actor, it => it.type == context.tmr.type && it.coord != context.tmr.coord)
  }

  static demireve_rompu = async (dialog, context) => {
    dialog.close()
  }

  static sort_aleatoire = async (dialog, context) => {
    context.sortReserve = await RdDDice.rollOneOf(context.actor.itemTypes['sortreserve']);
    if (context.sortReserve) {
      context.newTMR = TMRUtility.getTMR(context.sortReserve.system.coord);
      await dialog.positionnerDemiReve(context.newTMR.coord);
      await dialog.processSortReserve(context.sortReserve);
      dialog.close();
    }
    else {
      await EffetsRencontre.$reinsertion(dialog, context.actor, it => true);
    }
  }

  static deplacement_aleatoire = async (dialog, context) => {
    const oldCoord = context.actor.system.reve.tmrpos.coord;
    const newTmr = await TMRUtility.deplaceTMRAleatoire(context.actor, oldCoord);
    await dialog.positionnerDemiReve(newTmr.coord)
  }

  static rdd_part_tete = async (dialog, context) => {
    foundry.utils.mergeObject(context, {
      tete: context.rolled.isPart,
      poesie: await Poetique.getExtrait()
    })
    await ChatMessage.create({
      whisper: ChatUtility.getOwners(context.actor),
      content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-resultat-reve-de-dragon.hbs`, context)
    });
  }

  static rdd_echec_queue = async (dialog, context) => {
    foundry.utils.mergeObject(context, {
      queues: [await context.actor.ajouterQueue()],
      poesie: await Poetique.getExtrait()
    })
    if (context.rolled.isETotal) {
      context.queues.push(await context.actor.ajouterQueue());
    }

    await ChatMessage.create({
      whisper: ChatUtility.getOwners(context.actor),
      content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-resultat-reve-de-dragon.hbs`, context)
    });
  }

  static experience_particuliere = async (dialog, context) => {
    await context.actor.ajoutExperience(context)
  }

  static regain_seuil = async (dialog, context) => {
    await context.actor.recuperationSeuilReve()
  }

  static async $reinsertion(dialog, actor, filter) {
    const newTMR = await TMRUtility.getTMRAleatoire(filter);
    await actor.forcerPositionTMRInconnue(newTMR);
    await dialog.positionnerDemiReve(newTMR.coord);
  }

}
