import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Alternative } from '@msfa-enums/alternative.enum';
import { FranvaroOrsaksKodEnum } from '@msfa-enums/franvaro-orsak-kod.enum';
import { OrsaksKoderAvvikelse } from '@msfa-models/orsaks-koder-avvikelse.model';
import { OrsaksKoderFranvaro } from '@msfa-models/orsaks-koder-franvaro.model';

@Component({
  selector: 'msfa-deltagare-orsaks-form',
  templateUrl: './deltagare-orsaks-form.component.html',
  styleUrls: ['./deltagare-orsaks-form.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DeltagareOrsaksFormComponent implements OnChanges {
  @Input() franvaroOrsaker: OrsaksKoderFranvaro[] | null = null;
  @Input() avvikelseOrsaker: OrsaksKoderAvvikelse[] | null = null;
  @Input() andraKandaOrsaker: OrsaksKoderFranvaro[] | null = null;
  @Input() orsakerFormGroup: FormGroup | null = null;
  @Input() avvikelseFormGroup: FormGroup | null = null;
  @Input() selectedAlternative: string | null = null;

  ngOnChanges(changes: SimpleChanges): void {
    if (changes) {
      this.orsakerFormGroup.reset();
    }
  }

  get showAndraKandaOrsaker(): boolean {
    return this.selectedAlternative === Alternative.FRANVARO &&
      +this.orsakerFormGroup.get('orsaker')?.value === FranvaroOrsaksKodEnum.AnnanKandOrsak;
  }

}
