'use strict';

/**
 * @ngdoc service
 * @name dormManagementToolApp.DormService
 * @description
 * # DormService
 * Service in the dormManagementToolApp.
 */
angular.module('dormManagementToolApp')
  .service('DormService', function () {
    var mv = this;
    this.residents = [
      {
        "name": "Tom",
        "user_id": 1,
        "garbage_bag_duties": [
          {
            "name": "brown",
            "completed": 5
          },
          {
            "name": "green",
            "completed": 2
          },
          {
            "name": "blue",
            "completed": 3
          }
        ]
      },
      {
        "name": "Jim",
        "user_id": 10,
        "garbage_bag_duties": [
          {
            "name": "brown",
            "completed": 3
          },
          {
            "name": "green",
            "completed": 4
          },
          {
            "name": "blue",
            "completed": 3
          }
        ]
      }
    ];
    this.getNextResponsible = function (bag) {
      var responsible = null;
      var min = Infinity;
      var duty = 0;
      for (var i = 0; i < mv.residents.length; i++) {
        for (var j = 0; j < mv.residents[i].garbage_bag_duties.length; j++) {
          if (mv.residents[i].garbage_bag_duties[j].name == bag && mv.residents[i].garbage_bag_duties[j].completed < min) {
            responsible = i;
            duty = j;
            min = mv.residents[i].garbage_bag_duties[j].completed;
          }
        }
      }
      mv.residents[responsible].garbage_bag_duties[duty].completed++;
      return mv.residents[responsible];
    };
  });

@@ -1013,19 +1013,264 @@
 ion (bag
-) %7B
+_name, current_responsible_name) %7B%0A      var current_resident = $filter('filter')(mv.residents, %7Bname: current_responsible_name%7D)%5B0%5D;%0A      var current_duty = $filter('filter')(current_resident, %7Bname: bag_name%7D)%5B0%5D;%0A      current_duty.completed++;
 %0A      v
@@ -1322,28 +1322,8 @@
 ty;%0A
-      var duty = 0;%0A
     
@@ -1513,16 +1513,21 @@
 e == bag
+_name
  && mv.r
@@ -1612,30 +1612,8 @@
  i;%0A
-            duty = j;%0A
     
@@ -1697,24 +1697,24 @@
       %7D%0A
+
       %7D%0A
       mv
@@ -1709,78 +1709,8 @@
   %7D%0A
-      mv.residents%5Bresponsible%5D.garbage_bag_duties%5Bduty%5D.completed++;%0A
     

Increment duties completed by current responsible before getting the next