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 { Signal } from '@msfa-models/signal.model';
import { Observable } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import { SignalViewService } from './signal-view.service';
import { SignalResponseOmfattning, SignalResponseType } from '@msfa-models/api/signal.response.model';

@Component({
  selector: 'msfa-signal-view',
  templateUrl: './signal-view.component.html',
  styleUrls: ['./signal-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SignalViewComponent {
  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.signalViewService.fetchAvropInformation$(+genomforandeReferens)),
    shareReplay(1)
  );
  signal$: Observable<Signal> = this.params$.pipe(
    switchMap(({ handlingId }) => this.signalViewService.fetchSignal$(handlingId as string)),
    shareReplay(1)
  );
  typeToText(type: SignalResponseType): string {
    switch (type) {
      case SignalResponseType.Work:
        return 'Arbete';
      case SignalResponseType.Education:
        return 'Utbildning';
      default:
        return 'Okänd';
    }
  }
  omfattningToText(omfattning: SignalResponseOmfattning): string {
    switch (omfattning) {
      case SignalResponseOmfattning.Heltid:
        return 'Heltid';
      case SignalResponseOmfattning.Deltid:
        return 'Deltid';
      default:
        return 'Okänd';
    }
  }

  constructor(private signalViewService: SignalViewService, private activatedRoute: ActivatedRoute) {}
}
