import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DriversLicense } from '@msfa-models/drivers-license.model';
import { Education } from '@msfa-models/education.model';
import { HighestEducation } from '@msfa-models/highest-education.model';
import { WorkExperience } from '@msfa-models/work-experience.model';
import { DeltagareApiService } from '@msfa-services/api/deltagare.api.service';
import { HandledareService } from '@msfa-services/handledare.service';
import { combineLatest, Observable } from 'rxjs';
import { map, shareReplay, startWith, switchMap } from 'rxjs/operators';
import { DeltagareCardService } from '../../deltagare-card.service';

@Component({
  selector: 'msfa-deltagare-tab-experiences',
  templateUrl: './deltagare-tab-experiences.component.html',
  styleUrls: ['./deltagare-tab-experiences.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeltagareTabExperiencesComponent {
  accordionExpanded = false;
  genomforandereferens$: Observable<number> = this.activatedRoute.params.pipe(
    map(({ genomforandeReferens }) => +genomforandeReferens)
  );
  workExperiences$: Observable<WorkExperience[]> = this.genomforandereferens$.pipe(
    switchMap(genomforandeReferens =>
      genomforandeReferens ? this.deltagareApiService.fetchWorkExperiences$(genomforandeReferens) : null
    ),
    shareReplay(1)
  );
  highestEducation$: Observable<HighestEducation> = this.genomforandereferens$.pipe(
    switchMap(genomforandeReferens =>
      genomforandeReferens ? this.deltagareApiService.fetchHighestEducation$(genomforandeReferens) : null
    ),
    shareReplay(1)
  );
  educations$: Observable<Education[]> = this.genomforandereferens$.pipe(
    switchMap(genomforandeReferens =>
      genomforandeReferens ? this.deltagareApiService.fetchEducations$(genomforandeReferens) : null
    ),
    shareReplay(1)
  );
  driversLicense$: Observable<DriversLicense> = this.genomforandereferens$.pipe(
    switchMap(genomforandeReferens =>
      genomforandeReferens ? this.deltagareApiService.fetchDriversLicense$(genomforandeReferens) : null
    ),
    shareReplay(1)
  );

  dataIsLoading$: Observable<boolean> = combineLatest([
    this.workExperiences$,
    this.highestEducation$,
    this.educations$,
    this.driversLicense$,
  ]).pipe(
    map(
      ([workExperiences, highestEducation, educations, driversLicense]) =>
        !(workExperiences && highestEducation && educations && driversLicense)
    ),
    startWith(true)
  );

  firstVisibleWorkExperiences(workExperiences: WorkExperience[]): WorkExperience[] {
    return workExperiences?.slice(0, 2);
  }

  hiddenWorkExperiences(workExperiences: WorkExperience[]): WorkExperience[] {
    return workExperiences?.slice(2);
  }

  toggleAccordionExpanded(): void {
    this.accordionExpanded = !this.accordionExpanded;
  }

  constructor(
    private activatedRoute: ActivatedRoute,
    private deltagareCardService: DeltagareCardService,
    private handledareService: HandledareService,
    private deltagareApiService: DeltagareApiService
  ) {}
}
