import { app, BrowserWindow, ipcMain, shell, powerMonitor } from 'electron';
import path from 'path';
import { fileURLToPath } from 'url';
import keytar from 'keytar';

import trayService from './modules/tray-service.js';
import menuService from './modules/menu-service.js';
import settingsService from './modules/settings-service.js';
import setFindBar from "find-bar" // search bar

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export let mainWindow;
let zoom = 1;

const isMac = process.platform === 'darwin';
const isWin = process.platform === 'win32';

function initApp() {
    if (!app.requestSingleInstanceLock()) {
        app.quit();
    }

    createWindow();

    if (isWin) {
        app.setAppUserModelId("com.rscommunity.rs-desktop");
    }
}

function createWindow() {
    const getSavedWindowBounds = () => {
        const winBounds = settingsService.get('winBounds') || { width: 800, height: 600, x: undefined, y: undefined };        winBounds.width = Math.max(winBounds.width, 200);
        winBounds.height = Math.max(winBounds.height, 200);
        return winBounds;
    };

    const mainWindowOptions = {
        zoomToPageWidth: true,
        show: false,
        autoHideMenuBar: settingsService.get('hideMenubar') || false,
        webPreferences: {
            nodeIntegration: true,
            preload: path.join(__dirname, 'preload.cjs'), // this is a commonjs file, because electron doesnt support natively loading esm in preload files, which is retarded, but i cant change that.. so we will just cope :p
        },
        icon: path.join(__dirname, 'resources', 'images', 'logo-48x48.png'),
        ...getSavedWindowBounds(),
    };

    mainWindow = new BrowserWindow(mainWindowOptions);

    trayService.initTray(mainWindow);
    menuService.createMenu(mainWindow);

    mainWindow.on('close', (e) => {
        settingsService.set('isMaximized', mainWindow.isMaximized());
        if (mainWindow.isFullScreen()) {
            mainWindow.setFullScreen(false);
        }
        if (!app.isQuitting && settingsService.get('minimizeOnClose')) {
            e.preventDefault();
            mainWindow.hide();
        }
        return false;
    });

    mainWindow.on('resized', () => {
        settingsService.set('winBounds', mainWindow.getBounds());
    });
    mainWindow.on('moved', () => {
        settingsService.set('winBounds', mainWindow.getBounds());
    });

    if (isMac) {
        powerMonitor.on('shutdown', () => {
            app.isQuitting = true;
            app.quit();
        });
    }

    ipcMain.on('app-quit', () => {
        app.isQuitting = true;
        app.quit();
    });

    ipcMain.on('increaseZoom', () => { // custom zoom for the client
        if (zoom >= 2) return;
        zoom += 0.1;
        mainWindow.webContents.setZoomFactor(zoom);
    });

    ipcMain.on('decreaseZoom', () => {
        if (zoom <= 0.5) return;
        zoom -= 0.1;
        mainWindow.webContents.setZoomFactor(zoom);
    });

    mainWindow.webContents.setZoomFactor(1);

    mainWindow.on('closed', () => {
        mainWindow = null;
    });

    mainWindow.webContents.setWindowOpenHandler((details) => {
        shell.openExternal(details.url).catch(console.log);
        return { action: 'deny' };
    });

    settingsService.webContents = mainWindow.webContents;

    ipcMain.handle('settings', (e, method, ...args) => {
        return settingsService[method].apply(settingsService, args);
    });

    ipcMain.handle('trayService', (e, method, ...args) => {
        return trayService[method].apply(trayService, args);
    });

    ipcMain.handle('keytar', (e, method, ...args) => {
        return keytar[method].apply(keytar, args);
    });

    mainWindow.on('ready-to-show', () => {
        if (settingsService.get('isMaximized')) {
            mainWindow.maximize();
        } else {
            mainWindow.show();
        }
    });

    mainWindow.loadFile('index.html').catch((err) => {
        console.log(err);
        app.isQuitting = true;
        app.quit();
    });
}

app.on('ready', initApp);

app.on('browser-window-created', (_, win) => {
    setFindBar(win); // search bar
  });

app.on('window-all-closed', () => {
    app.quit();
});

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow();
    } else {
        mainWindow.show();
    }
});

app.on('second-instance', () => {
    mainWindow.show();
});

app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
