const { contextBridge, ipcRenderer } = require('electron');

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld('electronAPI', {
  // Server configuration
  getServerConfig: () => ipcRenderer.invoke('get-server-config'),
  saveServerConfig: (config) => ipcRenderer.invoke('save-server-config', config),
  testConnection: (serverUrl) => ipcRenderer.invoke('test-connection', serverUrl),
  connectToServer: () => ipcRenderer.invoke('connect-to-server'),
  getStoredCredentials: () => ipcRenderer.invoke('get-stored-credentials'),
  clearCredentials: () => ipcRenderer.invoke('clear-credentials'),
  
  // Offline mode
  cacheContent: (key, data) => ipcRenderer.invoke('cache-content', key, data),
  getCachedContent: (key, maxAge) => ipcRenderer.invoke('get-cached-content', key, maxAge),
  clearCache: () => ipcRenderer.invoke('clear-cache'),
  getOfflineMode: () => ipcRenderer.invoke('get-offline-mode'),
  onOfflineModeChanged: (callback) => ipcRenderer.on('offline-mode-changed', (event, enabled) => callback(enabled)),
  
  // Picture-in-Picture
  openPip: () => ipcRenderer.invoke('open-pip'),
  closePip: () => ipcRenderer.invoke('close-pip'),
  
  // Chromecast
  discoverChromecast: () => ipcRenderer.invoke('discover-chromecast'),
  castToDevice: (deviceInfo, mediaUrl) => ipcRenderer.invoke('cast-to-device', deviceInfo, mediaUrl),
  onChromecastDevices: (callback) => ipcRenderer.on('chromecast-devices', (event, devices) => callback(devices)),
  
  // Updates
  checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
  onUpdateProgress: (callback) => ipcRenderer.on('update-progress', (event, percent) => callback(percent)),
  
  // Platform info
  platform: process.platform,
  
  // Version info
  versions: {
    node: process.versions.node,
    chrome: process.versions.chrome,
    electron: process.versions.electron
  }
});
