
export class DialogChateauDormant extends Dialog {

  static async create() {
    const date = game.system.rdd.calendrier.dateCourante();
    const actors = game.actors.filter(actor => actor.hasPlayerOwner && actor.isPersonnage());

    const dialogData = {
      actors: actors,
      date: date,
      motifStress: `Nuit du ${date}`,
      finChateauDormant: game.system.rdd.calendrier.getTimestampFinChateauDormant()
    };
    const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/sommeil/dialog-chateau-dormant.hbs",
      dialogData);

    new DialogChateauDormant(dialogData, html)
      .render(true);
  }

  constructor(dialogData, html) {
    const options = {
      classes: ["rdd-dialog-chateau-dormant"],
      width: 600,
      height: 'fit-content',
      'z-index': 99999
    };
    const conf = {
      title: "De Chateau dormant à Vaisseau",
      content: html,
      buttons: {
        chateauDormant: { label: "Passer à Vaisseau!", callback: it => { this.onChateauDormant(); } }
      }
    };
    super(conf, options);
    this.dialogData = dialogData;
  }

  activateListeners(html) {
    super.activateListeners(html);
    this.html = html;
    this.html.find('input.sommeil-insomnie').change(event => this.onInsomnie(event));
    this._activateListenerOnActorMoral(this.html);
  }

  _activateListenerOnActorMoral(html) {
    html.find(`span.sommeil-actor-moral a`).click(event => this.onActorMoral(event));
  }

  onInsomnie(event) {
    const sommeilInsomnie = this.html.find(event.currentTarget);
    const isInsomnie = sommeilInsomnie.is(':checked');
    const sommeilHeures = sommeilInsomnie.parents('.set-sommeil-actor').find('input.sommeil-heures');
    sommeilHeures.prop('disabled', isInsomnie);
    if (isInsomnie) {
      sommeilHeures.val('0');
    }
  }

  async onActorMoral(event) {
    const selected = this.html.find(event.currentTarget);
    const actorRow = selected.parents('.set-sommeil-actor');
    const actorId = actorRow.data('actor-id');
    const actor = this.getActor(actorId);
    actor.system.sommeil.moral = selected.data('moral');
    const htmlMoral = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/sommeil/sommeil-actor-moral.hbs', actor.system.sommeil)
    actorRow.find('.sommeil-actor-moral').html(htmlMoral);
    // re-attach listeners for actor row
    this._activateListenerOnActorMoral(actorRow);
  }

  getActor(actorId) {
    return this.dialogData.actors.find(it => it.id == actorId);
  }

  async onChateauDormant() {
    const motifStress = this.html.find("form input[name='motifStress']").val();
    jQuery.map(
      this.html.find('li.set-sommeil-actor'),
      it => this.extractConsigneActor(this.html.find(it), motifStress)
    ).forEach(async consigne => await consigne.actor.prepareChateauDormant(consigne))
  }


  extractConsigneActor(actorRow, motifStress) {
    const actorId = actorRow.data('actor-id');
    const actor = this.getActor(actorId);
    const insomnie = actorRow.find('input.sommeil-insomnie').is(':checked');
    return {
      actor,
      ignorer: actorRow.find('input.sommeil-ignorer').is(':checked'),
      stress: {
        motif: motifStress,
        valeur: Number.parseInt(actorRow.find('input.sommeil-stress').val()),
      },
      sommeil: {
        nouveaujour: true,
        date: this.dialogData.finChateauDormant,
        insomnie: insomnie,
        heures: insomnie ? 0 : Number.parseInt(actorRow.find('input.sommeil-heures').val()),
        moral: actor.system.sommeil.moral ?? 'neutre',
      }
    };
  }
}