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

export class HeritiersRollDialog extends Dialog {

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

    let options = { classes: ["HeritiersDialog"], width: 420, 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 buttons = {
      rolld8: {
        icon: '<i class="fas fa-check"></i>',
        label: "Lancer 1d8",
        callback: () => { this.roll("d8") }
      }
    }
    let enableD10 = false
    let enableD12 = false
    if (rollData.mode == "pouvoir" || rollData.competence?.system.niveau > 0) {
      enableD10 = true
    }
    if (rollData.mode == "pouvoir" || rollData.competence?.system.niveau > 1) {
      enableD12 = true
    }
    if (enableD10) {
      buttons.rolld10 = {
        icon: '<i class="fas fa-check"></i>',
        label: "Lancer 1d10",
        callback: () => { this.roll("d10") }
      }
    }
    if (enableD12) {
        buttons.rolld12 =  {
        icon: '<i class="fas fa-check"></i>',
        label: "Lancer 1d12",
        callback: () => { this.roll("d12") }
      }
    }

    if (rollData.tricherie) {
      buttons["rollTricherie"] = {
        icon: '<i class="fas fa-check"></i>',
        label: "Lancer avec 1 Point de Tricherie",
        callback: () => { this.roll("tricherie") }
      }
    }
    if (rollData.heritage) {
      buttons["rollHeritage"] = {
        icon: '<i class="fas fa-check"></i>',
        label: "Lancer avec 1 Point d'Héritage",
        callback: () => { this.roll("heritage") }
      }
    }
    buttons["Cancel"] = {
      icon: '<i class="fas fa-times"></i>',
      label: "Annuler",
      callback: () => { this.close() }
    }
    let conf = {
      title: "Test de Capacité",
      content: html,
      buttons: buttons,
      close: close
    }
    // Overwrite in case of carac only -> 1d8
    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) {
    if (dice == "heritage") {
      this.rollData.useHeritage = true
    }
    else {
      if (dice == "tricherie") {
        this.rollData.useTricherie = true
      } else {
        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) => {
      //console.log("caracKey", event.currentTarget.value)
      this.rollData.caracKey = String(event.currentTarget.value)
    })
    html.find('#bonus-malus-context').change((event) => {
      this.rollData.bonusMalusContext = Number(event.currentTarget.value)
    })
    html.find('#bonus-attaque-plusieurs').change((event) => {
      this.rollData.bonusAttaquePlusieurs = Number(event.currentTarget.value)
    })
    html.find('#useSpecialite').change((event) => {
      this.rollData.useSpecialite = event.currentTarget.checked
    })
    html.find('#pouvoirPointsUsage').change((event) => {
      this.rollData.pouvoirPointsUsage = Number(event.currentTarget.value)
    })
    html.find('#attaqueDos').change((event) => {
      this.rollData.attaqueDos = event.currentTarget.checked
    })
    html.find('#bonus-attaque-seconde-arme').change((event) => {
      this.rollData.secondeArme = String(event.currentTarget.value)
    })
    html.find('#attaque-cible').change((event) => {
      this.rollData.attaqueCible = String(event.currentTarget.value)
    })
    
  }
}