import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
    startService: (name: string) => electronAPI.ipcRenderer.invoke('services:start', name),
    stopService: (name: string) => electronAPI.ipcRenderer.invoke('services:stop', name),
    getServiceStatus: () => electronAPI.ipcRenderer.invoke('services:status'),
    getServiceLogs: (name: string) => electronAPI.ipcRenderer.invoke('services:logs', name),
    getConfig: (key: string) => electronAPI.ipcRenderer.invoke('config:get', key),
    saveConfig: (config: any) => electronAPI.ipcRenderer.invoke('config:save', config),
    getProjects: () => electronAPI.ipcRenderer.invoke('projects:list'),
    addProject: (project: any) => electronAPI.ipcRenderer.invoke('projects:add', project),
    removeProject: (id: string) => electronAPI.ipcRenderer.invoke('projects:remove', id),
    updateProject: (project: any) => electronAPI.ipcRenderer.invoke('projects:update', project),
    getPhpVersions: () => electronAPI.ipcRenderer.invoke('binaries:list-php'),
    downloadPhpVersion: (id: string) => electronAPI.ipcRenderer.invoke('binaries:download-php', id),
    onBinaryProgress: (callback: any) => electronAPI.ipcRenderer.on('binaries:progress', (_event, data) => callback(data)),
    getMariaDbVersions: () => electronAPI.ipcRenderer.invoke('mariadb:versions'),
    downloadMariaDbVersion: (id: string) => electronAPI.ipcRenderer.invoke('mariadb:download', id),
    onMariaDbProgress: (callback: any) => electronAPI.ipcRenderer.on('mariadb:progress', (_event, data) => callback(data)),
    selectDirectory: () => electronAPI.ipcRenderer.invoke('dialog:select-directory'),
    getSettings: () => electronAPI.ipcRenderer.invoke('config:get'),
    saveSettings: (settings: any) => electronAPI.ipcRenderer.invoke('config:save', settings),
    getNginxVersions: () => electronAPI.ipcRenderer.invoke('nginx:versions'),
    downloadNginxVersion: (id: string) => electronAPI.ipcRenderer.invoke('nginx:download', id),
    onNginxProgress: (callback: any) => electronAPI.ipcRenderer.on('nginx:progress', (_event, data) => callback(data)),
    getApacheVersions: () => electronAPI.ipcRenderer.invoke('apache:versions'),
    downloadApacheVersion: (id: string) => electronAPI.ipcRenderer.invoke('apache:download', id),
    onApacheProgress: (callback: any) => electronAPI.ipcRenderer.on('apache:progress', (_event, data) => callback(data)),
    pmaStatus: () => electronAPI.ipcRenderer.invoke('pma:status'),
    pmaSetup: (mariaDbPort: string) => electronAPI.ipcRenderer.invoke('pma:setup', mariaDbPort),
    onPmaProgress: (callback: any) => electronAPI.ipcRenderer.on('pma:progress', (_event, data) => callback(data)),
    selectFile: (filters?: any[]) => electronAPI.ipcRenderer.invoke('dialog:select-file', filters),
    checkMariaDbDb: (versionId: string, dbName: string) => electronAPI.ipcRenderer.invoke('mariadb:import-wizard:check-db', { versionId, dbName }),
    startMariaDbImport: (data: any) => electronAPI.ipcRenderer.invoke('mariadb:import-wizard:start', data),
    onMariaDbImportLog: (callback: any) => {
        const listener = (_event, msg: any) => callback(msg)
        electronAPI.ipcRenderer.on('mariadb:import:log', listener)
        return () => electronAPI.ipcRenderer.removeListener('mariadb:import:log', listener)
    },
    onMariaDbExportLog: (callback: any) => {
        const listener = (_event, msg: any) => callback(msg)
        electronAPI.ipcRenderer.on('mariadb:export:log', listener)
        return () => electronAPI.ipcRenderer.removeListener('mariadb:export:log', listener)
    },
    invoke: (channel: string, ...args: any[]) => electronAPI.ipcRenderer.invoke(channel, ...args)
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
    try {
        contextBridge.exposeInMainWorld('electron', electronAPI)
        contextBridge.exposeInMainWorld('api', api)
    } catch (error) {
        console.error(error)
    }
} else {
    // @ts-ignore (define in dts)
    window.electron = electronAPI
    // @ts-ignore (define in dts)
    window.api = api
}
