import { dialog, IpcMainInvokeEvent } from "electron";
import log from "electron-log/main";
import _ from "lodash";
import Store from "../store/store";
import { getMainWindow } from "../util/toRenderer";
import {
  addWatcherPath,
  removeWatcherPath,
  StartWatcher,
  StopWatcher,
} from "../watcher/watcher";

const SettingsWatchedFilePathsAdd = async (): Promise<string[]> => {
  const mainWindow = getMainWindow();
  if (!mainWindow) {
    log.error("No main window found when trying to open dialog");
    return [];
  }
  const result = await dialog.showOpenDialog(mainWindow, {
    properties: ["openDirectory"],
  });

  if (!result.canceled) {
    Store.set(
      "settings.filepaths",
      _.union(result.filePaths, Store.get("settings.filepaths")),
    );
    addWatcherPath(result.filePaths);
  }

  return Store.get("settings.filepaths");
};
const SettingsWatchedFilePathsRemove = async (
  _event: IpcMainInvokeEvent,
  path: string,
): Promise<string[]> => {
  Store.set(
    "settings.filepaths",
    _.without(Store.get("settings.filepaths"), path),
  );
  removeWatcherPath(path);
  return Store.get("settings.filepaths");
};

const SettingsWatchedFilePathsGet = async (): Promise<string[]> => {
  const filepaths: string[] = Store.get("settings.filepaths") || [];
  return filepaths;
};

const SettingsWatcherPollingGet = async (): Promise<{
  enabled: boolean;
  interval: number;
}> => {
  const pollingEnabled: { enabled: boolean; interval: number } =
    Store.get("settings.polling");
  return { enabled: pollingEnabled.enabled, interval: pollingEnabled.interval };
};
const SettingsWatcherPollingSet = async (
  _event: IpcMainInvokeEvent,
  pollingSettings: {
    enabled: boolean;
    interval: number;
  },
): Promise<{
  enabled: boolean;
  interval: number;
}> => {
  log.info("Polling set", pollingSettings);
  const { enabled, interval } = pollingSettings;
  Store.set("settings.polling", { enabled, interval });

  //Restart the watcher with these new settings.
  await StopWatcher();
  StartWatcher();

  return { enabled, interval };
};
const SettingsPpcFilPathGet = async (): Promise<string> => {
  const ppcFilePath: string = Store.get("settings.ppcFilePath");
  return ppcFilePath;
};
const SettingsPpcFilPathSet = async (): Promise<string> => {
  const mainWindow = getMainWindow();
  if (!mainWindow) {
    log.error("No main window found when trying to open dialog");
    return "";
  }
  const result = await dialog.showOpenDialog(mainWindow, {
    properties: ["openDirectory"],
  });

  if (!result.canceled) {
    Store.set("settings.ppcFilePath", result.filePaths[0]); //There should only ever be on directory that was selected.
  }

  return (Store.get("settings.ppcFilePath") as string) || "";
};

const SettingEmsOutFilPathGet = async (): Promise<string> => {
  const emsOutFilePath: string = Store.get("settings.emsOutFilePath");
  return emsOutFilePath;
};
const SettingEmsOutFilPathSet = async (): Promise<string> => {
  const mainWindow = getMainWindow();
  if (!mainWindow) {
    log.error("No main window found when trying to open dialog");
    return "";
  }
  const result = await dialog.showOpenDialog(mainWindow, {
    properties: ["openDirectory"],
  });

  if (!result.canceled) {
    Store.set("settings.emsOutFilePath", result.filePaths[0]); //There should only ever be on directory that was selected.
  }

  return (Store.get("settings.emsOutFilePath") as string) || "";
};

export {
  SettingsPpcFilPathGet,
  SettingsPpcFilPathSet,
  SettingsWatchedFilePathsAdd,
  SettingsWatchedFilePathsGet,
  SettingsWatchedFilePathsRemove,
  SettingsWatcherPollingGet,
  SettingsWatcherPollingSet,
  SettingEmsOutFilPathGet,
  SettingEmsOutFilPathSet,
};
