var angular = require('angular');
require('../modules');

var module = angular.module('commons.directives.longpress');

module.directive('onLongPress', function() {
  return {
    restrict: 'A',
    link: function(scope, elm, attrs) {
      
      elm.bind('touchstart', function() {
        scope.longPress = true;
        scope.startLongPress = false;
        setTimeout(function() {
          if (scope.longPress) {
            scope.$apply(function() {
              scope.startLongPress = true;
              scope.$eval(attrs.onLongPress);
            });
          }
        }, 1000);
      });

      elm.bind('touchend', function(event) {
        scope.longPress = false;
        if (attrs.onShortPress && !scope.startLongPress) {
          scope.$apply(function() {
            scope.$eval(attrs.onShortPress);
          });
        }
        if (attrs.onTouchEnd) {
          scope.$apply(function() {
            scope.$eval(attrs.onTouchEnd);
          });
        }
        
      });
    }
  };
});
