import { AbstractControl, ValidatorFn } from '@angular/forms';
import { Alternative } from '@msfa-enums/alternative.enum';
import { DayOrPartOfDay } from '@msfa-enums/day-or-part-of-day.enum';
import { FranvaroOrsaksKodEnum } from '@msfa-enums/franvaro-orsak-kod.enum';
import { KandaOrsakerEnum } from '@msfa-enums/kanda-orsaker-kod.enum';

export class DescriptionIsRequiredCheck {
  static CheckIfRequired(
    controlToValidateName: string,
    nestedFormGroupName: string,
    nestedFormGroupControlName: string,
    valueForWhichTheControlShouldBeRequired: KandaOrsakerEnum
  ): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfControlToValidate = fg?.get(controlToValidateName)?.value as string;
      const valueOfNestedFormControl = fg?.get(nestedFormGroupName)?.get(nestedFormGroupControlName)?.value as string;
      const isRequired = +valueOfNestedFormControl === valueForWhichTheControlShouldBeRequired && valueOfControlToValidate === '';

      return isRequired ? { descriptionIsRequired: true } : null;
    };
  }
}

export class OrsakerIsRequiredCheck {
  static CheckIfRequired(
    nestedFormGroupName: string,
    controlToValidateName: string,
  ): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfNestedFormControl = fg?.get(nestedFormGroupName)?.get(controlToValidateName)?.value as string;
      const isRequired = valueOfNestedFormControl === null && fg?.get(nestedFormGroupName)?.get('andraKandaOrsaker')?.value === null;

      return isRequired ? { orsakerIsRequired: true } : null;
    };
  }
}

export class DayOrPartOfDayIsRequiredCheck {
  static CheckIfRequired(
    controlToValidateName: string,
  ): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfControlToValidate = fg?.get(controlToValidateName)?.value as string;
      const isRequired = valueOfControlToValidate === null && fg?.get('alternative').value === Alternative.FRANVARO;

      return isRequired ? { dayOrPartOfDayIsRequired: true } : null;
    };
  }
}

export class AnnanKandOrsakeIsRequiredCheck {
  static CheckIfRequired(
    nestedFormGroupName: string,
    controlToValidateName: string,
  ): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfNestedFormControl = fg?.get(nestedFormGroupName)?.get(controlToValidateName)?.value as string;
      const isRequired = +fg?.get(nestedFormGroupName).get('orsaker').value === FranvaroOrsaksKodEnum.AnnanKandOrsak &&
        valueOfNestedFormControl === null;

      return isRequired ? { annanKandorsakIsRequired: true } : null;
    };
  }
}

export class StartTimeIsRequiredCheck {
  static CheckIfRequired(
    nestedFormGroupName: string,
    startTimeControlToValidateName: string,
  ): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfStartTimeFormControl = fg?.get(nestedFormGroupName)?.get(startTimeControlToValidateName)?.value as string;
      const isRequired = fg?.get('alternative')?.value as string === Alternative.FRANVARO &&
        fg?.get('dayOrPartOfDay').value === DayOrPartOfDay.DEL_AV_DAG && !valueOfStartTimeFormControl;

      return isRequired ? { startTimeIsRequired: true } : null;
    }
  }
}

export class EndTimeIsRequiredCheck {
  static CheckIfRequired(
    nestedFormGroupName: string,
    endTimeControlToValidateName: string
  ): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfEndTimeFormControl = fg?.get(nestedFormGroupName)?.get(endTimeControlToValidateName)?.value as string;
      const isRequired = fg?.get('alternative').value as string === Alternative.FRANVARO &&
        fg?.get('dayOrPartOfDay').value === DayOrPartOfDay.DEL_AV_DAG && !valueOfEndTimeFormControl;

      return isRequired ? { endTimeIsRequired: true } : null;
    }
  }
}

export class MotiveringIsRequiredCheck {
  static CheckIfRequired(controlToValidateName: string): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: boolean } => {
      const valueOfControlToValidate = fg?.get('fragorFormGroup').get(controlToValidateName)?.value as string;
      const isRequired = fg?.get('alternative').value as string === Alternative.AVVIKELSE && valueOfControlToValidate === '';

      return isRequired ? { motiveringIsRequired: true } : null;
    };
  }
}
export class DateIsRequiredCheck {
  static CheckIfRequired(controlToValidateName: string): ValidatorFn {
    return (fg: AbstractControl): { [key: string]: string } => {
      const valueOfControlToValidate = fg?.get(controlToValidateName).value as string;
      const isRequired = valueOfControlToValidate === '';

      return isRequired ? { dateIsRequired: 'Datum är obligatoriskt' } : null;
    };
  }
}
