"use strict";
(function () {
  let
    control_addAdditionalPrincipalButton = document.getElementById("AddAdditionalPrincipalButton"),
    control_additionalPrincipalsList = document.getElementById("AdditionalPrincipalsList"),
    control_additionalPrincipalsValidation = document.getElementById("AdditionalPrincipalsValidation"),
    control_addWitnessButton = document.getElementById("AddWitnessButton"),
    control_currentUserEmail = document.getElementById("CurrentUserEmail"),
    control_documentFile = document.getElementById("DocumentFile"),
    control_isConfirmed = document.getElementById("IsConfirmed"),
    control_newPrincipalEmail = document.getElementById("NewPrincipalEmail"),
    control_newWitnessEmail = document.getElementById("NewWitnessEmail"),
    control_participantItemTemplate = document.getElementById("ParticipantItemTemplate"),
    control_participantsJson = document.getElementById("ParticipantsJson"),
    control_uploadDocumentForm = document.getElementById("UploadDocumentForm"),
    control_witnessesList = document.getElementById("WitnessesList"),
    control_witnessesValidation = document.getElementById("WitnessesValidation");

  let signatories = [];

  function _addSignatory(control_email, signatoryType, control_signatoryList) {
    let email = control_email.value;

    if (!email) {
      return "Email is required.";
    }

    if (control_currentUserEmail.value == email) {
      return "Email must not be the same as the currently logged in user.";
    }

    if (signatories.find(x => x.email === email)) {
      return "Email already exists.";
    }

    signatories.push({
      email: email,
      type: signatoryType
    });

    let liTemplate = control_participantItemTemplate.cloneNode(true);
    let li = liTemplate.content;
    li.querySelector(".participantitem__li").setAttribute("data-uid", "");
    li.querySelector(".participantitem__email").textContent = email;

    let deleteButton = li.querySelector(".participantitem__delete");
    deleteButton.addEventListener("click", _deleteParticipant);
    deleteButton.control_signatoryList = control_signatoryList;
    control_signatoryList.appendChild(li);

    control_email.value = "";
  }

  function _deleteParticipant(sender) {
    let li = sender.target.closest("li");
    let email = li.querySelector(".participantitem__email").textContent;
    jfa.components.dialog.confirm({
      message: "Are you sure you want to remove this participant?",
      callbackyes: function () {
        signatories = signatories.filter(m => m.email != email);
      }
    });
  }

  function _bindEvents() {
    control_addAdditionalPrincipalButton.addEventListener("click", () => {
      let err = _addSignatory(control_newPrincipalEmail, 'Principal', control_additionalPrincipalsList);
      control_additionalPrincipalsValidation.textContent = err;
    });
    control_addWitnessButton.addEventListener("click", () => {
      let err = _addSignatory(control_newWitnessEmail, 'Witness', control_witnessesList);
      control_witnessesValidation.textContent = err;
    });
    control_uploadDocumentForm.addEventListener("submit", _onSubmitForm);
  }

  function _init() {
    signatories = JSON.parse(control_participantsJson.value)?.map(p => ({
      email: p.Email,
      type: p.Type,
      uid: p.UID
    }));

    _bindEvents();
  }

  function _onSubmitForm(event) {
    control_participantsJson.value = JSON.stringify(signatories);
    if (control_documentFile.files.length === 0 || !control_isConfirmed.checked) {
      event.preventDefault();
      return false;
    }
    return true;
  }

  _init();
})();