var app = require('app'),
  BrowserWindow = require('browser-window'),
  Menu = require('menu'),
  dialog = require('dialog'),
  config = {},
  fs = require('fs');
//
// report crashes to atom-shell
require('crash-reporter').start();

//--------------------------------------------------------------------------------
// Add Project Dialog
//--------------------------------------------------------------------------------

var showAddExistingProjectDialog = (function(){

  var options = {
    title: "Select an existing project (project.clj)",
    properties: ["openFile"],
    filters: [
      {
        name: "Leiningen project config",
        extensions: ["clj"],
      }
    ],
  };

  function callback(filenames) {
    mainWindow.webContents.send('add-existing-project', filenames[0]);
  }

  return function() {
    dialog.showOpenDialog(options, callback);
  };

})();

//--------------------------------------------------------------------------------
// Menu Builder
//--------------------------------------------------------------------------------

menu_template = [
  {
    label: "File", // NOTE: On Mac, the first menu item is always the name of the Application
    submenu: [
      {
        label: "Add Existing Project",
        click: showAddExistingProjectDialog,
      },
    ],
  }
];

menu = Menu.buildFromTemplate(menu_template);
Menu.setApplicationMenu(menu);

//--------------------------------------------------------------------------------
// Main
//--------------------------------------------------------------------------------

// load config
// NOTE: this is mostly for development purposes
if (fs.existsSync(__dirname + '/config.json')) {
  config = require(__dirname + '/config.json');
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

function onWindowClose() {
  // dereference the window object
  mainWindow = null;
}

// send the OS-normalized app data path to the webpage
// NOTE: this event triggers the "global app init" on the webpage side
function onFinishLoad() {
  mainWindow.webContents.send('config-file-location', app.getDataPath());
}

// NOTE: a lot of the browserWindow options listed on the docs page aren't
// working - need to investigate
var browserWindowOptions = {
  height: 850,
  icon: __dirname + '/img/clojure-logo.png',
  title: 'ClojureScript Compiler',
  width: 1000
};

function startApp() {
  // create the browser window
  mainWindow = new BrowserWindow(browserWindowOptions);

  // load index.html
  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  // send info to the webpage
  mainWindow.webContents.on('did-finish-load', onFinishLoad);

  // Emitted when the window is closed.
  mainWindow.on('closed', onWindowClose);

  // optionally launch dev tools
  if (config.hasOwnProperty("dev-tools") && config["dev-tools"] === true) {
    mainWindow.openDevTools();
  }
}

// This method will be called when atom-shell has done everything
// initialization and ready for creating browser windows.
app.on('ready', startApp);

@@ -64,16 +64,42 @@
 ndow'),%0A
+  path = require('path'),%0A
   Menu =
@@ -725,29 +725,434 @@
 ion 
-callback(filenames) %7B
+addToProjectsJson(filename) %7B%0A    var projectsFilename = path.join(app.getDataPath(), %22projects.json%22);%0A%0A    var projects = JSON.parse(fs.readFileSync(projectsFilename, 'utf8'));%0A    projects.push(filename);%0A%0A    fs.writeFileSync(%0A      projectsFilename,%0A      JSON.stringify(projects, null, 2),%0A      %7Bencoding: %22utf8%22%7D);%0A  %7D%0A%0A  function callback(filenames) %7B%0A    var filename = filenames%5B0%5D;%0A    addToProjectsJson(filename);
 %0A   
@@ -1212,20 +1212,16 @@
 filename
-s%5B0%5D
 );%0A  %7D%0A%0A

update projects.json when adding existing project