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

export class MournbladeRollDialog extends Application {

  /* -------------------------------------------- */
  static async create(actor, rollData ) {
    return new MournbladeRollDialog(actor, rollData);
  }

  /* -------------------------------------------- */
  constructor(actor, rollData, options = {}) {
    super(options);
    this.actor = actor;
    this.rollData = rollData;
  }

  static get defaultOptions() {
    return foundry.utils.mergeObject(super.defaultOptions, {
      classes: ["fvtt-mournblade", "sheet", "item"],
      template: "systems/fvtt-mournblade/templates/roll-dialog-generic.hbs",
      width: 400,
      height: "auto",
      title: "Test de Capacité"
    });
  }

  getData() {
    const data = foundry.utils.duplicate(this.rollData);
    if (!data.config) {
      data.config = game.system.mournblade.config;
    }
    return data;
  }

  _onCancel() {
    this.close();
  }

  /* -------------------------------------------- */
  _onRoll(dice) {
    this.rollData.mainDice = dice;
    MournbladeUtility.rollMournblade(this.rollData);
    this.close();
  }

  /* -------------------------------------------- */
  activateListeners(html) {
    super.activateListeners(html);
    
    // Roll buttons
    html.find(".rolld10").click(this._onRoll.bind(this, "1d10"));
    html.find(".rolld20").click(this._onRoll.bind(this, "1d20"));
    html.find(".cancel").click(this._onCancel.bind(this));

    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)
    })

  }
}