import { FormControl, Validators } from '@angular/forms';
import { GENDERS } from '../../modules/customer/dropdown-values';

const telephoneValidation = /^[\d]{4}[\s]?[\d]{1,}?[\d]*$/;
const dateOfBirthValidation = /^\s*(3[01]|[12][0-9]|0?[1-9])\.(1[012]|0?[1-9])\.((?:19|20)\d{2})\s*$/;
// tslint:disable-next-line: max-line-length
const emaiValidation = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})/;

export class CustomValidators extends Validators {
  static validateTelephone(control: FormControl) {
    if (control.value && control.value.length > 0) {
      if (telephoneValidation.test(control.value)) {
        return null;
      }

      return { invalid_telephone: true };
    } else {
      return null;
    }
  }

  static validateDateOfBirth(control: FormControl) {
    if (control.value && control.value.length > 0) {
      if (dateOfBirthValidation.test(control.value)) {
        return null;
      }

      return { invalidDate: true };
    } else {
      return null;
    }
  }

  static validateEmail(control: FormControl) {
    if (control.value && control.value.length > 0) {
      if (emaiValidation.test(control.value)) {
        return null;
      }

      return { invalidEmail: true };
    } else {
      return null;
    }
  }

  static validateGender(control: FormControl) {
    if (control.value && control.value.length > 0) {
      if (control.value !== GENDERS[0]) {
        return null;
      }

      return { invalidGender: true };
    } else {
      return null;
    }
  }
}
