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

const addListeners = (ipcMain, {standardIPCReply}) => {
  ipcMain.on(Constants.IPC_Check_Dependency_Installed, (event, data) => {
    try {
      const exists = fs.lstatSync(data.File).isFile();
      standardIPCReply(event, Constants.IPC_Check_Dependency_Installed_Reply, {
        data: {
          Exists: exists,
        },
      });
    } catch (e) {
      standardIPCReply(event, Constants.IPC_Check_Dependency_Installed_Reply, {
        data : {
          Exists: false,
        },
      });
    }
  });

  ipcMain.on(Constants.IPC_Check_Dependency_Installed + '_sync', (event, data) => {
    try {
      const ls = fs.lstatSync(data.File);
      event.returnValue = {
        data: {
          Exists: ls.isFile() || ls.isSymbolicLink(),
        },
      };
    } catch (e) {
      event.returnValue = {
        data: {
          Exists: false
        },
      };
    }
  });

  ipcMain.on(Constants.IPC_Install_Dependency, (event, data) => {
    if (data.Source.toLowerCase().endsWith('.dmg')) {
      helpers
      .executeAsync('open', ['-a', 'Finder', '-W', data.Source])
      .then(() => {
        standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
          Source: data.Source,
          URL: data.URL,
        });
      })
      .catch(error=> {
        standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
          Source: data.Source,
          URL: data.URL,
        }, error);
      });
    } else {
      const execInstall = () => {
        helpers
        .executeAndWait(data.Source)
        .then(() => {
          standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
            Source: data.Source,
            URL: data.URL,
          });
        })
        .catch(error => {
          standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
            Source: data.Source,
            URL: data.URL,
          }, error);
        });
      };
      if (data.IsWinFSP) {
        helpers
        .performWindowsUninstall([
          'WinFsp 2019',
          'WinFsp 2019.0',
          'WinFsp 2019.1',
          'WinFsp 2019.2',
          'WinFsp 2019.3',
          'WinFsp 2019.3 B1',
          'WinFsp 2019.3 B2',
          'WinFsp 2019.3 B3',
          'WinFsp 2019.3 B4',
          'WinFsp 2019.3 B5',
          'WinFsp 2020',
          'WinFsp 2020.0',
          'WinFsp 2020.1',
        ])
        .then(uninstalled => {
          if (uninstalled) {
            standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
              RebootRequired: true,
              Source: data.Source,
              URL: data.URL,
            });
          } else {
            execInstall();
          }
        })
        .catch(error => {
          standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
            Source: data.Source,
            URL: data.URL,
          }, error);
        });
      } else {
        execInstall();
      }
    }
  });
};

module.exports = {
  addListeners
};
