/**
 * An element which displays an avatar for the given avatar data.
 */
angular.module('quay').directive('avatar', function () {
  var directiveDefinitionObject = {
    priority: 0,
    templateUrl: '/static/directives/avatar.html',
    replace: false,
    transclude: true,
    restrict: 'C',
    scope: {
      'data': '=data',
      'size': '=size'
    },
    controller: function($scope, $element, AvatarService, Config, UIService, $timeout) {
      $scope.AvatarService = AvatarService;
      $scope.Config = Config;
      $scope.isLoading = true;
      $scope.hasGravatar = false;
      $scope.loadGravatar = false;

      $scope.imageCallback = function(r) {
        $timeout(function() {
          $scope.isLoading = false;
          $scope.hasGravatar = r;
        }, 1);
      };

      $scope.$watch('size', function(size) {
        size = size * 1 || 16;
        $scope.fontSize = (size - 4) + 'px';
        $scope.lineHeight = size + 'px';
      });

      $scope.$watch('data', function(data) {
        if (!data) { return; }

        $scope.loadGravatar = Config.AVATAR_KIND == 'gravatar' &&
          (data.kind == 'user' || data.kind == 'org');

        $scope.isLoading = $scope.loadGravatar;
        $scope.hasGravatar = false;
      });
    }
  };
  return directiveDefinitionObject;
});