import { HeritiersUtility } from "./heritiers-utility.js";

export class HeritiersRollDialog extends Dialog {

  /* -------------------------------------------- */
  static async create(actor, rollData) {

    let options = { classes: ["HeritiersDialog"], width: 320, height: 'fit-content', 'z-index': 99999 };
    let html = await renderTemplate('systems/fvtt-les-heritiers/templates/roll-dialog-generic.html', rollData);

    return new HeritiersRollDialog(actor, rollData, html, options);
  }

  /* -------------------------------------------- */
  constructor(actor, rollData, html, options, close = undefined) {
    let conf = {
      title: "Test de Capacité",
      content: html,
      buttons:
      {
        rolld8: {
          icon: '<i class="fas fa-check"></i>',
          label: "Lancer 1d8",
          callback: () => { this.roll("d8") }
        },
        rolld10: {
          icon: '<i class="fas fa-check"></i>',
          label: "Lancer 1d10",
          callback: () => { this.roll("d10") }
        },
        rolld12: {
          icon: '<i class="fas fa-check"></i>',
          label: "Lancer 1d12",
          callback: () => { this.roll("d12") }
        },
        cancel: {
          icon: '<i class="fas fa-times"></i>',
          label: "Annuler",
          callback: () => { this.close() }
        }
      },
      close: close
    }
    // Overwrite in case of carac only -> 1d10
    if (rollData.mode == "carac") {
      conf.buttons = {
        rolld8: {
          icon: '<i class="fas fa-check"></i>',
          label: "Lancer 1d8",
          callback: () => { this.roll("d8") }
        },
        cancel: {
          icon: '<i class="fas fa-times"></i>',
          label: "Annuler",
          callback: () => { this.close() }
        }
      }
    }
    super(conf, options);

    this.actor = actor
    this.rollData = rollData
  }

  /* -------------------------------------------- */
  roll(dice) {
    this.rollData.mainDice = dice
    HeritiersUtility.rollHeritiers(this.rollData)
  }


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

    var dialog = this;
    function onLoad() {
    }
    $(function () { onLoad(); });

    html.find('#sdValue').change(async (event) => {
      this.rollData.sdValue = Number(event.currentTarget.value)
    })
    html.find('#caracKey').change(async (event) => {
      this.rollData.caracKey = String(event.currentTarget.value)
    })
    html.find('#bonus-malus-context').change((event) => {
      this.rollData.bonusMalusContext = Number(event.currentTarget.value)
    })
    html.find('#useTricherie').change((event) => {
      this.rollData.useTricherie = event.currentTarget.checked
    })
    html.find('#useHeritage').change((event) => {
      this.rollData.useHeritage = event.currentTarget.checked
    })
  }
}