import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DeltagareAvrop } from '@msfa-models/avrop.model';
import { Disability } from '@msfa-models/disability.model';
import { DeltagareApiService } from '@msfa-services/api/deltagare.api.service';
import { combineLatest, Observable } from 'rxjs';
import { map, shareReplay, startWith, switchMap } from 'rxjs/operators';
import { DeltagareCardService } from '../../deltagare-card.service';

@Component({
  selector: 'msfa-deltagare-tab-sensitive-information',
  templateUrl: './deltagare-tab-sensitive-information.component.html',
  styleUrls: ['./deltagare-tab-sensitive-information.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeltagareTabSensitiveInformationComponent {
  genomforandereferens$: Observable<number> = this.activatedRoute.params.pipe(
    map(({ genomforandeReferens }) => +genomforandeReferens)
  );
  avropInformation$: Observable<DeltagareAvrop> = this.deltagareCardService.avropInformation$;
  workLanguages$: Observable<string[]> = this.genomforandereferens$.pipe(
    switchMap(genomforandeReferens =>
      genomforandeReferens ? this.deltagareApiService.fetchWorkLanguages$(genomforandeReferens) : null
    ),
    shareReplay(1)
  );
  disabilities$: Observable<Disability[]> = this.genomforandereferens$.pipe(
    switchMap(genomforandeReferens =>
      genomforandeReferens ? this.deltagareApiService.fetchDisabilities$(genomforandeReferens) : null
    ),
    shareReplay(1)
  );
  dataIsLoading$: Observable<boolean> = combineLatest([
    this.avropInformation$,
    this.workLanguages$,
    this.disabilities$,
  ]).pipe(
    map(([avropInformation, workLanguages, disabilities]) => !(avropInformation && workLanguages && disabilities)),
    startWith(true)
  );

  constructor(
    private activatedRoute: ActivatedRoute,
    private deltagareCardService: DeltagareCardService,
    private deltagareApiService: DeltagareApiService
  ) {}
}
