import { shellApi } from "../../api/api";
import { getPatientCip } from "../../features/get-patient-cip/action";
import { navigateToEcap } from "../../features/navigate-to-ecap/navigate-to-ecap";
import { ClinicalPathwaysActionMenu } from "../components/clinical-pathways-action-menu/clinical-pathways-action-menu";
import { PetitionerActionMenu } from "../components/petitioner-action-menu/petitioner-action-menu";
import { QuickActionItem } from "../shared-components/quick-action-item/quick-action-item";
import { MenuItemConfig, registerCommunicationNavMenu, registerNavMenuDivider, registerNavMenuViews } from "./common-nav-menu";

const doctorNavMenuItems: MenuItemConfig[] = [
  {
    id: "dinamic-monitoring",
    icon: "poll",
    label: "Monitoratge dinàmic",
    type: "item",
    sortHint: "0030",
    callbackFn: () => navigateToEcap("IA_DEV"),
  },
  {
    id: "analytics-monitoring", // id correcta?
    icon: "science",
    label: "Seguiment analític",
    type: "item",
    sortHint: "0060",
    callbackFn: () => navigateToEcap("SEGUIMENT_ANALITIC"),
  },
  {
    id: "results-reports",
    icon: "folder_shared",
    label: "Documents",
    type: "tree",
    sortHint: "0070",
    actionMenuItems: [
      {
        icon: "open_in_new",
        label: "Resultats i informes",
        callbackFn: () => navigateToEcap("RESULTAT_INFORMES"),
      },
      {
        icon: "open_in_new",
        label: "Documents usuari",
        callbackFn: () => {
          setTimeout(() => {
            navigateToEcap("CRIDA_DOCS_USU");
          }, 0);
        },
      },
      {
        icon: "open_in_new",
        label: "Informes clínics",
        callbackFn: () => navigateToEcap("INFORME_CLINIC_NET"),
      },
    ],
  },
  {
    id: "test-cabinets",
    icon: "list_alt",
    label: "Gabinets de proves",
    type: "item",
    sortHint: "0080",
    callbackFn: () => navigateToEcap("GABINETS_PROVES"),
  },
  {
    id: "prescriptions",
    icon: "pill",
    label: "Prescripció",
    type: "tree",
    sortHint: "0090",
    actionMenuItems: [
      {
        icon: "open_in_new",
        label: "Prescripció",
        callbackFn: () => navigateToEcap("PRESCRIPCIO_ACTIVA"),
      },
      {
        icon: "open_in_new",
        label: "Ordres de tractament",
        callbackFn: () => navigateToEcap("ORDRE_TRACTAMENT"),
      },
      {
        icon: "open_in_new",
        label: "Dispensació material sanitari",
        callbackFn: () => navigateToEcap("DISPENSACIO_MATERIAL_PACIENT"),
      },
      {
        icon: "open_in_new",
        label: "Prescripció social",
        callbackFn: () => navigateToEcap("PRESCRIPCIO_SOCIAL"),
      },
    ],
  },
  {
    id: "vaccines",
    icon: "vaccines",
    label: "Immunitzacions",
    type: "item",
    sortHint: "0100",
    callbackFn: () => navigateToEcap("IMMUNITZACIONS"),
  },
  {
    id: "it",
    icon: "history_toggle_off",
    label: "Incapacitat temporal",
    type: "item",
    sortHint: "0100",
    callbackFn: () => navigateToEcap("REV_IT"),
  },
  {
    id: "visits",
    icon: "event",
    label: "Visites",
    type: "item",
    sortHint: "0130",
    callbackFn: () => navigateToEcap("CONSULTA_VISITES"),
  },
];

export const registerClinicalPathwaysNavMenu = () => {
  shellApi.regionManager.registerView(shellApi.regionManager.regions.shell.navigationMenu, {
    id: "processes",
    sortHint: "0040",
    factory: () => {
      const menuItem = new ClinicalPathwaysActionMenu("account_tree", "Vies clíniques");

      return Promise.resolve(menuItem as any);
    },
  });
};

