import { MournbladeUtility } from "./mournblade-utility.js";

export class MournbladeRollDialog extends Dialog {

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

    let options = { classes: ["MournbladeDialog"], width: 340, height: 'fit-content', 'z-index': 99999 };
    let html = await foundry.applications.handlebars.renderTemplate('systems/fvtt-mournblade/templates/roll-dialog-generic.html', rollData);

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

  /* -------------------------------------------- */
  constructor(actor, rollData, html, options, close = undefined) {
    let conf = {
      title: "Test de Capacité",
      content: html,
      buttons: {
        rolld10: {
            icon: '<i class="fas fa-check"></i>',
            label: "Lancer 1d10",
            callback: () => { this.roll("1d10") }
          },
          rolld20: {
            icon: '<i class="fas fa-check"></i>',
            label: "Lancer 1d20",
            callback: () => { this.roll("1d20") }
          },
          cancel: {
            icon: '<i class="fas fa-times"></i>',
            label: "Annuler",
            callback: () => { this.close() }
        } },
      close: close
    }

    super(conf, options);

    this.actor = actor
    this.rollData = rollData
  }

  /* -------------------------------------------- */
  roll ( dice) {
    this.rollData.mainDice = dice
    MournbladeUtility.rollMournblade( this.rollData )
  }


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

    function onLoad() {
    }
    $(function () { onLoad(); });

    html.find('.apply-modifier').change(async (event) =>  {
      let modifierIdx = $(event.currentTarget).data("modifier-idx")
      let modifier = this.rollData.modifiers[modifierIdx]
      modifier.system.apply = event.currentTarget.checked
    })

    html.find('#modificateur').change(async (event) =>  {
      this.rollData.modificateur = Number(event.currentTarget.value)
    })
    html.find('#typeAttaque').change(async (event) =>  {
      this.rollData.typeAttaque = String(event.currentTarget.value)
    })
    html.find('#difficulte').change(async (event) =>  {
      this.rollData.difficulte = Number(event.currentTarget.value)
    })
    html.find('#attrKey').change(async (event) =>  {
      this.rollData.attrKey = String(event.currentTarget.value)
    })
    html.find('#runemode').change(async (event) =>  {
      this.rollData.runemode = String(event.currentTarget.value)
    })
    html.find('#runeame').change(async (event) =>  {
      this.rollData.runeame = Number(event.currentTarget.value)
    })
    html.find('#isMonte').change(async (event) =>  {
      this.rollData.desavantages.isMonte = event.currentTarget.checked
    })

    html.find('#cibleausol').change(async (event) =>  {
      this.rollData.desavantages.cibleausol = event.currentTarget.checked
    })
    html.find('#cibledesarmee').change(async (event) =>  {
      this.rollData.desavantages.cibledesarmee = event.currentTarget.checked
    })
    html.find('#ciblerestreint').change(async (event) =>  {
      this.rollData.desavantages.ciblerestreint = event.currentTarget.checked
    })
    html.find('#cibleimmobilisée').change(async (event) =>  {
      this.rollData.desavantages.cibleimmobilisée = event.currentTarget.checked
    })
    html.find('#ciblesurplomb').change(async (event) =>  {
      this.rollData.desavantages.ciblesurplomb = event.currentTarget.checked
    })

    html.find('#doubleD20').change(async (event) =>  {
      this.rollData.doubleD20 = event.currentTarget.checked
    })
    html.find('#visee').change(async (event) =>  {
      this.rollData.visee = event.currentTarget.checked
    })
    html.find('#cibleconsciente').change(async (event) =>  {
      this.rollData.cibleconsciente = event.currentTarget.checked
    })
    html.find('#ciblecourt').change(async (event) =>  {
      this.rollData.ciblecourt = event.currentTarget.checked
    })
    html.find('#typeCouvert').change(async (event) =>  {
      this.rollData.typeCouvert = String(event.currentTarget.value)
    })

  }
}