class CorpusDisplay extends ResourceDisplay {
  constructor(displayElement) {
    super(displayElement);
    this.corpusId = displayElement.dataset.corpusId;
    this.displayElement
      .querySelector('.action-button[data-action="build-request"]')
      .addEventListener('click', (event) => {
        Requests.corpora.entity.build(this.corpusId);
      });
  }

  init(user) {
    let corpus = user.corpora[this.corpusId];
    this.setCreationDate(corpus.creation_date);
    this.setDescription(corpus.description);
    this.setStatus(corpus.status);
    this.setTitle(corpus.title);
    this.setNumTokens(corpus.num_tokens);
  }

  onPatch(patch) {
    let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}`);
    let filteredPatch = patch.filter(operation => re.test(operation.path));
    for (let operation of filteredPatch) {
      switch(operation.op) {
        case 'remove': {
          let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}$`);
          if (re.test(operation.path)) {
            window.location.href = '/dashboard#corpora';
          }
          break;
        }
        case 'replace': {
          re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/num_tokens`);
          if (re.test(operation.path)) {
            this.setNumTokens(operation.value);
            break;
          }
          re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/status$`);
          if (re.test(operation.path)) {
            this.setStatus(operation.value);
            break;
          }
          break;
        }
        default: {
          break;
        }
      }
    }
  }

  setTitle(title) {
    this.setElements(this.displayElement.querySelectorAll('.corpus-title'), title);
  }

  setNumTokens(numTokens) {
    this.setElements(
      this.displayElement.querySelectorAll('.corpus-token-ratio'),
      `${numTokens}/${app.data.users[this.userId].corpora[this.corpusId].max_num_tokens}`
    );
  }

  setDescription(description) {
    this.setElements(this.displayElement.querySelectorAll('.corpus-description'), description);
  }

  setStatus(status) {
    let elements = this.displayElement.querySelectorAll('.action-button[data-action="analyze"]');
    for (let element of elements) {
      if (['BUILT', 'STARTING_ANALYSIS_SESSION', 'RUNNING_ANALYSIS_SESSION', 'CANCELING_ANALYSIS_SESSION'].includes(status)) {
        element.classList.remove('disabled');
      } else {
        element.classList.add('disabled');
      }
    }
    elements = this.displayElement.querySelectorAll('.action-button[data-action="build-request"]');
    for (let element of elements) {
      if (['UNPREPARED', 'FAILED'].includes(status) && Object.values(app.data.users[this.userId].corpora[this.corpusId].files).length > 0) {
        element.classList.remove('disabled');
      } else {
        element.classList.add('disabled');
      }
    }
    elements = this.displayElement.querySelectorAll('.corpus-status');
    for (let element of elements) {
      element.dataset.status = status;
    }
    elements = this.displayElement.querySelectorAll('.corpus-status-spinner');
    for (let element of elements) {
      if (['SUBMITTED', 'QUEUED', 'BUILDING', 'STARTING_ANALYSIS_SESSION', 'CANCELING_ANALYSIS_SESSION'].includes(status)) {
        element.classList.remove('hide');
      } else {
        element.classList.add('hide');
      }
    }
  }

  setCreationDate(creationDate) {
    this.setElements(
      this.displayElement.querySelectorAll('.corpus-creation-date'),
      new Date(creationDate).toLocaleString("en-US")
    );
  }
}