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

const addListeners = (ipcMain, {standardIPCReply}) => {
  ipcMain.on(Constants.IPC_Check_Daemon_Version, (event, data) => {
    helpers
    .checkDaemonVersion(data.Version, data.Provider)
    .then(code => {
      standardIPCReply(event, Constants.IPC_Check_Daemon_Version_Reply, {
        Valid: (code === 0),
        Code: code,
      });
    })
    .catch(e => {
      standardIPCReply(event, Constants.IPC_Check_Daemon_Version_Reply, {
        Valid: false,
      }, e);
    });
  });

  ipcMain.on(Constants.IPC_Check_Daemon_Version + '_sync', (event, data) => {
    helpers
    .checkDaemonVersion(data.Version, data.Provider)
    .then(code => {
      event.returnValue = {
        data: {
          Success: true,
          Valid: (code === 0),
          Code: code,
        },
      };
    })
    .catch(e => {
      event.returnValue = {
        data: {
          Error: e.toString(),
          Success: false,
          Valid: false
        },
      };
    });
  });
};

module.exports = {
  addListeners
};
