/**
 * Module for Tray functions.
 */
import { Tray } from 'electron';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

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

let tray = null;

const trayService = {};

const getTrayServiceIcon = (withEnvelope = false) => {
    let size;
    if (process.platform === 'darwin' || process.platform === 'win32') {
        size = '16x16';
    } else {
        size = '48x48';
    }

    if (withEnvelope) {
        return join(__dirname, '..', 'resources', 'images', 'tray', `envelope-${size}.png`);
    } else {
        return join(__dirname, '..', 'resources', 'images', `logo-${size}.png`);
    }
};

trayService.initTray = (window) => {
    const iconPath = getTrayServiceIcon();
    tray = new Tray(iconPath);
    tray.setToolTip('Converse Desktop');
    tray.on('click', () => {
        window.webContents.send('open-unread-chat');
        trayService.hideEnvelope();
        window.show();
    });
};

trayService.showEnvelope = () => {
    const iconPath = getTrayServiceIcon(true);
    tray.setImage(iconPath);
};

trayService.hideEnvelope = () => {
    const iconPath = getTrayServiceIcon();
    tray.setImage(iconPath);
};

export default trayService;
