import moment from 'moment';
import {
  SearchFilter,
  UrlQueryParams,
  statusDictionary,
  Option,
} from '@/recruits/models';

export default class RegisteredSearchFilter extends SearchFilter {
  firstName = '';
  lastName = '';
  middleName = '';
  birthDateFrom: Date | null = null;
  birthDateTo: Date | null = null;
  idERN = '';
  inn = '';
  snils = '';
  series = '';
  number = '';
  statuses: Array<Option> = [];

  public countFilters(): number {
    let filtersCount = super.countFilters();
    if (this.firstName) {
      filtersCount++;
    }
    if (this.lastName) {
      filtersCount++;
    }
    if (this.middleName) {
      filtersCount++;
    }
    if (this.birthDateFrom || this.birthDateTo) {
      filtersCount++;
    }
    if (this.idERN) {
      filtersCount++;
    }
    if (this.inn) {
      filtersCount++;
    }
    if (this.snils) {
      filtersCount++;
    }
    if (this.series) {
      filtersCount++;
    }
    if (this.number) {
      filtersCount++;
    }
    if (this.statuses && this.statuses.length > 0) {
      filtersCount++;
    }
    return filtersCount;
  }

  public prepareRequestParams(): UrlQueryParams {
    const birthdatefrom = moment(this.birthDateFrom);
    const birthdateto = moment(this.birthDateTo);

    return Object.assign(super.prepareRequestParams(), {
      firstname: this.firstName || undefined,
      lastname: this.lastName || undefined,
      middlename: this.middleName || undefined,
      birthdatefrom:
        (birthdatefrom.isValid() && birthdatefrom.format('YYYY-MM-DD')) ||
        undefined,
      birthdateto:
        (birthdateto.isValid() && birthdateto.format('YYYY-MM-DD')) ||
        undefined,
      idern: this.idERN || undefined,
      inn: this.inn || undefined,
      snils: this.snils || undefined,
      series: this.series || undefined,
      number: this.number || undefined,
      statuses: this.statuses?.flatMap(({ value }) => value || []) || undefined,
    });
  }

  public updateParamsFromRoute(query: { [key: string]: string }): void {
    super.updateParamsFromRoute(query);
    if (query.firstname) {
      this.firstName = String(query.firstname);
    }
    if (query.lastname) {
      this.lastName = String(query.lastname);
    }
    if (query.middlename) {
      this.middleName = String(query.middlename);
    }
    if (query.birthdatefrom) {
      const birthDateFrom = moment(String(query.birthdatefrom));
      if (birthDateFrom.isValid()) this.birthDateFrom = birthDateFrom.toDate();
    }
    if (query.birthdateto) {
      const birthDateTo = moment(String(query.birthdateto));
      if (birthDateTo.isValid()) this.birthDateTo = birthDateTo.toDate();
    }
    if (query.idern) {
      this.idERN = String(query.idern);
    }
    if (query.inn) {
      this.inn = String(query.inn);
    }
    if (query.snils) {
      this.snils = String(query.snils);
    }
    if (query.series) {
      this.series = String(query.series);
    }
    if (query.number) {
      this.number = String(query.number);
    }
    if (query.statuses) {
      this.statuses = String(query.statuses)
        .split(',')
        .map((value) => {
          return {
            label: statusDictionary[value],
            value,
          };
        });
    }
  }
}
