import { RadiobuttonModel } from '@af/digi-ng/_form/form-radiobutton-group';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { AbstractControl, FormControl, FormGroup } from '@angular/forms';
import { RequiredValidator } from '@msfa-validators/required.validator';
import { SlutredovisningFormStep0Education } from './slutredovisning-form-step0-education.validator';
import { EducationLength, EducationLevel, MainOccupation } from '@msfa-models/slutredovisning.model';

export interface SlutredovisningFormStep0EducationFormData {
  educationLevel: EducationLevel;
  educationLength: EducationLength;
  otherExplanation?: string;
  educationSpecification: string;
}

type FormKeys = keyof SlutredovisningFormStep0EducationFormData;

@Component({
  selector: 'msfa-slutredovisning-form-step0-education',
  templateUrl: './slutredovisning-form-step0-education.component.html',
  styleUrls: ['./slutredovisning-form-step0-education.component.scss'],
})
export class SlutredovisningFormStep0EducationComponent implements OnInit {
  readonly formGroupKey: MainOccupation = MainOccupation.Education;
  @Input() formGroupRef: FormGroup;

  @Input() formData: SlutredovisningFormStep0EducationFormData;
  EducationLevel = EducationLevel;
  @Input() shouldValidate: boolean;
  @Output() nextClick = new EventEmitter<SlutredovisningFormStep0EducationFormData>();
  @Output() backClick = new EventEmitter<void>();

  private educationLevelFormControlName: FormKeys = 'educationLevel';
  private educationLengthFormControlName: FormKeys = 'educationLength';
  private otherExplanationFormControlName: FormKeys = 'otherExplanation';
  private educationSpecificationFormControlName: FormKeys = 'educationSpecification';

  // utbildningsniva
  educationLevelOptions: RadiobuttonModel[] = [
    { label: 'Högskola eller universitet', value: EducationLevel.HogskolaEllerUniversitet },
    { label: 'Yrkeshögskola', value: EducationLevel.Yrkeshogskola },
    { label: 'Komvux, gymnasium eller folkhögskola', value: EducationLevel.KomvuxGymnasiumFolkhogskola },
    {
      label: 'Vet ej',
      value: EducationLevel.VetEj,
    },
    {
      label: 'Annat',
      value: EducationLevel.Annat,
    },
  ];

  // langd_utbildning
  educationLengthOptions: RadiobuttonModel[] = [
    { label: 'Upp till ett år', value: EducationLength.UpToOneYear },
    { label: 'Ett år till två år', value: EducationLength.OneToTwoYears },
    { label: 'Två år eller längre', value: EducationLength.OverTwoYears },
  ];

  formGroup: FormGroup | AbstractControl;

  get educationLevelFormControl(): AbstractControl | undefined {
    return this.formGroup.get(this.educationLevelFormControlName);
  }

  get educationLengthFormControl(): AbstractControl | undefined {
    return this.formGroup.get(this.educationLengthFormControlName);
  }

  get otherExplanationFormControl(): AbstractControl | undefined {
    return this.formGroup.get(this.otherExplanationFormControlName);
  }
  get educationSpecificationFormControl(): AbstractControl | undefined {
    return this.formGroup.get(this.educationSpecificationFormControlName);
  }

  formControlIsInvalid(formControl: AbstractControl): boolean {
    return formControl.invalid && (formControl.touched || this.shouldValidate);
  }

  ngOnInit(): void {
    if (this.formGroupRef.get(this.formGroupKey)) {
      this.formGroup = this.formGroupRef.get(this.formGroupKey);
    } else {
      this.formGroup = new FormGroup(
        {
          [this.educationLevelFormControlName]: new FormControl(null, [
            RequiredValidator('Utbildningsnivå är obligatoriskt'),
          ]),
          [this.otherExplanationFormControlName]: new FormControl(null),
          [this.educationLengthFormControlName]: new FormControl(null, [
            RequiredValidator('Utbildningens längd är obligatorisk'),
          ]),
          [this.educationSpecificationFormControlName]: new FormControl(null, [
            RequiredValidator('Utbildningens inriktning är obligatorisk'),
          ]),
        },
        [SlutredovisningFormStep0Education.slutredovisningFormStep1EducationIsValid()]
      );
      this.formGroupRef.addControl(this.formGroupKey, this.formGroup);
    }
  }
}
