// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('electronWindow', {
  minimize: () => ipcRenderer.send('window:minimize'),
  maximize: () => ipcRenderer.send('window:maximize'),
  fullscreen: () => ipcRenderer.send('window:fullscreen'),
  close: () => ipcRenderer.send('window:close'),
  openHuddle: (data: { token: string; serverUrl: string }) => ipcRenderer.send('window:open-huddle', data),
  closeHuddle: () => ipcRenderer.send('window:close-huddle'),
  platform: process.platform,
  onMaximizeChange: (callback: (isMaximized: boolean) => void) => {
    const handler = (_event: Electron.IpcRendererEvent, value: boolean) => callback(value);
    ipcRenderer.on('window:maximize-changed', handler);
    return () => { ipcRenderer.removeListener('window:maximize-changed', handler); };
  },
});

contextBridge.exposeInMainWorld('electronHuddle', {
  getScreenSources: () => ipcRenderer.invoke('screen:get-sources'),
});

contextBridge.exposeInMainWorld('electronAutoplay', {
  play: (payload: unknown) => ipcRenderer.send('autoplay:play', payload),
  dismiss: () => ipcRenderer.send('autoplay:dismiss'),
  navigate: (data: { networkId: string; streamId: string }) => ipcRenderer.send('autoplay:navigate', data),
  onPlay: (callback: (payload: unknown) => void) => {
    const handler = (_event: Electron.IpcRendererEvent, payload: unknown) => callback(payload);
    ipcRenderer.on('autoplay:play', handler);
    return () => { ipcRenderer.removeListener('autoplay:play', handler); };
  },
  onStop: (callback: () => void) => {
    const handler = () => callback();
    ipcRenderer.on('autoplay:stop', handler);
    return () => { ipcRenderer.removeListener('autoplay:stop', handler); };
  },
  onNavigate: (callback: (data: { networkId: string; streamId: string }) => void) => {
    const handler = (_event: Electron.IpcRendererEvent, data: { networkId: string; streamId: string }) => callback(data);
    ipcRenderer.on('autoplay:navigate', handler);
    return () => { ipcRenderer.removeListener('autoplay:navigate', handler); };
  },
});

contextBridge.exposeInMainWorld('electronScreen', {
  getScreenSources: () => ipcRenderer.invoke('screen:get-sources'),
  startRecordingWindow: () => ipcRenderer.send('screen-record:start'),
  stopRecordingWindow: () => ipcRenderer.send('screen-record:cancel'),
  onStopRequested: (callback: () => void) => {
    const handler = () => callback();
    ipcRenderer.on('screen-record:stopped', handler);
    return () => { ipcRenderer.removeListener('screen-record:stopped', handler); };
  },
});

contextBridge.exposeInMainWorld('electronScreenRecord', {
  stop: () => ipcRenderer.send('screen-record:stop'),
  onInit: (callback: () => void) => {
    const handler = () => callback();
    ipcRenderer.on('screen-record:init', handler);
    return () => { ipcRenderer.removeListener('screen-record:init', handler); };
  },
});

contextBridge.exposeInMainWorld('electronLink', {
  openExternal: (url: string) => ipcRenderer.invoke('link:open-external', url),
});

contextBridge.exposeInMainWorld('electronAttachment', {
  download: (url: string, filename?: string) =>
    ipcRenderer.send('attachment:download', { url, filename }),
});

contextBridge.exposeInMainWorld('electronApp', {
  setDockBadge: (count: number) => ipcRenderer.send('app:set-dock-badge', count),
  getVersion: () => ipcRenderer.invoke('app:get-version') as Promise<string>,
});
