import log from "electron-log/main";
import express from "express";
import _ from "lodash";
import errorTypeCheck from "../../util/errorTypeCheck";
import store from "../store/store";
import createdDirectoryIfNotExist from "../util/createDirectoryIfNotExist";
import EmsPartsOrderGenerateAd1File from "./ems-parts-order-generate-ad1";
import EmsPartsOrderGenerateAd2File from "./ems-parts-order-generate-ad2";
import EmsPartsOrderGenerateEnvFile from "./ems-parts-order-generate-env";
import EmsPartsOrderGenerateLinFile from "./ems-parts-order-generate-lin";
import EmsPartsOrderGeneratePfhFile from "./ems-parts-order-generate-pfh";
import EmsPartsOrderGeneratePflFile from "./ems-parts-order-generate-pfl";
import EmsPartsOrderGeneratePfmFile from "./ems-parts-order-generate-pfm";
import EmsPartsOrderGeneratePfoFile from "./ems-parts-order-generate-pfo";
import EmsPartsOrderGeneratePfpFile from "./ems-parts-order-generate-pfp";
import EmsPartsOrderGeneratePftFile from "./ems-parts-order-generate-pft";
import EmsPartsOrderGenerateStlFile from "./ems-parts-order-generate-stl";
import EmsPartsOrderGenerateTtlFile from "./ems-parts-order-generate-ttl";
import EmsPartsOrderGenerateVehFile from "./ems-parts-order-generate-veh";
import { EmsPartsOrder } from "./ems-parts-order-interfaces";

const handleEMSPartsOrder = async (
  req: express.Request,
  res: express.Response,
): Promise<void> => {
  //Route handler here only.

  const partsOrderBody = req.body as EmsPartsOrder;
  try {
    await generateEMSPartsOrder(partsOrderBody);
    res.status(200).json({ success: true });
  } catch (error) {
    log.error("Error generating parts price change", errorTypeCheck(error));
    res.status(500).json({
      success: false,
      error: "Error generating parts price change.",
      ...errorTypeCheck(error),
    });
  }
  return;
};

const generateEMSPartsOrder = async (
  partsOrder: EmsPartsOrder,
): Promise<void> => {
  log.debug(" Generating parts price change");
  //Check to make sure that the EMS Output file path exists. If it doesn't, create it. If it's not set, abandon ship.

  const emsOutFilePath: string | null = store.get("settings.emsOutFilePath");
  if (_.isEmpty(emsOutFilePath) || emsOutFilePath === null) {
    log.error("EMS Out file path is not set");
    throw new Error("EMS Out file path is not set");
  }
  try {
    createdDirectoryIfNotExist(emsOutFilePath);

    //Generate all required files: ad1, ad2, veh, lin, pfh, pfl, pfm,pfo, pfp, pft, stl, ttl
    await EmsPartsOrderGenerateAd1File(partsOrder);
    await EmsPartsOrderGenerateAd2File(partsOrder);
    await EmsPartsOrderGenerateVehFile(partsOrder);
    await EmsPartsOrderGenerateLinFile(partsOrder);
    await EmsPartsOrderGeneratePfhFile(partsOrder);
    await EmsPartsOrderGeneratePflFile(partsOrder);
    await EmsPartsOrderGeneratePfmFile(partsOrder);
    await EmsPartsOrderGeneratePfoFile(partsOrder);
    await EmsPartsOrderGeneratePfpFile(partsOrder);
    await EmsPartsOrderGeneratePftFile(partsOrder);
    await EmsPartsOrderGenerateStlFile(partsOrder);
    await EmsPartsOrderGenerateTtlFile(partsOrder);

    await EmsPartsOrderGenerateEnvFile(partsOrder);

    log.info(
      "EMS Parts Order files generated successfully for " +
        partsOrder.job.ciecaid,
    );
  } catch (error) {
    log.error("Error generating parts price change", errorTypeCheck(error));
    throw error;
  }
};

export { handleEMSPartsOrder };
