const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const ipc = electron.ipcMain;

const path = require('path');
const os = require('os')
const url = require('url');
const store = require('store');


/*

let mainWindow;
let loginWindow;

function handleNavigation(newURL) {
	console.log("got new url", newURL)
	const query = url.parse(newURL, true).query
	if (query) {
		if (query.error) {
			reject(new Error(`There was an error: ${query.error}`))
		}
		else if (query.code) {
			mainWindow.loadURL(isDev ? 'http://devel.structure.rest:8080/app/editor' : 'http://www.structure.rest:8080/app/editor')
			loginWindow.close()
		}
	}
}

function createWindow() {
	var screenElectron = electron.screen;
	var mainScreen = screenElectron.getPrimaryDisplay();
	var dimensions = mainScreen.size;

	mainWindow = new BrowserWindow({width:1280, 
			height: 900,
			webPreferences: {
webSecurity:false, 
nodeIntegration:true
}
}
);
	//mainWindow.loadURL('http://206.189.169.115/account/login');
	console.log("is dev??", isDev)
	isDev && require('react-devtools-electron')
	mainWindow.loadURL(isDev ? 'http://localhost/app/editor' : 'https://editor.structure.rest/app/editor')
	mainWindow.on('closed', () => mainWindow = null);

	return;
//old below
	ipc.on('loginStart', (event, args) => {
		loginWindow = new BrowserWindow({
			webPreferences:{
				nodeIntegration:true
			}
			}
		)
		var filter = {
			urls: [ '*://*.structure.rest/api/login/google/callback*']
		};
		electron.session.defaultSession.webRequest.onCompleted(filter, (details) => {
			var newUrl = details.url;
			handleNavigation(newUrl);
		});

		loginWindow.webContents.on('did-get-redirect-request', (event, oldUrl, newUrl) => {
				handleNavigation(newUrl)
				})
		loginWindow.loadURL(isDev? "http://devel.structure.rest/api/login/google" : "http://www.structure.rest:8080/api/login/google" )
		event.returnValue = "ok"
	})
	ipc.on('loginComplete', (event, args) => {
		console.log("login complete!")
		loginWindow.close()
		mainWindow.loadURL(isDev ? 'http://devel.structure.rest:8080/app/editor' : 'http://www.structure.rest:8080/app/editor')
		
		event.returnValue = "ok"
	})
	ipc.on('logout', (event, args) => {
		console.log("logging out")
		mainWindow.loadURL(isDev ? 'http://devel.structure.rest:8080/account/login' : `http://www.structure.rest:8080/account/login`);
		event.returnValue = "ok"
	})

	var screenElectron = electron.screen;
	var mainScreen = screenElectron.getPrimaryDisplay();
	var dimensions = mainScreen.size;

	mainWindow = new BrowserWindow({width:1280, 
			height: 900,
			webPreferences: {
			webSecurity:false, 
			nodeIntegration:true
			}
			}
			);
	//mainWindow.loadURL('http://206.189.169.115/account/login');
	console.log("is dev??", isDev)
	isDev && require('react-devtools-electron')
	mainWindow.loadURL(isDev ? 'http://devel.structure.rest:8080/account/login' : `http://www.structure.rest:8080/account/login`);
	//mainWindow.loadURL(true? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
	mainWindow.on('closed', () => mainWindow = null);
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {

  if (mainWindow === null) {
    createWindow();
  }
});

*/