const Constants = require('../../constants');
const fs = require('fs');

const addListeners = (ipcMain, {getMainWindow, dialog}) => {
  ipcMain.on(Constants.IPC_Browse_Directory + '_sync', (event, data) => {
    dialog.showOpenDialog(getMainWindow(), {
      defaultPath: data.Location,
      properties: ['openDirectory'],
      title: data.Title,
    }, (filePaths) => {
      if (filePaths && (filePaths.length > 0)) {
        event.returnValue = filePaths[0];
      } else {
        event.returnValue = '';
      }
    });
  });

  ipcMain.on(Constants.IPC_Delete_File, (event, data) => {
    try {
      if (fs.existsSync(data.FilePath)) {
        fs.unlinkSync(data.FilePath);
      }
    } catch (e) {
    }
  });
};

module.exports = {
  addListeners
};
