class CorpusDisplay extends RessourceDisplay {
  constructor(displayElement) {
    super(displayElement);
    this.corpusId = displayElement.dataset.corpusId;
  }

  init(user) {
    let corpus;

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

  usersPatchHandler(patch) {
    let filteredPatch;
    let operation;
    let re;

    re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}`);
    filteredPatch = patch.filter(operation => re.test(operation.path));
    for (operation of filteredPatch) {
      switch(operation.op) {
        case 'replace':
          re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/last_edited_date$`);
          if (re.test(operation.path)) {
            this.setLastEditedDate(operation.value);
            break;
          }
          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.users[this.userId].corpora[this.corpusId].max_num_tokens}`
    );
  }

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

  setStatus(status) {
    let element;
    let elements;

    elements = this.displayElement.querySelectorAll('.corpus-analyse-trigger')
    for (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('.corpus-build-trigger');
    for (element of elements) {
      if (status === 'UNPREPARED' && Object.values(app.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 (element of elements) {
      element.dataset.corpusStatus = status;
    }
    elements = this.displayElement.querySelectorAll('.corpus-status-spinner');
    for (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")
    );
  }

  setLastEditedDate(lastEditedDate) {
    this.setElements(
      this.displayElement.querySelectorAll('.corpus-end-date'),
      new Date(lastEditedDate).toLocaleString("en-US")
    );
  }
}