class CorpusFileList extends RessourceList {
  static autoInit() {
    for (let corpusFileListElement of document.querySelectorAll('.corpus-file-list:not(.no-autoinit)')) {
      new CorpusFileList(corpusFileListElement);
    }
  }

  static options = {
    initialHtmlGenerator: (id) => {
      return `
        <div class="input-field">
          <i class="material-icons prefix">search</i>
          <input id="${id}-search" class="search" type="search"></input>
          <label for="${id}-search">Search corpus file</label>
        </div>
        <table>
          <thead>
            <tr>
              <th>Filename</th>
              <th>Author</th>
              <th>Title</th>
              <th>Publishing year</th>
              <th></th>
            </tr>
          </thead>
          <tbody class="list"></tbody>
        </table>
        <ul class="pagination"></ul>
      `.trim();
    },
    item: `
      <tr class="clickable hoverable">
        <td><span class="filename"></span></td>
        <td><span class="author"></span></td>
        <td><span class="title"></span></td>
        <td><span class="publishing-year"></span></td>
        <td class="right-align">
          <a class="action-button btn-floating red waves-effect waves-light" data-action="delete"><i class="material-icons">delete</i></a>
          <a class="action-button btn-floating service-color darken waves-effect waves-light" data-action="download" data-service="corpus-analysis"><i class="material-icons">file_download</i></a>
          <a class="action-button btn-floating service-color darken waves-effect waves-light" data-action="view" data-service="corpus-analysis"><i class="material-icons">send</i></a>
        </td>
      </tr>
    `.trim(),
    ressourceMapper: (corpusFile) => {
      return {
        'id': corpusFile.id,
        'author': corpusFile.author,
        'creation-date': corpusFile.creation_date,
        'filename': corpusFile.filename,
        'publishing-year': corpusFile.publishing_year,
        'title': corpusFile.title
      };
    },
    sortArgs: ['creation-date', {order: 'desc'}],
    valueNames: [
      {data: ['id']},
      {data: ['creation-date']},
      'author',
      'filename',
      'publishing-year',
      'title'
    ]
  };

  constructor(listElement, options = {}) {
    super(listElement, {...CorpusFileList.options, ...options});
    this.corpusId = listElement.dataset.corpusId;
  }

  init(user) {
    this._init(user.corpora[this.corpusId].files);
  }

  onClick(event) {
    let actionButtonElement = event.target.closest('.action-button');
    let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action;
    let corpusFileElement = event.target.closest('tr');
    let corpusFileId = corpusFileElement.dataset.id;
    switch (action) {
      case 'delete': {
        Utils.deleteCorpusFileRequest(this.userId, this.corpusId, corpusFileId);
        break;
      }
      case 'download': {
        window.location.href = `/corpora/${this.corpusId}/files/${corpusFileId}/download`;
        break;
      }
      case 'view': {
        window.location.href = `/corpora/${this.corpusId}/files/${corpusFileId}`;
        break;
      }
      default: {
        break;
      }
    }
  }

  onPatch(patch) {
    let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/files/([A-Za-z0-9]*)`);
    let filteredPatch = patch.filter(operation => re.test(operation.path));
    for (let operation of filteredPatch) {
      switch(operation.op) {
        case 'add': {
          let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/files/([A-Za-z0-9]*)$`);
          if (re.test(operation.path)) {this.add(operation.value);}
          break;
        }
        case 'remove': {
          let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/files/([A-Za-z0-9]*)$`);
          if (re.test(operation.path)) {
            let [match, corpusFileId] = operation.path.match(re);
            this.remove(corpusFileId);
          }
          break;
        }
        case 'replace': {
          let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/files/([A-Za-z0-9]*)/(author|filename|publishing_year|title)$`);
          if (re.test(operation.path)) {
            let [match, corpusFileId, valueName] = operation.path.match(re);
            this.replace(corpusFileId, valueName.replace('_', '-'), operation.value);
          }
          break;
        }
        default: {
          break;
        }
      }
    }
  }
}
