import { HtmlUtility } from "./html-utility.js";

export class DialogItemVente extends Dialog {

  static async display({ item, callback, quantiteMax = undefined }) {
    const quantite = quantiteMax ?? item.getQuantite() ?? 1;
    const isOwned = item.parent;
    const venteData = {
      item: item,
      alias: item.actor?.name ?? game.user.name,
      vendeurId: item.actor?.id,
      prixOrigine: item.calculerPrixCommercant(),
      prixUnitaire: item.calculerPrixCommercant(),
      prixLot: item.calculerPrixCommercant(),
      tailleLot: 1,
      quantiteNbLots: quantite,
      quantiteMaxLots: quantite,
      quantiteMax: quantite,
      quantiteIllimite: item.isItemCommerce() ? quantiteMax == undefined : !isOwned,
      isOwned: isOwned,
    };
    const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-vente.html`, venteData);
    return new DialogItemVente(venteData, html, callback).render(true);
  }

  constructor(venteData, html, callback) {
    let options = { classes: ["dialogvente"], width: 400, height: 'fit-content', 'z-index': 99999 };

    let conf = {
      title: "Proposer",
      content: html,
      default: "proposer",
      buttons: { "proposer": { label: "Proposer", callback: it => { this.onProposer(it); } } }
    };

    super(conf, options);
    this.callback = callback;
    this.venteData = venteData;
  }

  activateListeners(html) {
    super.activateListeners(html);

    this.html = html;
    this.html.find(".tailleLot").change(event => this.setTailleLot(Number(event.currentTarget.value)));
    this.html.find(".quantiteNbLots").change(event => this.setNbLots(Number(event.currentTarget.value)));
    this.html.find(".quantiteIllimite").change(event => this.setQuantiteIllimite(event.currentTarget.checked));
    this.html.find(".prixLot").change(event => this.setPrixLot(Number(event.currentTarget.value)));

    this.setQuantiteIllimite(this.venteData.quantiteIllimite);
  }

  async onProposer(it) {
    this.updateVente(this.getChoixVente());
    this.callback(this.venteData);
  }

  updateVente(update) {
    mergeObject(this.venteData, update);
  }

  getChoixVente() {
    return {
      quantiteNbLots: Number(this.html.find(".quantiteNbLots").val()),
      tailleLot: Number(this.html.find(".tailleLot").val()),
      quantiteIllimite: this.html.find(".quantiteIllimite").is(':checked'),
      prixLot: Number(this.html.find(".prixLot").val())
    };
  }

  /* -------------------------------------------- */
  setPrixLot(prixLot) {
    this.venteData.prixLot = prixLot;
  }

  setTailleLot(tailleLot) {
    const maxLots = Math.floor(this.venteData.quantiteMax / tailleLot);
    this.updateVente({
      tailleLot,
      quantiteNbLots: Math.min(maxLots, this.venteData.quantiteNbLots),
      quantiteMaxLots: maxLots,
      prixLot: (tailleLot * this.venteData.prixOrigine).toFixed(2)
    });

    this.html.find(".prixLot").val(this.venteData.prixLot);
    this.html.find(".quantiteNbLots").val(this.venteData.quantiteNbLots);
    this.html.find(".quantiteNbLots").attr("max", this.venteData.quantiteMaxLots)
  }

  setNbLots(nbLots) {
    this.updateVente({
      quantiteNbLots: this.venteData.isOwned ? Math.max(0, Math.min(nbLots, this.venteData.quantiteMaxLots)) : nbLots
    })
    this.html.find(".quantiteNbLots").val(this.venteData.quantiteNbLots);
  }

  setQuantiteIllimite(checked) {
    this.updateVente({ quantiteIllimite: checked })
    this.html.find(".label-quantiteIllimite").text(this.venteData.quantiteIllimite ? "Illimités" : "disponibles");
    HtmlUtility.showControlWhen(this.html.find(".quantiteNbLots"), !this.venteData.quantiteIllimite)
  }
}