import { Injectable } from '@angular/core';
import { Activity } from '@msfa-models/activity.model';
import { DeltagareAvrop } from '@msfa-models/avrop.model';
import {
  GemensamPlaneringDetailed,
  mapResponseToGemensamPlaneringDetailed,
} from '@msfa-models/gemensam-planering.model';
import { DeltagareApiService } from '@msfa-services/api/deltagare.api.service';
import { GemensamPlaneringApiService } from '@msfa-services/api/gemensam-planering-api.service';
import { HandlingarApiService } from '@msfa-services/api/handlingar.api.service';
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';

@Injectable()
export class GemensamPlaneringViewService {
  private _activities$: Observable<Activity[]> = this.gemensamPlaneringApiService.fetchActivities$();

  constructor(
    private handlingarApiService: HandlingarApiService,
    private deltagareApiService: DeltagareApiService,
    private gemensamPlaneringApiService: GemensamPlaneringApiService
  ) {}

  public fetchAvropInformation$(genomforandeReferens: number): Observable<DeltagareAvrop> {
    return this.deltagareApiService.fetchAvropInformation$(genomforandeReferens);
  }

  public fetchGemensamPlanering$(handlingId: string): Observable<GemensamPlaneringDetailed> {
    return this._activities$.pipe(
      switchMap(activities =>
        this.handlingarApiService
          .fetchGemensamPlanering$(handlingId)
          .pipe(map(({ data }) => mapResponseToGemensamPlaneringDetailed(data, activities)))
      )
    );
  }
}
