import ipcTypes from "../ipc.types";
import {
  setWatchedPaths,
  setWatcherStatus,
} from "../redux/application/application.actions";
import { store } from "../redux/store";
import { 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.default.fileWatcher.toRenderer.filepathsList,
  (event, obj) => {
    store.dispatch(setWatchedPaths(obj));
  }
);

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

//Estimate Section
ipcRenderer.on(
  ipcTypes.default.estimate.toRenderer.estimateDecodeStart,
  (event, obj) => {
    console.log("Decoding started!");
  }
);

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