let corpusId = {{ corpus.hashid|tojson }};
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));

// #region Publishing
let publishingModalIsPublicSwitchElement = document.querySelector('#publishing-modal-is-public-switch');
publishingModalIsPublicSwitchElement.addEventListener('change', (event) => {
  let newIsPublic = publishingModalIsPublicSwitchElement.checked;
  Requests.corpora.entity.isPublic.update(corpusId, newIsPublic)
    .catch((response) => {
      publishingModalIsPublicSwitchElement.checked = !newIsPublic;
    });
});
// #endregion Publishing

// #region Delete
let deleteModalDeleteButtonElement = document.querySelector('#delete-modal-delete-button');
deleteModalDeleteButtonElement.addEventListener('click', (event) => {
  Requests.corpora.entity.delete(corpusId)
    .then((response) => {
      window.location.href = {{ url_for('main.dashboard')|tojson }};
    });
});
// #endregion Delete

// #region Invite users
let inviteUserModalElement = document.querySelector('#invite-user-modal');
let inviteUserModalSearchElement = document.querySelector('#invite-user-modal-search');
let inviteUserModalInviteButtonElement = document.querySelector('#invite-user-modal-invite-button');
const users = {};

for (let user of {{ users|tojson }}) {
  users[user.username] = user.avatar ? `/users/${user.id}/avatar` : '/static/images/user_avatar.png';
}

let inviteUserModalSearch = M.Chips.init(
  inviteUserModalSearchElement,
  {
    autocompleteOptions: {
      data: users
    },
    limit: 3,
    onChipAdd: (a, chipElement) => {
      if (!(chipElement.firstChild.data in inviteUserModalSearch.autocomplete.options.data)) {
        chipElement.firstElementChild.click();
      }
    },
    placeholder: 'Enter username',
    secondaryPlaceholder: 'Add more users'
  }
);

M.Modal.init(
  inviteUserModalElement,
  {
    onOpenStart: (modalElement, modalTriggerElement) => {
      while (inviteUserModalSearch.chipsData.length > 0) {
        inviteUserModalSearch.deleteChip(0);
      }
    }
  }
)

inviteUserModalInviteButtonElement.addEventListener('click', (event) => {
  let usernames = inviteUserModalSearch.chipsData.map((chipData) => chipData.tag);
  Requests.corpora.entity.followers.add(corpusId, usernames);
});
// #endregion Invite users

// #region Share link
let shareLinkModalElement = document.querySelector('#share-link-modal');
let shareLinkModalCorpusFollowerRoleSelectElement = document.querySelector('#share-link-modal-corpus-follower-role-select');
let shareLinkModalExpirationDateDatepickerElement = document.querySelector('#share-link-modal-expiration-date-datepicker');
let shareLinkModalCreateButtonElement = document.querySelector('#share-link-modal-create-button');
let shareLinkModalOutputContainerElement = document.querySelector('#share-link-modal-output-container');
let shareLinkModalOutputFieldElement = document.querySelector('#share-link-modal-output-field');
let shareLinkModalOutputCopyButtonElement = document.querySelector('#share-link-modal-output-copy-button');

let today = new Date();
let tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
let oneWeekLater = new Date();
oneWeekLater.setDate(today.getDate() + 7);
let fourWeeksLater = new Date();
fourWeeksLater.setDate(today.getDate() + 28);

M.Datepicker.init(
  shareLinkModalExpirationDateDatepickerElement,
  {
    container: document.querySelector('main'),
    defaultDate: oneWeekLater,
    setDefaultDate: true,
    minDate: tomorrow,
    maxDate: fourWeeksLater
  }
);

M.Modal.init(
  shareLinkModalElement,
  {
    onOpenStart: (modalElement, modalTriggerElement) => {
      shareLinkModalOutputFieldElement.value = '';
      shareLinkModalOutputContainerElement.classList.add('hide');
    }
  }
)

shareLinkModalCreateButtonElement.addEventListener('click', (event) => {
  let role = shareLinkModalCorpusFollowerRoleSelectElement.value;
  let expiration = shareLinkModalExpirationDateDatepickerElement.value
  Requests.corpora.entity.generateShareLink(corpusId, role, expiration)
    .then((response) => {
      response.json()
        .then((json) => {
          shareLinkModalOutputContainerElement.classList.remove('hide');
          shareLinkModalOutputFieldElement.value = json.corpusShareLink;
        });
    });
});

shareLinkModalOutputCopyButtonElement.addEventListener('click', (event) => {
  navigator.clipboard.writeText(shareLinkModalOutputFieldElement.value)
    .then(
      () => {app.flash('Copied!');},
      () => {app.flash('Could not copy to clipboard. Please copy manually.', 'error');}
    );
  
});
// #endregion Share link
