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 { GemensamPlaneringDetailed } from '@msfa-models/gemensam-planering.model';
import { Observable } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import { GemensamPlaneringViewService } from './gemensam-planering-view.service';

@Component({
  selector: 'msfa-gemensam-planering-view',
  templateUrl: './gemensam-planering-view.component.html',
  styleUrls: ['./gemensam-planering-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class GemensamPlaneringViewComponent {
  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.gemensamPlaneringViewService.fetchAvropInformation$(+genomforandeReferens)
    ),
    shareReplay(1)
  );
  gemensamPlanering$: Observable<GemensamPlaneringDetailed> = this.params$.pipe(
    switchMap(({ handlingId }) => this.gemensamPlaneringViewService.fetchGemensamPlanering$(handlingId as string)),
    shareReplay(1)
  );

  constructor(
    private gemensamPlaneringViewService: GemensamPlaneringViewService,
    private activatedRoute: ActivatedRoute
  ) {}
}
