import { YggdrasillUtility } from "./yggdrasill-utility.js";
const dureeGaldrSD = { "1d5a": 3, "1d10t": 6, "1d10m": 9, "1d10h": 12, "1d5j": 15};
const ciblesGaldrSD = { "1": 3, "2_4": 6, "5_9": 9, "10_49": 12, "50plus": 15};
const zonesciblesGaldrSD = { "INS10cm3": 3, "INS50cm3": 6, "INS1m3": 9, "INS5m3": 12, "INS10m3": 15};

export class YggdrasillRoll extends Dialog {

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

    let html
    let h = 440;
    if ( rollData.mode == "competence") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-competence.html', rollData);
      h = 340;
    } else if (rollData.mode == "carac") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-carac.html', rollData);
      h = 320;
    } else if (rollData.mode == "attribut") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-attribut.html', rollData);
      h = 320;
    } else if (rollData.mode == "armecc") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-armecc.html', rollData);
    } else if (rollData.mode == "sejdr") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-sejdr.html', rollData);
    } else if (rollData.mode == "rune") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-rune.html', rollData);
    } else if (rollData.mode == "galdr") {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-galdr.html', rollData);
    } else {
      html = await renderTemplate('systems/fvtt-yggdrasill/templates/roll-dialog-armetir.html', rollData);
    }
    let options = { classes: ["yggdrasilldialog"], width: 600, height: h, 'z-index': 99999 };
    return new YggdrasillRoll(actor, rollData, html, options );
  }

  /* -------------------------------------------- */
  constructor(actor, rollData, html, options, close = undefined) {
    let conf = {
      title: (rollData.mode == "competence") ? "Compétence" : "Caractéristique",
      content: html,
      buttons: { 
        roll: {
            icon: '<i class="fas fa-check"></i>',
            label: "Lancer le Test",
            callback: () => { this.roll() } 
          },
          cancel: {
            icon: '<i class="fas fa-times"></i>',
            label: "Annuler",
            callback: () => { this.close() }
        } },
      default: "Roll",
      close: close
    }

    super(conf, options);

    this.actor = actor;
    this.rollData = rollData;
  }

  /* -------------------------------------------- */
  roll () {
    if ( this.rollData.mode == "attribut") {
      YggdrasillUtility.rollAttribute(this.rollData)
    } else {
      YggdrasillUtility.rollYggdrasill( this.rollData )
    }
  }
  
  /* -------------------------------------------- */
  updateGaldrSR( ) {
    let sdDuree = Number(dureeGaldrSD[this.rollData.dureeGaldr]);
    let sdVar = 0;
    if ( this.rollData.sort.data.voie == "illusion") {
      sdVar = Number(zonesciblesGaldrSD[this.rollData.zoneGaldr]);
    } else {
      sdVar = Number(ciblesGaldrSD[this.rollData.nbCibles]);
    }
    let SR = Number(this.rollData.sort.data.sd) + sdDuree + sdVar;
    $("#srTotal").text(SR);
    this.rollData.sr = SR;
  }

  /* -------------------------------------------- */
  updateRuneSR() {
    let support = 0;

    this.rollData.dureeRune = 6 - this.rollData.agiliteCarac.value;
    if ( this.rollData.supportRune == "peau") { 
      support = 3;
      this.rollData.echelleDuree = "Actions";
    }
    if ( this.rollData.supportRune == "tissu") { 
      support = 6;
      this.rollData.echelleDuree = "Tours";
    }
    if ( this.rollData.supportRune == "cuir") {
      support = 9;
      this.rollData.echelleDuree = "Minutes";
    }
    if ( this.rollData.supportRune == "bois") {
      support = 12;
      this.rollData.echelleDuree = "Heures";
    }
    if ( this.rollData.supportRune == "pierremetal") {
      support = 15;
      this.rollData.echelleDuree = "Jours";
    }
    let SR = this.rollData.puissanceRune + (Number(this.rollData.sort.data.niveau)*3) + support;
    $("#srTotal").text(SR);
    $("#runeDuree").text( this.rollData.dureeRune + " " + this.rollData.echelleDuree);
    $("#runeDureeVie").text( this.rollData.puissanceRune + " " + this.rollData.echelleDuree);
    this.rollData.sr = SR;
  }

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

    var dialog = this;
    function onLoad() {
      if (dialog.rollData.mode == "competence") {
        let carac = dialog.actor.getCarac( "Puissance" );
        dialog.rollData.selectedCarac = carac;
      } else if (dialog.rollData.mode == "armecc" || dialog.rollData.mode == "armedist" ) {
        $("#caracName").text(dialog.rollData.selectedCarac.label);
        $("#attackDescr").text(dialog.rollData.attackDef.description);
      } else if ( dialog.rollData.mode == "sejdr" || dialog.rollData.mode == "rune" || dialog.rollData.mode == "galdr" ) {
        $("#caracName").text(dialog.rollData.selectedCarac.label);
      }
      if (dialog.rollData.mode == "rune" ) {
        dialog.updateRuneSR();
      }
      if (dialog.rollData.mode == "galdr" ) {
        dialog.updateGaldrSR();
      }
      if (dialog.rollData.mode == "attribut") {
        $("#attrValue").text("2d10+"+dialog.rollData.subAttr.value);
      } else {
        $("#caracValue").text(dialog.rollData.selectedCarac.value+"d10");
      }
    }
    $(function () { onLoad(); });

    html.find('#caracName').change((event) => {
      let caracKey = event.currentTarget.value;
      let carac = this.actor.getCarac( caracKey );
      this.rollData.selectedCarac = carac;
      $("#caracValue").text(carac.value+"d10");
    });

    html.find('#typeAttack').change((event) => {      
      let attackType = event.currentTarget.value;
      let attackDef
      if ( this.rollData.mode == 'armecc') 
        attackDef = this.actor.getAttaqueData( attackType);
      else 
        attackDef = this.actor.getTirData( attackType);
      this.rollData.attackDef = attackDef;
      this.rollData.selectedCarac = attackDef.carac;
      $("#caracValue").text(attackDef.carac.value+"d10");
      $("#caracName").text(attackDef.carac.label);
      $("#attackDescr").text(attackDef.description);
      $("#malus").text(attackDef.malus);
    });

    html.find('#supportRune').change((event) => {
      this.rollData.supportRune = event.currentTarget.value;
      this.updateRuneSR();
    });
    html.find('#puissanceRune').change((event) => {
      this.rollData.puissanceRune = Number(event.currentTarget.value);
      this.updateRuneSR();
    });

    html.find('#dureeGaldr').change((event) => {
      this.rollData.dureeGaldr = event.currentTarget.value;
      this.updateGaldrSR();
    });
    html.find('#nbCibles').change((event) => {
      this.rollData.nbCibles = event.currentTarget.value;
      this.updateGaldrSR();
    });
    html.find('#zoneGaldr').change((event) => {
      this.rollData.zoneGaldr = event.currentTarget.value;
      this.updateGaldrSR();
    });
    
    
    html.find('#bonusMalus').change((event) => {
      this.rollData.bonusMalus = Number(event.currentTarget.value);
    });
    html.find('#furorUsage').change((event) => {
      this.rollData.furorUsage = Number(event.currentTarget.value);
    });
    html.find('#sr').change((event) => {
      this.rollData.sr = Number(event.currentTarget.value);
    });
    html.find('#bonusdefense').change((event) => {
      this.rollData.bonusdefense = Number(event.currentTarget.value);
    });

  }
  
}