import {
  SlutredovisningRequest,
  SlutredovisningRequestMainOccupationType,
} from '@msfa-models/api/slutredovisning.request.model';
import {
  Anstallningsform,
  MainOccupation,
  Omfattning,
  StillUnemployedReason,
} from '@msfa-models/slutredovisning.model';
import {
  SlutredovisningFormData,
  SlutredovisningFormDataMainOccupationDetails,
} from '../models/slutredovisning-form-data.model';
import { SlutredovisningStep0FormData } from '../slutredovisning-form-step0/slutredovisning-form-step0.component';
import { SlutredovisningStep1FormData } from '../slutredovisning-form-step1/slutredovisning-form-step1.component';
import { SlutredovisningStep2FormData } from '../slutredovisning-form-step2/slutredovisning-form-step2.component';

export function formsToSlutredovisningFormData(
  genomforandereferens: number,
  step0FormData: SlutredovisningStep0FormData,
  step1FormData: SlutredovisningStep1FormData,
  step2FormData: SlutredovisningStep2FormData
): SlutredovisningFormData {
  if (!genomforandereferens || !step0FormData || !step1FormData || !step2FormData) {
    return;
  }

  let mainOccupation: SlutredovisningFormDataMainOccupationDetails;
  if (step0FormData.mainOccupation === MainOccupation.Other) {
    mainOccupation = {
      type: MainOccupation.Other,
      other: {
        otherExplanation: step0FormData.other.otherExplanation,
      },
    };
  }

  if (step0FormData.mainOccupation === MainOccupation.Work) {
    mainOccupation = {
      type: MainOccupation.Work,
      work: step0FormData.work.map(workItem => {
        return {
          yrkesomrade: workItem.yrkesomrade,
          yrkesgrupp: workItem.yrkesgrupp,
          omfattning: workItem.omfattning as Omfattning,
          omfattningPercent: workItem.omfattningPercent,
          otherExplanation: workItem.annatAnstallningComment,
          anstallningsform: workItem.anstallningsform as Anstallningsform,
        };
      }),
    };
  }

  if (step0FormData.mainOccupation === MainOccupation.Education) {
    mainOccupation = {
      type: MainOccupation.Education,
      education: {
        educationLength: step0FormData.education.educationLength,
        educationLevel: step0FormData.education.educationLevel,
        otherExplanation: step0FormData.education.otherExplanation,
        educationSpecification: step0FormData.education.educationSpecification,
      },
    };
  }
  if (step0FormData.mainOccupation === MainOccupation.StillUnemployed) {
    mainOccupation = {
      type: MainOccupation.StillUnemployed,
      [MainOccupation.StillUnemployed]: {
        reasonsGoalNotReached: Object.entries(step0FormData.stillUnemployed.reasonsGoalNotReached)
          .map(([reason, isChecked]) => ({ reason: reason as StillUnemployedReason, isChecked }))
          .filter(reasonChecked => !!reasonChecked.isChecked)
          .map(({ reason }) => reason),
        otherExplanation: step0FormData.stillUnemployed.stillUnemployedExplanation,
      },
    };
  }

  if (step0FormData.mainOccupation === MainOccupation.ByteTillNyLeverantorIRustaOchMatcha) {
    mainOccupation = { type: MainOccupation.ByteTillNyLeverantorIRustaOchMatcha };
  }

  return {
    genomforandereferens: genomforandereferens,
    mainOccupation: mainOccupation,
    activities: step1FormData.activities,
    progressDescription: step2FormData.progressDescription,
    nextStepDescription: step2FormData.nextStepDescription,
    otherInformation: step2FormData.otherInformation,
  };
}

export function slutredovisningFormDataToSlutredovisningRequest(
  slutredovisningResponse: SlutredovisningFormData
): SlutredovisningRequest {
  const mainOccupationToSwedish: { [key: string]: SlutredovisningRequestMainOccupationType } = {
    [MainOccupation.Work]: 'arbete',
    [MainOccupation.Education]: 'utbildning',
    [MainOccupation.ByteTillNyLeverantorIRustaOchMatcha]: 'byte till ny leverantör i rusta och matcha',
    [MainOccupation.StillUnemployed]: 'fortsatt arbetssökande',
    [MainOccupation.Other]: 'annat',
  };

  return {
    ...slutredovisningResponse,
    mainOccupation: {
      ...slutredovisningResponse.mainOccupation,
      type: mainOccupationToSwedish[slutredovisningResponse.mainOccupation.type],
    },
    activities: slutredovisningResponse.activities.map(({ whatHasBeenDone, id }) => ({ whatHasBeenDone, id })),
  };
}