export const registerDoctorCommunicationMenuActions = () => {
  const communicationItems = [
    {
      id: "1",
      sortHint: "0010",
      icon: "open_in_new",
      label: "SMS",
      callbackFn: async () => {
        const CIP = await getPatientCip(shellApi);
        shellApi.ecapEventManager.publish("MISSATGES_DEV", "NO_TANCAR", {
          CIP: CIP || "",
          TipusMissatge: "SMS",
        });
      },
    },
    {
      id: "3",
      icon: "open_in_new",
      label: "Mail",
      callbackFn: async () => {
        const CIP = await getPatientCip(shellApi);
        shellApi.ecapEventManager.publish("COMUNICACIO_MISSATGES_CREAR", "NO_TANCAR", {
          CIP: CIP || "",
          TipusMissatge: "SMS",
        });
      },
    },
    {
      id: "4",
      sortHint: "0030",
      icon: "open_in_new",
      label: "Veu IP",
      callbackFn: () => navigateToEcap("VOIP"),
    },
    // { id: "5", sortHint: "0040", icon: "open_in_new", label: "Videoconsulta", callbackFn: () => {} },
  ];
  for (const item of communicationItems) {
    shellApi.regionManager.registerView(shellApi.regionManager.regions.shell.communicationSidenav, {
      id: item.id,
      sortHint: item.sortHint,
      factory: () => {
        const menuTest = new QuickActionItem(item.icon, item.label, item.callbackFn);
        return Promise.resolve(menuTest as any);
      },
    });
  }
};

export const registerDoctorClinicalPathwaysMenuActions = () => {
  const clinicalPathwaysItems = [
    {
      id: "1",
      sortHint: "0010",
      icon: "open_in_new",
      label: "Proces d'atenció - ARES",
      callbackFn: () => navigateToEcap("PROCES_ATENCIO"),
    },
    {
      id: "3",
      sortHint: "0030",
      icon: "open_in_new",
      label: "Nen sa",
      callbackFn: () => navigateToEcap("NEN_SA"),
    },
    {
      id: "4",
      sortHint: "0040",
      icon: "open_in_new",
      label: "ASSIR",
      callbackFn: () => navigateToEcap("NAVEGACIO_ASSIR"),
    },
    {
      id: "5",
      sortHint: "0050",
      icon: "open_in_new",
      label: "Odontologia",
      callbackFn: () => navigateToEcap("ODONTOLOGO"),
    },
    {
      id: "6",
      sortHint: "0060",
      icon: "open_in_new",
      label: "Situació d'especial cura",
      callbackFn: () => navigateToEcap("SIT_ESPEC_CURA_DEV"),
    },
    {
      id: "7",
      sortHint: "0070",
      icon: "open_in_new",
      label: "Valoració social",
      callbackFn: () => navigateToEcap("VALORACIO_SOCIAL"),
    },
  ];
  for (const item of clinicalPathwaysItems) {
    shellApi.regionManager.registerView(shellApi.regionManager.regions.shell.clinicalPathwaysSidenav, {
      id: item.id,
      sortHint: item.sortHint,
      factory: () => {
        const menuTest = new QuickActionItem(item.icon, item.label, item.callbackFn);
        return Promise.resolve(menuTest as any);
      },
    });
  }
};

export const registerPetitionerNavMenu = () => {
  shellApi.regionManager.registerView(shellApi.regionManager.regions.shell.navigationMenu, {
    id: "petitioner",
    sortHint: "0050",
    factory: () => {
      const menuItem = new PetitionerActionMenu("playlist_add", "Peticionari");
      return Promise.resolve(menuItem as any);
    },
  });
};

export const registerDoctorPetitionerMenuActions = () => {
  const petitionerItems = [
    {
      id: "1",
      sortHint: "0010",
      icon: "open_in_new",
      label: "Analítiques",
      callbackFn: () => navigateToEcap("LABORATORI"),
    },
    {
      id: "2",
      sortHint: "0020",
      icon: "open_in_new",
      label: "Ordres Clíniques",
      callbackFn: () => navigateToEcap("RESULT_OC"),
    },
    {
      id: "3",
      sortHint: "0030",
      icon: "open_in_new",
      label: "OC Exprés",
      callbackFn: () => navigateToEcap("OC_EXPRES"),
    },
    {
      id: "4",
      sortHint: "0040",
      icon: "open_in_new",
      label: "Sol·licitud de trasllat",
      callbackFn: () => navigateToEcap("TRANSPORT"),
    },
  ];
  for (const item of petitionerItems) {
    shellApi.regionManager.registerView(shellApi.regionManager.regions.shell.petitionerSidenav, {
      id: item.id,
      sortHint: item.sortHint,
      factory: () => {
        const menuItem = new QuickActionItem(item.icon, item.label, item.callbackFn);
        return Promise.resolve(menuItem as any);
      },
    });
  }
};

export const registerDoctorNavMenuViews = () => {
  registerCommunicationNavMenu();
  registerDoctorCommunicationMenuActions();
  registerClinicalPathwaysNavMenu();
  registerDoctorClinicalPathwaysMenuActions();
  registerPetitionerNavMenu();
  registerDoctorPetitionerMenuActions();
  registerNavMenuViews(doctorNavMenuItems);
  registerNavMenuDivider();
};
