'use strict';
/**
 * Checklist-model
 * AngularJS directive for list of checkboxes
 */

angular.module('checklist-model', [])
.directive('checklistModel', ['$parse', '$compile', function($parse, $compile) {
  

  // contains
  function contains(arr, item, compareOn) {
    if (angular.isArray(arr)) {

      for (var i = arr.length; i--;) {
        var boolArray = [];
        for (var j = compareOn.length; j--;) {
          boolArray.push(arr[i][compareOn[j]] === item[compareOn[j]]);
        }

        if (angular.equals(arr[i], item) || boolArray.every(Boolean) ) {
          return true;
        }
      }
    }
    return false;
  }

  // add
  function add(arr, item, compareOn) {
    arr = angular.isArray(arr) ? arr : [];
    if (contains(arr, item, compareOn)) {
      return arr;
    }
    arr.push(item);
    return arr;
  }  

  // remove
  function remove(arr, item, compareOn) {
    if (angular.isArray(arr)) {
      for (var i = arr.length; i--;) {
        var boolArray = [];
        for (var j = compareOn.length; j--;) {
          boolArray.push(arr[i][compareOn[j]] === item[compareOn[j]]);
        }

        if (angular.equals(arr[i], item) || boolArray.every(Boolean) ) {
          arr.splice(i, 1);
          break;
        }
      }
    }
    return arr;
  }

  // http://stackoverflow.com/a/19228302/1458162
  function postLinkFn(scope, elem, attrs) {
    // compile with `ng-model` pointing to `checked`
    $compile(elem)(scope);

    // getter / setter for original model
    var getter = $parse(attrs.checklistModel);
    var setter = getter.assign;

    // evaluates the array passed into the attribute
    var compareOn = scope.$eval(attrs.checklistCompareOn);
    
    // value added to list
    var value = $parse(attrs.checklistValue)(scope.$parent);

    // watch UI checked change
    scope.$watch('checked', function(newValue, oldValue) {
      if (newValue === oldValue) { 
        return;
      } 
      var current = getter(scope.$parent);
      if (newValue === true) {
        setter(scope.$parent, add(current, value, compareOn));
      } else {
        setter(scope.$parent, remove(current, value, compareOn));
      }
    });

    // watch original model change
    scope.$parent.$watchCollection(attrs.checklistModel, function(newArr, oldArr) {
      scope.checked = contains(newArr, value, compareOn);
    });
  }

  return {
    restrict: 'A',
    priority: 1000,
    terminal: true,
    scope: true,
    compile: function(tElement, tAttrs) {
      if (tElement[0].tagName !== 'INPUT' || !tElement.attr('type', 'checkbox')) {
        throw 'checklist-model should be applied to `input[type="checkbox"]`.';
      }

      if (!tAttrs.checklistValue) {
        throw 'You should provide `checklist-value`.';
      }

      // exclude recursion
      tElement.removeAttr('checklist-model');
      
      // local scope var storing individual checkbox model
      tElement.attr('ng-model', 'checked');

      return postLinkFn;
    }
  };
}]);