import { Address } from './address.model';
import { DeltagareCompactResponse, DeltagareResponse } from './api/deltagare.response.model';
import { Avrop, mapAvropResponseToAvrop } from './avrop.model';
import { mapResponseToContactInformation } from './contact-information.model';
import { Disability, mapResponseToDisability } from './disability.model';
import { DriversLicense, mapResponseToDriversLicense } from './drivers-license.model';
import { Education, mapResponseToEducation } from './education.model';
import { HighestEducation, mapResponseToHighestEducation } from './highest-education.model';
import { PaginationMeta } from './pagination-meta.model';
import { PhoneNumber } from './phonenumber.model';
import { mapResponseToWorkExperience, WorkExperience } from './work-experience.model';

export interface DeltagareCompact {
  id: string;
  firstName: string;
  lastName: string;
  fullName: string;
  tjanst: string;
  utforandeVerksamhet: string;
  utforandeAdress: string;
  hasAvbrott: boolean;
  genomforandeReferens: string;
  startdatumAvrop: Date;
  slutdatumAvrop: Date;
}

export interface Deltagare {
  id: string;
  fullName: string;
  firstName: string;
  lastName: string;
  ssn: string;
  email: string;
  phoneNumbers: PhoneNumber[];
  addresses: Address[];
  driversLicense: DriversLicense;
  educations: Education[];
  highestEducation: HighestEducation;
  translator: string;
  disabilities: Disability[];
  workLanguages: string[];
  workExperiences: WorkExperience[];
  avropInformation: Avrop;
}

export interface DeltagareCompactData {
  data: DeltagareCompact[];
  meta: PaginationMeta;
}

export function mapResponseToDeltagareCompact(data: DeltagareCompactResponse): DeltagareCompact {
  const {
    sokandeId,
    fornamn,
    efternamn,
    tjanst,
    utforandeVerksamhet,
    utforandeAdress,
    hasAvbrott,
    genomforandeReferens,
    startdatumAvrop,
    slutdatumAvrop,
  } = data;
  return {
    id: sokandeId.toString(),
    firstName: fornamn,
    lastName: efternamn,
    fullName: `${fornamn} ${efternamn}`,
    tjanst,
    utforandeVerksamhet,
    utforandeAdress,
    hasAvbrott,
    genomforandeReferens,
    startdatumAvrop,
    slutdatumAvrop,
  };
}

export function mapResponseToDeltagare(data: DeltagareResponse): Deltagare {
  const {
    id,
    contact,
    driverlicense,
    highestEducation,
    educations,
    translator,
    workLanguages,
    disabilities,
    workExperiences,
    avropInformation,
  } = data;

  return {
    id,
    ...mapResponseToContactInformation(contact),
    driversLicense: mapResponseToDriversLicense(driverlicense),
    highestEducation: highestEducation && mapResponseToHighestEducation(highestEducation),
    educations: educations && educations.utbildningar.map(education => mapResponseToEducation(education)),
    translator: translator && translator.sprak.beskrivning,
    workLanguages: workLanguages && workLanguages.sprak.map(language => language.beskrivning),
    disabilities:
      disabilities && disabilities.funktionsnedsattningar.map(disability => mapResponseToDisability(disability)),
    workExperiences:
      workExperiences &&
      workExperiences.arbetslivserfarenheter.map(workExperience => mapResponseToWorkExperience(workExperience)),
    avropInformation: avropInformation && mapAvropResponseToAvrop(avropInformation),
  };
}
