/**
 * Welcome to Pebble.js!
 *
 * This is where you write your app.
 */

var UI          = require('ui'),
Vector2         = require('vector2'),
Settings        = require('settings'),
// placeHolderList = JSON.parse(Settings.option('taskList')) || require('placeHolderList'),
taskListArray = require('placeHolderList'),
incompleteTasks = function () {
  return taskListArray.filter(function (value) {
    return !value.extras || !value.extras.hasOwnProperty('completed');
  });
},
homeCard        = new UI.Card({
  title: 'Honeybees',
  icon: 'images/logo28.png',
  subtitle: 'Hello World!',
  body: 'Press any button.'
}),
noTasksCard     = new UI.Card({
  title: 'Honeybees',
  icon: 'images/logo28.png',
  subtitle: 'Hello World!',
  body: 'You\'re good, for now.'
}),
decisionMenuConfig = {
  sections: [{
    title: 'Finished Task?',
    items: [{
      title: 'Completed',
      icon: 'IMAGE_CHECK',
      subtitle: 'I love you Rob!'
    }, {
      title: 'Later',
      icon: 'IMAGE_X',
      subtitle: 'Superdry'
    }]
  }]
},
snoozeMenuConfig = {
  sections: [{
    title: 'Snooze Duration',
    items: [{
      title: '1 hour'
    }, {
      title: '1 day'
    }]
  }]
});

homeCard.show();

homeCard.on('click', 'select', function (e) {
  var taskListMenu;
  if (!taskListMenu && placeHolderList.length) {
    taskListMenu = new UI.Menu({
      sections: [{
        title: 'Get yo ship together',
        items: placeHolderList
      }]
    });
  } else {
    noTasksCard.show();
  }

  taskListMenu.show();
  taskListMenu.on('select', function (e) {
    var taskCard;
    if (!taskCard) {
      taskCard = new UI.Card({
        taskId: e.item.taskId,
        title: e.item.title,
        subtitle: e.item.subtitle,
        body: e.item.body,
        style: 'small',
        scrollable: true
      });
    }
    taskCard.show();
    taskCard.on('click', 'select', function () {
      var decisionMenu = new UI.Menu(decisionMenuConfig);
      decisionMenu.show();
      decisionMenu.on('select', function (e) {
        var selection = e.item.title,
        taskId = taskCard.state.taskId;

        if (selection === 'Completed') {
          updateTask(taskId, { 'completed': true });
          decisionMenu.hide();
          taskCard.hide();
          taskListMenu.items(0, incompleteTasks()); // Finds section of task menu with index 0 and replaces all items.
          Settings.option('taskList', JSON.stringify(taskListArray)); // Persist updated list in localStorage.

          if (!incompleteTasks().length) {
            taskListMenu.hide();
            noTasksCard.show();
          }
        } else if (selection === 'Later') {
          var snoozeMenu = new UI.Menu(snoozeMenuConfig);
          snoozeMenu.show();
          snoozeMenu.on('select', function (e) {
            var duration = e.item.title;
            if (duration === '1 hour') updateTask(taskId, { 'snooze': 1 });
            if (duration === '1 day') updateTask(taskId, { 'snooze': 24 });
            snoozeMenu.hide();
            decisionMenu.hide();
            taskCard.hide();
          });
        }
      });
    });
  });
});

function updateTask (id, options) {
  for (var i = 0; i < taskListArray.length; i++) {
    if (taskListArray[i].taskId === id) {
      taskListArray[i].extras = options;
      return;
    }
  }
}

@@ -1174,19 +1174,18 @@
 %5D%0A  %7D%5D%0A%7D
-)
 ;%0A
+
 %0AhomeCar
@@ -1284,31 +1284,29 @@
 Menu && 
-placeHolderList
+taskListArray
 .length)
@@ -1414,31 +1414,29 @@
  items: 
-placeHolderList
+taskListArray
 %0A      %7D

Update placeHolderList to taskListArray.