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

contextBridge.exposeInMainWorld("versions", {
    silkFlyLauncher: () => ipcRenderer.invoke("get-version"),
    node: () => process.versions.node,
    chromium: () => process.versions.chrome,
    electron: () => process.versions.electron,
});

contextBridge.exposeInMainWorld("files", {
    delete: () => ipcRenderer.invoke("delete-data"),
    export: () => ipcRenderer.invoke("export-data"),
    import: () => ipcRenderer.invoke("import-data"),

    autoDetectGamePath: () => ipcRenderer.invoke("auto-detect-game-path"),
    saveSilksongPath: (path) => ipcRenderer.invoke("save-path", path),
    loadSilksongPath: () => ipcRenderer.invoke("load-path"),
    loadBepinexVersion: () => ipcRenderer.invoke("load-bepinex-version"),
    loadBepinexBackupVersion: () => ipcRenderer.invoke("load-bepinex-backup-version"),
    saveNexusAPI: (api) => ipcRenderer.invoke("save-nexus-api", api),
    loadNexusAPI: () => ipcRenderer.invoke("load-nexus-api"),
    saveTheme: (theme, lacePinState) => ipcRenderer.invoke("save-theme", theme, lacePinState),
    loadTheme: () => ipcRenderer.invoke("load-theme"),
    saveLinuxSteam: (state) => ipcRenderer.invoke("save-linux-steam", state),
    loadLinuxSteam: () => ipcRenderer.invoke("load-linux-steam"),
});

contextBridge.exposeInMainWorld("electronAPI", {
    openExternalLink: (url) => ipcRenderer.invoke("open-link", url),
    openWindow: (file) => ipcRenderer.invoke("open-window", file),
    launchGame: (mode) => ipcRenderer.invoke("launch-game", mode),
    loadMainPage: () => ipcRenderer.invoke("load-main-page"),
    getPage: () => ipcRenderer.invoke("get-page"),
    getOS: () => process.platform,
    onShowToast: (callback) => {
        ipcRenderer.on("showToast", (event, message, type, duration) => {
            callback(message, type, duration);
        });
    },
    onShowBanner: (callback) => {
        ipcRenderer.on("showBanner", (event, message) => {
            callback(message);
        });
    },
});

contextBridge.exposeInMainWorld("bepinex", {
    install: () => ipcRenderer.invoke("install-bepinex"),
    uninstall: () => ipcRenderer.invoke("uninstall-bepinex"),
    backup: () => ipcRenderer.invoke("backup-bepinex"),
    deleteBackup: () => ipcRenderer.invoke("delete-bepinex-backup"),
});

contextBridge.exposeInMainWorld("nexus", {
    verifyAPI: () => ipcRenderer.invoke("verify-nexus-api"),
    download: (link) => ipcRenderer.invoke("open-download", link),
    search: (keywords, offset, count, sortFilter, sortOrder) => ipcRenderer.invoke("search-nexus-mods", keywords, offset, count, sortFilter, sortOrder),
});

contextBridge.exposeInMainWorld("mods", {
    searchInstalled: (keywords, offset, count, sortFilter, sortOrder) => ipcRenderer.invoke("search-installed-mods", keywords, offset, count, sortFilter, sortOrder),
    uninstall: (modId) => ipcRenderer.invoke("uninstall-mod", modId),
    getMods: (type) => ipcRenderer.invoke("get-mods", type),
    activateMods: (modId) => ipcRenderer.invoke("activate-mod", modId),
    deactivateMods: (modId) => ipcRenderer.invoke("deactivate-mod", modId),
    add: () => ipcRenderer.invoke("add-offline-mod"),
});

contextBridge.exposeInMainWorld("thunderstore", {
    search: (keywords, offset, count, sortFilter, sortOrder) => ipcRenderer.invoke("search-thunderstore-mods", keywords, offset, count, sortFilter, sortOrder),
    download: (url, modId) => ipcRenderer.invoke("download-thunderstore-mods", url, modId),
});
