import { BrowserWindow, ipcMain, shell } from 'electron'
import Constants from './utils/Constants'

/*
 * IPC Communications
 * */
export default class IPCs {
  static initialize(window: BrowserWindow): void {
    // Get application version
    ipcMain.on('msgRequestGetVersion', () => {
      window.webContents.send('msgReceivedVersion', Constants.APP_VERSION)
    })

    // Open url via web browser
    ipcMain.on('msgOpenExternalLink', async (event, url: string) => {
      await shell.openExternal(url)
    })

    ipcMain.on('msgRequestGetServerPort', () => {
      const port = process.env['PLC_SIM_PORT']
      window.webContents.send('msgReceivedServerPort', port)
    })
  }
}
