import { antdNotification as notification } from "../util/antdFeedback";
import ipcTypes from "../ipc.types";
import {
  setReleaseNotes,
  setScrubResults,
  setSettings,
  setUpdateAvailable,
  setUpdateProgress,
  setWatchedPaths,
  setWatcherStatus
} from "../redux/application/application.actions";
import { calculateAudit, setAuditError } from "../redux/reporting/reporting.actions";
import { setScanEstimateList } from "../redux/scan/scan.actions";
import { store } from "../redux/store";
import { signOutStart } from "../redux/user/user.actions";
import { GetR4PDateWithClaim, UpsertEstimate } from "./ipc-estimate-utils";
const { ipcRenderer } = window;

console.log("----Initializing IPC Listeners in React App.");
ipcRenderer.on("test-toRenderer", (event, obj) => {
  console.log("test-toRenderer", obj);
});

ipcRenderer.on(ipcTypes.fileWatcher.toRenderer.filepathsList, (event, obj) => {
  store.dispatch(setWatchedPaths(obj));
});

//Filewatcher Status Section
ipcRenderer.on(ipcTypes.fileWatcher.toRenderer.startSuccess, (event, obj) => {
  store.dispatch(setWatcherStatus("Started"));
});
ipcRenderer.on(ipcTypes.fileWatcher.toRenderer.stopSuccess, (event, obj) => {
  store.dispatch(setWatcherStatus("Stopped"));
});
ipcRenderer.on(ipcTypes.fileWatcher.toRenderer.error, (event, obj) => {
  store.dispatch(setWatcherStatus(obj));
});

//Estimate Section
ipcRenderer.on(ipcTypes.estimate.toRenderer.getCloseDate, async (event, { filepath, clm_no }) => {
  const close_date = await GetR4PDateWithClaim(clm_no);
  ipcRenderer.send(ipcTypes.app.toMain.importJob, {
    filepath,
    close_date
  });
});

ipcRenderer.on(ipcTypes.estimate.toRenderer.estimateDecodeSuccess, async (event, obj) => {
  await UpsertEstimate(obj);
});

ipcRenderer.on(ipcTypes.store.response, (event, obj) => {
  store.dispatch(setSettings(obj));
});

//FileScan Section
ipcRenderer.on(ipcTypes.fileScan.toRenderer.scanFilePathsResponse, async (event, listOfEstimates) => {
  store.dispatch(setScanEstimateList(listOfEstimates));
});

ipcRenderer.on(ipcTypes.app.toRenderer.updateAvailable, async (event, updateInfo) => {
  store.dispatch(setUpdateAvailable(updateInfo));
});

ipcRenderer.on(ipcTypes.app.toRenderer.downloadProgress, async (event, progress) => {
  store.dispatch(setUpdateProgress(progress));
});

ipcRenderer.on(ipcTypes.app.toRenderer.signOut, async (event, progress) => {
  store.dispatch(signOutStart());
});

ipcRenderer.on(ipcTypes.app.toRenderer.setReleaseNotes, async (event, releaseNotes) => {
  store.dispatch(setReleaseNotes(releaseNotes));
});

ipcRenderer.on(ipcTypes.app.toRenderer.appVersion, async (event, appversion) => {
  window.$crisp.push(["set", "session:data", [[["rps-version", appversion]]]]);
});

//Handle Autdit

ipcRenderer.on(ipcTypes.audit.toRenderer.auditClaimsArray, async (event, claimsArray) => {
  store.dispatch(calculateAudit(claimsArray));
});
ipcRenderer.on(ipcTypes.audit.toRenderer.auditError, async (event, error) => {
  store.dispatch(setAuditError(error));
});
ipcRenderer.on(ipcTypes.app.toRenderer.scrubResults, async (event, results) => {
  store.dispatch(setScrubResults(results));
});

ipcRenderer.on(ipcTypes.app.toRenderer.scrubError, async (event, { message }) => {
  notification.open({ type: "error", message: "Estimate Scrubber Error", description: message });
});
