'use strict'
const app = require('app')
const BrowserWindow = require('browser-window')
const path = require('path')
const fs = require('fs')
const ipc = require('ipc')

// report crashes to the Electron project
require('crash-reporter').start()

// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')()

// WHITELABELING OPTIONS
var options = {
	page_title: 'Aufbau' // What do you want to display in the menu bar?
}

// Return requested path location
function sendPath (mode) {
	return function (event, arg) {
		var path = app.getPath(arg)
		if (mode == 'async') {
		  event.sender.send('asynchronous-reply', path)
		} else if (mode == 'sync') {
			event.returnValue = path
		}
	}
}

// Allow for messages to be sent from client
ipc.on('asynchronous-message', sendPath('async'))
ipc.on('synchronous-message', sendPath('sync'))

function createMainWindow () {
	const win = new BrowserWindow({
		width: 1280,
		height: 768,
		resizable: true,
		title: options.page_title
	})

	// Set the download directory to being off of the user's home folder
	var tilde_downloads = path.join(app.getHomeDir(), 'Downloads')
	win.webContents.session.setDownloadPath(tilde_downloads)

	// Send the location of the user data folder to the client
	ipc.on('synchronous-message', function(event, arg) {
		event.returnValue = app.getPath(arg)
	});

	win.webContents.on('did-finish-load', function (webContents) {
		var page_title = webContents.sender.getTitle()
		var index_path
		var home_btn_css
		var home_btn_js
		if (page_title != 'Aufbau home') {
			// Add css
			home_btn_css = fs.readFileSync(path.join(__dirname, 'home-btn.css'), 'utf-8')
			webContents.sender.insertCSS(home_btn_css)
			
			// Add home button
			index_path = path.join(__dirname, 'www', 'index.html')
			home_btn_js = fs.readFileSync(path.join(__dirname, 'home-btn.jst'), 'utf-8').replace('{{index_path}}', index_path)
			webContents.sender.executeJavaScript(home_btn_js)

		}
	})

	win.loadUrl(`file://${__dirname}/www/index.html`)
	win.on('closed', onClosed)

	return win
}

function onClosed() {
	// deref the window
	// for multiple windows store them in an array
	mainWindow = null
}

// prevent window being GC'd
let mainWindow

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

app.on('activate-with-no-open-windows', function () {
	if (!mainWindow) {
		mainWindow = createMainWindow()
	}
})

app.on('ready', function () {
	var Menu = require('menu')
	var menu_template = require('./menus.js')(BrowserWindow)
	var menu = Menu.buildFromTemplate(menu_template)
	Menu.setApplicationMenu(menu)

	mainWindow = createMainWindow()
})

@@ -726,151 +726,8 @@
 %0A%7D%0A%0A
-// Allow for messages to be sent from client%0Aipc.on('asynchronous-message', sendPath('async'))%0Aipc.on('synchronous-message', sendPath('sync'))%0A
 %0Afun
@@ -1070,56 +1070,42 @@
 %09// 
-Send the location of th
+Allow for messages to b
 e 
-u
 se
-r data folder to the
+nt from
  cli
@@ -1117,16 +1117,17 @@
 ipc.on('
+a
 synchron
@@ -1144,74 +1144,75 @@
 e', 
-function(event, arg) %7B%0A%09%09event.returnValue = app.getPath(arg)%0A%09%7D);
+sendPath('async'))%0A%09ipc.on('synchronous-message', sendPath('sync'))
 %0A%0A%09w

move listeners to after window creation