import { ipcMain, shell, dialog, app } from 'electron';
import { UtilsService } from '../services/utils.service';
import { IpcRequest } from 'shared/models/ipc';
import { SystemNotificationOptions } from 'shared/models/notification/system-notification.model';
import { NotificationService } from '../services/notification.service';
import { SteamService } from '../services/steam.service';
import { IpcService } from '../services/ipc.service';
import { from } from 'rxjs';
import { fstat, lstat } from 'fs';
import { pathExist } from '../helpers/fs.helpers';


// TODO IMPROVE WINDOW CONTROL BY USING WINDOW SERVICE

const ipc = IpcService.getInstance();

ipcMain.on('window.close', async () => {
    const utils = UtilsService.getInstance();
    utils.getMainWindows("index.html")?.close();
});

ipcMain.on('window.maximize', async () => {
    const utils = UtilsService.getInstance();
    utils.getMainWindows("index.html")?.maximize();
});

ipcMain.on('window.minimize', async () => {
    const utils = UtilsService.getInstance();
    utils.getMainWindows("index.html")?.minimize();
});

ipcMain.on('window.reset', async () => {
    const utils = UtilsService.getInstance();
    utils.getMainWindows("index.html")?.restore();
});

ipcMain.on('new-window', async (event, request: IpcRequest<string>) => {
    shell.openExternal(request.args);
});

ipc.on('choose-folder', async (req: IpcRequest<string>, reply) => {
  reply(
    from(dialog.showOpenDialog({properties: ['openDirectory'], defaultPath: req.args ?? ""}))
  )
});

ipcMain.on("window.progression", async (event, request: IpcRequest<number>) => {
    const utils = UtilsService.getInstance();
    utils.getMainWindows("index.html")?.setProgressBar(request.args / 100);
});

ipcMain.on('save-file', async (event, request: IpcRequest<{filename?: string, filters?: Electron.FileFilter[]}>) => {
    dialog.showSaveDialog({properties: ['showOverwriteConfirmation'], defaultPath: request.args.filename, filters: request.args.filters}).then(res => {
        const utils = UtilsService.getInstance();
        if(res.canceled || !res.filePath){ utils.ipcSend(request.responceChannel, {success: false}); }
        UtilsService.getInstance().ipcSend(request.responceChannel, {success: true, data: res.filePath});
    })
});

ipcMain.on("current-version", async (event, request: IpcRequest<void>) => {
    UtilsService.getInstance().ipcSend(request.responceChannel, {success: true, data: app.getVersion()});
});

ipcMain.on("open-logs", async (event, request: IpcRequest<void>) => {
    shell.openPath(app.getPath("logs"));
});

ipcMain.on("notify-system", async (event, request: IpcRequest<SystemNotificationOptions>) => {
    NotificationService.getInstance().notify(request.args)
});

ipcMain.on("open-steam", async (event, request: IpcRequest<void>) => {
    const steam = SteamService.getInstance();
    const utils = UtilsService.getInstance();
    steam.openSteam().then(res => {
        utils.ipcSend(request.responceChannel, {success: true, data: res});
    }).catch((e) => {
        utils.ipcSend(request.responceChannel, {success: false, error: e});
    });
});

ipc.on("is-folder-symlink", async (req: IpcRequest<string>, reply) => {
    const promise = new Promise<boolean>(async resolve => {
        if(!(await pathExist(req.args))){ return resolve(false); }
        lstat(req.args, (err, stats) => {
            if(err){ return resolve(false); }
            resolve(stats.isSymbolicLink());
        });
    }); 
    reply(from(promise));
});