import { ReglesOptionnelles } from "../settings/regles-optionnelles.js";
import { EffetsDraconiques } from "../tmr/effets-draconiques.js";

export class DialogRepos extends Dialog {

  static async create(actor) {
    if (!actor.isPersonnage()) {
      return
    }
    if (!ReglesOptionnelles.isUsing("chateau-dormant-gardien") || !actor.hasPlayerOwner) {
      actor.system.sommeil = {
        "nouveaujour": true,
        "insomnie": EffetsDraconiques.isSujetInsomnie(actor),
        "moral": "neutre",
        "heures": 4
      }
    }
    const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/sommeil/dialog-repos.html", actor);
    const dialog = new DialogRepos(html, actor);
    dialog.render(true);
  }

  constructor(html, actor) {
    let options = { classes: ["dialog-repos"], width: 400, height: 'fit-content', 'z-index': 99999 };
    let conf = {
      title: "Se reposer",
      content: html,
      default: "repos",
      buttons: {
        "repos": { label: "Se reposer", callback: async it => { this.repos(); } }
      }
    };
    super(conf, options);
    this.actor = actor;
  }
  activateListeners(html) {
    super.activateListeners(html);
    this.html = html;
    this.html.find(`.sommeil-actor-moral a`).click(event => this.onActorMoral(event));
  }
  /* -------------------------------------------- */

  async repos() {
    const selection = await this.html.find("[name='repos']:checked").val();
    switch (selection) {
      case "sieste": return await this.sieste();
      case "nuit": return await this.nuit();
      case "chateau-dormant": return await this.chateauDormant();
      case "gris-reve": return await this.grisReve();
    }
  }

  async grisReve() {
    await this.html.find("[name='nb-jours']").change();
    const nbJours = Number.parseInt(await this.html.find("[name='nb-jours']").val());
    await this.actor.grisReve(nbJours);
  }

  async chateauDormant() {
    await this.actor.dormirChateauDormant();
  }

  async nuit() {
    await this.html.find("[name='sommeil.heures']").change();
    const val = await this.html.find("[name='sommeil.heures']").val();
    const sommeilHeures = Number.parseInt(val ?? '0');
    await this.actor.dormir(sommeilHeures, { chateauDormant: true });
  }

  async sieste() {
    await this.html.find("[name='sieste.heures']").change();
    const siesteHeures = Number.parseInt(await this.html.find("[name='sieste.heures']").val());
    await this.actor.dormir(siesteHeures);
  }

  async onActorMoral(event) {
    const selected = this.html.find(event.currentTarget);
    const parentDiv = selected.parents().find('.sommeil-actor-moral');
    const situationMoral = selected.data('moral');
    await this.actor.setInfoSommeilMoral(situationMoral);
    const htmlMoral = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/sommeil/sommeil-actor-moral.hbs', {
      moral: situationMoral
    });
    parentDiv.html(htmlMoral);
    this.html.find(`.sommeil-actor-moral a`).click(event => this.onActorMoral(event));
  }
}