class CorpusList extends RessourceList {
  static instances = [];

  static getInstance(elem) {
    return CorpusList.instances.find((instance) => {
      return instance.listjs.list === elem;
    });
  }

  static autoInit() {
    for (let corpusListElement of document.querySelectorAll('.corpus-list:not(.no-autoinit)')) {
      new CorpusList(corpusListElement);
    }
  }

  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</label>
        </div>
        <table>
          <thead>
            <tr>
              <th></th>
              <th>Title and Description</th>
              <th>Status</th>
              <th></th>
            </tr>
          </thead>
          <tbody class="list"></tbody>
        </table>
        <ul class="pagination"></ul>
      `.trim();
    },
    item: `
      <tr class="clickable hoverable">
        <td><a class="btn-floating disabled"><i class="material-icons service-color darken" data-service="corpus-analysis">book</i></a></td>
        <td><b class="title"></b><br><i class="description"></i></td>
        <td><span class="status badge new corpus-status-color corpus-status-text" data-badge-caption=""></span></td>
        <td class="right-align">
          <a class="action-button btn-floating red waves-effect waves-light" data-action="delete-request"><i class="material-icons">delete</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: (corpus) => {
      return {
        'id': corpus.id,
        'creation-date': corpus.creation_date,
        'description': corpus.description,
        'status': corpus.status,
        'title': corpus.title
      };
    },
    sortArgs: ['creation-date', {order: 'desc'}],
    valueNames: [
      {data: ['id']},
      {data: ['creation-date']},
      {name: 'status', attr: 'data-corpus-status'},
      'description',
      'title'
    ]
  };

  constructor(listElement, options = {}) {
    super(listElement, {...CorpusList.options, ...options});
    CorpusList.instances.push(this);
  }

  init(user) {
    this._init(user.corpora);
  }

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

  onPatch(patch) {
    let re = new RegExp(`^/users/${this.userId}/corpora/([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/([A-Za-z0-9]*)$`);
          if (re.test(operation.path)) {this.add(operation.value);}
          break;
        }
        case 'remove': {
          let re = new RegExp(`^/users/${this.userId}/corpora/([A-Za-z0-9]*)$`);
          if (re.test(operation.path)) {
            let [match, corpusId] = operation.path.match(re);
            this.remove(corpusId);
          }
          break;
        }
        case 'replace': {
          let re = new RegExp(`^/users/${this.userId}/corpora/([A-Za-z0-9]*)/(status|description|title)$`);
          if (re.test(operation.path)) {
            let [match, corpusId, valueName] = operation.path.match(re);
            this.replace(corpusId, valueName, operation.value);
          }
          break;
        }
        default: {
          break;
        }
      }
    }
  }
}
