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

@Component({
  selector: 'msfa-slutredovisning-view',
  templateUrl: './slutredovisning-view.component.html',
  styleUrls: ['./slutredovisning-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SlutredovisningViewComponent {
  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.slutredovisningViewService.fetchAvropInformation$(+genomforandeReferens)
    ),
    shareReplay(1)
  );

  slutredovisning$: Observable<Slutredovisning> = this.params$.pipe(
    switchMap(({ handlingId }) => this.slutredovisningViewService.fetchSlutredovisning$(handlingId as string)),
    shareReplay(1)
  );

  statusIsFailed(status: string): boolean {
    return status === 'Ej godkänd';
  }

  constructor(private slutredovisningViewService: SlutredovisningViewService, private activatedRoute: ActivatedRoute) {}
}
