import { RdDItemCompetence } from "./item-competence.js";
import { Misc } from "./misc.js";
import { SYSTEM_SOCKET_ID } from "./constants.js";


/**
 * Extend the base Dialog entity by defining a custom window to perform roll.
 * @extends {Dialog}
 */
export class RdDAstrologieJoueur extends Dialog {

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

    let dialogData = {
      nombres: this.organizeNombres(actor),
      dates: game.system.rdd.calendrier.getJoursSuivants(10),
      etat: actor.getEtatGeneral(),
      ajustementsConditions: CONFIG.RDD.ajustementsConditions,
      astrologie: RdDItemCompetence.findCompetence(actor.items, 'Astrologie')
    }
    const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-astrologie-joueur.html', dialogData);

    const options = { classes: ["rdd-roll-dialog"], width: 600, height: 'fit-content', 'z-index': 99999 };
    const dialog = new RdDAstrologieJoueur(html, actor, dialogData, options);
    dialog.render(true);
  }

  /* -------------------------------------------- */
  constructor(html, actor, dialogData, dialogOptions) {
    const dialogConf = {
      title: "Nombres Astraux",
      content: html,
      default: "saveButton",
      buttons: {
        saveButton: { label: "Fermer", callback: html => {} }
      },
    };
    super(dialogConf, dialogOptions);

    this.actor = actor;
    this.dataNombreAstral = duplicate(dialogData);
  }

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

    this.html.find("[name='diffConditions']").val(0);

    this.html.find('[name="jet-astrologie"]').click((event) => {
      this.requestJetAstrologie();
    });
  }

  /* -------------------------------------------- */
  static organizeNombres(actor) {
    let itemNombres = actor.listItems('nombreastral');
    let itemFiltered = {};
    for (let item of itemNombres) {
      if (itemFiltered[item.system.jourindex]) {
        itemFiltered[item.system.jourindex].listValues.push(item.system.value);
      } else {
        itemFiltered[item.system.jourindex] = {
          listValues: [item.system.value],
          jourlabel: item.system.jourlabel
        }
      }
    }
    return itemFiltered;
  }

  /* -------------------------------------------- */
  requestJetAstrologie() {
    let socketData = {
      id: this.actor.id,
      carac_vue: this.actor.system.carac['vue'].value,
      etat: this.dataNombreAstral.etat,
      astrologie: this.dataNombreAstral.astrologie,
      conditions: this.html.find('[name="diffConditions"]').val(),
      date: this.html.find('[name="joursAstrologie"]').val(),
      userId: game.user.id
    }
    if (Misc.isUniqueConnectedGM()) {
      game.system.rdd.calendrier.requestNombreAstral(socketData);
    } else {
      game.socket.emit(SYSTEM_SOCKET_ID, {
        msg: "msg_request_nombre_astral",
        data: socketData
      });
    }
    this.close();
  }

}