var UI = require('ui');
var Vector2 = require('vector2');
var Settings = require('settings');

var BASE_URL = 'https://timetablepush.me/';
var API_BASE_URL = BASE_URL + 'api/v1/';
var CONFIGURATION_URL = BASE_URL + 'app/config';

/*
 * App Start
 */

var viewMain = new UI.Card({
  title: 'Timetable Pusher',
  subtitle: 'Loading...'
});
viewMain.show();

/*
 * Configuration
 */

// Listen for configuration start
Settings.config(
  { 
    url: CONFIGURATION_URL,
    autoSave: true,
  },
  function(e) {
    
  },
  function(e) {
    console.log('Configuration closed');
  }
);

// Listen for configuration complete
Settings.config(
  { url: CONFIGURATION_URL },
  function(e) {
    console.log('Configuration closed');
    Settings.option('apiKey', e.apiKey);
  }
);

/*
 * Views
 */

var createViewSetupCard = new UI.Card({
  title: 'Setup Required',
  body: 'Open this app\'s configuration page on the Pebble watchapp.'
});

var createViewHomeMenu = new UI.Menu({
  sections: [{
    title: 'Timetable Pusher',
  },
  {
    title: 'Add entries for',
    items: [{
      title: 'Current week'
    },
    {
      title: 'Next week'
    }]
  },  
  {
    title: 'Advanced',
    items: [{
      title: 'Delete all pins'
    }],
  }]
});

/*
 * App Ready
 */

// Check whether the API key is set
Pebble.addEventListener("ready", function() {
  if (typeof Settings.option('apiKey') === 'undefined') {
    var viewSetupCard = createViewSetupCard;
    viewSetupCard.show();
  } else {
    var viewHomeMenu = createViewHomeMenu;
    viewHomeMenu.show();
  }
  
  viewMain.hide();
});

/*
 * Error Handlers
 */
@@ -21,42 +21,8 @@
 ');%0A
-var Vector2 = require('vector2');%0A
 var 
@@ -442,17 +442,16 @@
 ve: true
-,
 %0A  %7D,%0A  
@@ -531,16 +531,34 @@
 osed');%0A
+    checkReady();%0A
   %7D%0A);%0A%0A
@@ -637,16 +637,36 @@
 TION_URL
+,%0A    autoSave: true
  %7D,%0A  fu
@@ -726,42 +726,19 @@
     
-Settings.option('apiKey', e.apiKey
+checkReady(
 );%0A 
@@ -765,23 +765,17 @@
 */%0A%0Avar 
-createV
+v
 iewSetup
@@ -901,23 +901,17 @@
 );%0A%0Avar 
-createV
+v
 iewHomeM
@@ -1260,40 +1260,24 @@
 set%0A
-Pebble.addEventListener(%22r
+var checkR
 eady
-%22,
+ =
  fun
@@ -1281,16 +1281,17 @@
 function
+ 
 () %7B%0A  i
@@ -1345,28 +1345,24 @@
 ned') %7B%0A    
-var 
 viewSetupCar
@@ -1362,26 +1362,41 @@
 etupCard
- = createV
+.show();%0A  %7D else %7B%0A    v
 iewSetup
@@ -1399,16 +1399,23 @@
 etupCard
+.hide()
 ;%0A    vi
@@ -1412,33 +1412,32 @@
 ();%0A    view
-SetupCard
+HomeMenu
 .show();%0A  %7D
@@ -1440,107 +1440,283 @@
 %0A  %7D
- else %7B%0A    var viewHomeMenu = createViewHomeMenu;%0A    viewHomeMenu.show();%0A  %7D%0A  %0A  viewMain.hide(
+%0A  %0A  viewMain.hide();%0A%7D;%0A%0APebble.addEventListener(%22ready%22, function() %7B%0A  checkReady();%0A%7D);%0A%0AviewSetupCard.on('select', function(e) %7B%0A  console.log('Selected item #' + e.itemIndex + ' of section #' + e.sectionIndex);%0A  console.log('The item is titled %22' + e.item.title + '%22'
 );%0A%7D

Fix ready checking