import excelData from "./excelToJson";
import { write, writeFileSync } from "fs";
import { v4 as uuidv4 } from "uuid";
import { getJsDateFromExcel } from "excel-date-to-js";
import {
  Klinika,
  Pacienti,
  Shtrimi,
  Diagnoza,
  Trajtimi,
  Bari,
} from "./interfaces";
import { barnat, barnatRAw, rregulloBarnat } from "./rregulloBarnat";
// import { findPacient, upsertKlinika } from "./saveToNeo4j";

let klinikat: Klinika[] = [];
let pacientet: Pacienti[] = [];
let shtrimet: Shtrimi[] = [];
let diagnozat: Diagnoza[] = [];
let trajtimet: Trajtimi[] = [];

const createObjects = () => {
  let errorNr = 0;
  let pacientiNr = 0;

  const data = excelData();
  data.every(async (record: any) => {
    if (record && record["EMER"] && record["MBIEMER"]) {
      let emerPacienti = record.EMER;
      let mbiemerPacienti = record.MBIEMER;
      let moshaPacienti = record.MOSHA;
      let gjiniaPacienti = record.GJINIA;
      let datelindjaPacienti = record.DATELINDJA_KORIGJ;
      let klinikaEmer = record.klinika;
      let dataEShtrimit = record.DT_SHTRIMI_KORIGJ;
      let dite_qendrimi = record["DITE QENDRIMI NE SPITAL"];
      let diagnozaRaw = record.DIAGNOZA;
      let kodi_dg_icd_9Raw = record["Kodi DG  ICD-9"];
      let trajtimiUrgjence = record["TRAJTIMI SPITALOR Urgjence"];
      let trajtimiPavion = record["TRAJTIMI SPITALOR Pavion"];
      let trajtimiSalleLindje = record["TRAJTIMI SALLE LINDJE"];
      try {
        datelindjaPacienti = getJsDateFromExcel(datelindjaPacienti);
        dataEShtrimit = getJsDateFromExcel(dataEShtrimit);
      } catch (error: any) {
        datelindjaPacienti = null;
      }

      let klinika: Klinika = {
        id: uuidv4(),
        emer: klinikaEmer.trim(),
        pacientet: [],
        diagnozat: [],
      };
      insertKlinikaArr(klinika);
      let pacienti: Pacienti = {
        id: uuidv4(),
        emer: emerPacienti.trim(),
        mbiemer: mbiemerPacienti.trim(),
        datelindja: datelindjaPacienti,
        mosha: moshaPacienti,
        gjinia: klinika.emer === "OBS-GYN" ? "F" : gjiniaPacienti.trim(),
      };
      const newPacient = insertPacientiArr(pacienti);
      klinika.pacientet.push(newPacient);
      let diagnoza: Diagnoza = {
        id: uuidv4(),
        emer: diagnozaRaw.trim(),
        kodi_dg_icd_9: kodi_dg_icd_9Raw ? kodi_dg_icd_9Raw.split("\n") : [],
        pacientet: [],
      };
      diagnoza.pacientet.push(newPacient);
      const newDiagnoze = insertDiagnozaArr(diagnoza);
      klinika.diagnozat.push(newDiagnoze);
      let shtrimi: Shtrimi = {
        id: uuidv4(),
        date: dataEShtrimit,
        dite_qendrimi: dite_qendrimi ? dite_qendrimi : null,
      };
      shtrimet.push(shtrimi);

      if (trajtimiPavion) rregulloBarnat(trajtimiPavion);
      if (trajtimiSalleLindje) rregulloBarnat(trajtimiSalleLindje);
      if (trajtimiUrgjence) rregulloBarnat(trajtimiUrgjence);
      let trajtimi: Trajtimi = {
        id: uuidv4(),
        salle_lindje: trajtimiSalleLindje ? true : false,
        urgjence: trajtimiUrgjence ? true : false,
        barnat: barnat,
      };
      // return false;
    } else {
      // console.log(errorNr++, record);
    }
    return true;
  });
  // console.log(barnat.length);
  writeFileSync("barnat_raw.json", JSON.stringify(barnat, null, 2));
  writeFileSync("barnat_raw.txt", JSON.stringify(barnatRAw, null, 2));
};

const insertKlinikaArr = (klinika: Klinika) => {
  const isFoundKlinika = klinikat.some((klinike) => {
    if (klinika.emer === klinike.emer) {
      return true;
    }
    return false;
  });
  if (!isFoundKlinika) klinikat.push(klinika);
  return klinika;
};

const insertPacientiArr = (pacienti: Pacienti) => {
  const isFoundPacienti = pacientet.some((pacient) => {
    if (
      pacienti.emer === pacient.emer &&
      pacienti.mbiemer === pacient.mbiemer
    ) {
      return true;
    }
    return false;
  });
  if (!isFoundPacienti) pacientet.push(pacienti);
  return pacienti;
};

const insertDiagnozaArr = (diagnoza: Diagnoza) => {
  const isFoundDiagnoza = diagnozat.some((diagnoze) => {
    if (diagnoza.emer === diagnoze.emer) {
      return true;
    }
    return false;
  });
  if (!isFoundDiagnoza) diagnozat.push(diagnoza);
  return diagnoza;
};

export default createObjects;
