/*global angular */
/*jslint white: true */

( function() {'use strict';

  function TasksListController($location, $routeParams, $scope, activeItem, tagsArray, tasksArray, tasksRequest, tasksResponse, userPrefix) {

    $scope.taskEdit = function(task) {
      $location.path(userPrefix.getPrefix() + '/tasks/edit/' + task.uuid);
    };

    $scope.taskChecked = function(task) {

      if (task.completed) {

        tasksArray.deleteTaskProperty(task, 'completed');

        tasksRequest.uncompleteTask(task).then(function(uncompleteTaskResponse) {
          tasksResponse.putTaskContent(task, uncompleteTaskResponse);
        });

      } else {

        tasksRequest.completeTask(task).then(function(completeTaskResponse) {
          tasksResponse.putTaskContent(task, completeTaskResponse);
        });

      }
    };

    $scope.taskToProject = function(task) {
      $location.path(userPrefix.getPrefix() + '/tasks/new');
      activeItem.setItem(task);
      task.project=true;

      tasksRequest.putExistingTask(task).then(function(putExistingTaskResponse) {
        tasksResponse.putTaskContent(task, putExistingTaskResponse);
      });
    };

    $scope.deleteTask = function(task) {

      tasksArray.removeTask(task);

      tasksRequest.deleteTask(task).then(function(deleteTaskResponse) {
        tasksResponse.putTaskContent(task, deleteTaskResponse);
      });
    };

    $scope.addSubtask = function() {

      if ($routeParams.uuid) {
        $scope.subtask.relationships = {};

        if (tasksArray.getProjectByUUID($routeParams.uuid)) {

          $scope.subtask.relationships.parentTask = $routeParams.uuid;

        } else if (tagsArray.getTagByUUID($routeParams.uuid)) {

          $scope.subtask.relationships.tags = [];
          $scope.subtask.relationships.tags[0] = $routeParams.uuid;
        }
      }

      tasksArray.putNewTask($scope.subtask);

      tasksRequest.putTask($scope.subtask).then(function(putTaskResponse) {
        tasksResponse.putTaskContent($scope.subtask, putTaskResponse);

        $scope.subtask = {};
      });
    };
  }

  TasksListController.$inject = ['$location', '$routeParams', '$scope', 'activeItem', 'tagsArray', 'tasksArray', 'tasksRequest', 'tasksResponse', 'userPrefix'];
  angular.module('em.app').controller('TasksListController', TasksListController);
}());

@@ -1416,19 +1416,54 @@
 unction(
-) %7B
+subtask) %7B%0A%0A      $scope.subtask = %7B%7D;
 %0A%0A      
@@ -1487,39 +1487,32 @@
 uuid) %7B%0A        
-$scope.
 subtask.relation
@@ -1589,39 +1589,32 @@
 )) %7B%0A%0A          
-$scope.
 subtask.relation
@@ -1719,39 +1719,32 @@
 )) %7B%0A%0A          
-$scope.
 subtask.relation
@@ -1766,31 +1766,24 @@
 ;%0A          
-$scope.
 subtask.rela
@@ -1844,54 +1844,8 @@
  %7D%0A%0A
-      tasksArray.putNewTask($scope.subtask);%0A%0A
     
@@ -1867,23 +1867,16 @@
 putTask(
-$scope.
 subtask)
@@ -1946,23 +1946,16 @@
 Content(
-$scope.
 subtask,
@@ -1982,36 +1982,48 @@
         
-$scope.
+tasksArray.putNewTask(
 subtask
- = %7B%7D;
+);%0A
 %0A      %7D

Clear subtask textbox immediately after submit.