import path from 'path';
import jetpack from 'fs-jetpack';
import { app, BrowserWindow, Menu, shell } from 'electron';
import createWindow from './helpers/window';

const env = require('./env.json');
const config = require('./config');
const appMenu = require('./menu');
const tray = require('./tray');

require('electron-debug')({enabled: config.debug});
require('electron-dl')({saveAs: true});
require('electron-context-menu')();

let mainWindow;
let isQuitting = false;
let oldtitle;

/*if (env.name !== 'production') {
    var userDataPath = app.getPath('userData');
    app.setPath('userData', userDataPath + ' (' + env.name + ')');
}
*/
const isAlreadyRunning = app.makeSingleInstance(() => {
	if (mainWindow) {
		if (mainWindow.isMinimized()) {
			mainWindow.restore();
		}

		mainWindow.show();
	}
});

if (isAlreadyRunning) {
	app.quit();
}

function updateBadge(messageCount) {
    // Set badge
    if (process.platform === 'darwin') {
        app.dock.setBadge(messageCount ? messageCount[1] : '');
    } else {
        tray.setBadge(messageCount);
    }
}

function checkMessages(title) {
  const messageCount = (/\(([0-9]+)\)/).exec(title);
  updateBadge(messageCount);
}

function createMainWindow() {
	const isDarkMode = config.get('darkMode');

	const win = new createWindow('main', {
		title: app.getName(),
		show: false,
		width: 1300,
		height: 850,
		icon: process.platform === 'linux' && path.join(__dirname, 'static/Icon.png'),
		minWidth: 1000,
		minHeight: 700,
		alwaysOnTop: config.get('alwaysOnTop'),
    titleBarStyle: 'hidden-inset',
		autoHideMenuBar: true,
		darkTheme: isDarkMode, // GTK+3
		//backgroundColor: isDarkMode ? '#192633' : '#fff',
		webPreferences: {
			preload: path.join(__dirname, 'browser.js'),
			nodeIntegration: true,
			plugins: true
		}
	});

	if (process.platform === 'darwin') {
		win.setSheetOffset(40);
	}

    win.loadURL('file://' + __dirname + '/index.html');

	win.on('close', e => {
		if (!isQuitting) {
			e.preventDefault();

			if (process.platform === 'darwin') {
				app.hide();
			} else {
				win.hide();
			}
		}
	});

    win.on('page-title-updated', (e, title) => {
        e.preventDefault();
        checkMessages(title);
    });

	return win;
}

app.on('ready', () => {
	Menu.setApplicationMenu(appMenu);
	mainWindow = createMainWindow();
	tray.create(mainWindow);

	const page = mainWindow.webContents;

	const argv = require('minimist')(process.argv.slice(1));

	page.on('dom-ready', () => {
		page.insertCSS(jetpack.read(path.join(__dirname, 'browser.css'), 'utf8'));
		page.insertCSS(jetpack.read(path.join(__dirname, 'themes/dark-mode.css'), 'utf8'));

        if (process.platform === 'darwin') {
            page.insertCSS(jetpack.read(path.join(__dirname, 'themes/osx-fix.css'), 'utf8'));
        }
        
		if (argv.minimize) {
			mainWindow.minimize();
		} else {
			mainWindow.show();
		}
    
    if (env.name === 'development') {
      mainWindow.openDevTools();
    }
	});

	page.on('new-window', (e, url) => {
		e.preventDefault();
		shell.openExternal(url);
	});
});

app.on('activate', () => {
	mainWindow.show();
});

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



@@ -333,22 +333,19 @@
 nabled: 
-config
+env
 .debug%7D)
@@ -466,22 +466,8 @@
 lse;
-%0Alet oldtitle;
 %0A%0A/*
@@ -1841,21 +1841,19 @@
 40);%0A%09%7D%0A
-%0A    
+%09%0A%09
 win.load
@@ -2064,21 +2064,19 @@
 %09%7D%0A%09%7D);%0A
-%0A    
+%09%0A%09
 win.on('
@@ -2112,24 +2112,18 @@
 e) =%3E %7B%0A
-        
+%09%09
 e.preven
@@ -2134,24 +2134,18 @@
 ault();%0A
-        
+%09%09
 checkMes
@@ -2158,20 +2158,17 @@
 title);%0A
-    
+%09
 %7D);%0A%0A%09re
@@ -2591,25 +2591,21 @@
 tf8'));%0A
-%0A        
+%09%09%0A%09%09
 if (proc
@@ -2637,20 +2637,11 @@
 ) %7B%0A
-            
+%09%09%09
 page
@@ -2722,24 +2722,12 @@
 ));%0A
-        %7D%0A      
+%09%09%7D%0A
   %0A%09
@@ -2832,30 +2832,13 @@
 env.
-name === 'development'
+debug
 ) %7B%0A

Fix debug preferences