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

export class RecruitsFromListSearchFilter extends SearchFilter {
  firstName = '';
  lastName = '';
  middleName = '';
  birthDateFrom: Date | null = null;
  birthDateTo: Date | null = null;
  // address = '';
  idERN = '';
  snils = '';
  documentSeries = '';
  documentNumber = '';
  subpoenaSeries = '';
  subpoenaNumber = '';
  // status: 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.address) {
    //   filtersCount++;
    // }
    if (this.snils) {
      filtersCount++;
    }
    if (this.documentSeries) {
      filtersCount++;
    }
    if (this.documentNumber) {
      filtersCount++;
    }
    if (this.subpoenaSeries) {
      filtersCount++;
    }
    if (this.subpoenaNumber) {
      filtersCount++;
    }
    // if (this.status && this.status.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,
      // address: this.address || undefined,
      snils: this.snils || undefined,
      documentseries: this.documentSeries || undefined,
      documentnumber: this.documentNumber || undefined,
      subpoenaseries: this.subpoenaSeries || undefined,
      subpoenanumber: this.subpoenaNumber || undefined,
      // status: this.status.map((s) => s.value).join(',') || 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.address) {
    //   this.address = String(query.address);
    // }
    if (query.snils) {
      this.snils = String(query.snils);
    }
    if (query.documentseries) {
      this.documentSeries = String(query.documentseries);
    }
    if (query.documentnumber) {
      this.documentNumber = String(query.documentnumber);
    }
    if (query.subpoenaseries) {
      this.subpoenaSeries = String(query.subpoenaseries);
    }
    if (query.subpoenanumber) {
      this.subpoenaNumber = String(query.subpoenanumber);
    }
    // if (query.status) {
    //   this.status = String(query.status)
    //     .split(',')
    //     .map((value) => {
    //       return {
    //         label: statusDictionary[value],
    //         value,
    //       };
    //     });
    // }
  }
}
