import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Params } from '@msfa-models/api/params.model';
import { DeltagareAvrop } from '@msfa-models/avrop.model';
import { FranvaroReport } from '@msfa-models/franvaro.model';
import { Observable } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import { FranvaroReportViewService } from './franvaro-report-view.service';

@Component({
  selector: 'msfa-franvaro-report-view',
  templateUrl: './franvaro-report-view.component.html',
  styleUrls: ['./franvaro-report-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FranvaroReportViewComponent {
  params$: Observable<Params> = this.activatedRoute.params.pipe(
    map(params => ({
      handlingId: params.handlingId as string,
      genomforandeReferens: params.genomforandeReferens as string,
    }))
  );
  avrop$: Observable<DeltagareAvrop> = this.params$.pipe(
    switchMap(({ genomforandeReferens }) =>
      this.franvaroReportViewService.fetchAvropInformation$(+genomforandeReferens)
    ),
    shareReplay(1)
  );
  franvaroReport$: Observable<FranvaroReport> = this.params$.pipe(
    switchMap(({ handlingId }) => this.franvaroReportViewService.fetchFranvaroReport$(handlingId as string)),
    shareReplay(1)
  );

  constructor(private franvaroReportViewService: FranvaroReportViewService, private activatedRoute: ActivatedRoute) {}
}
