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

var module = angular.module('info.directives');

function getMaxHeight(element, elementCss) {
  var container = element.parentElement,
    containerHeight = container.getBoundingClientRect().height,
    containerPaddingTop = document.defaultView.getComputedStyle(container, null).paddingTop || 0,
    outlineMarginVertical = parseInt(elementCss.marginTop) + parseInt(elementCss.marginBottom);

  return containerHeight - parseInt(containerPaddingTop) - outlineMarginVertical;
}

module.directive('contentOutline', function($timeout) {
  return {
    restrict: 'C',
    link: function(scope, element, attrs) {
      var promise = $timeout(function() {
        var outlineCss = document.defaultView.getComputedStyle(element[0], null),
          maxHeight = getMaxHeight(element[0], outlineCss),
          lineHeight = parseInt(outlineCss.lineHeight),
          adjustHeihgt = parseInt((maxHeight + (lineHeight - parseInt(outlineCss.fontSize)) / 2.0)/ lineHeight) * lineHeight;
        
        element[0].children[0].style.maxHeight = adjustHeihgt + 'px';
      }, 0);

      scope.$on('$destroy', function() {
        $timeout.cancel(promise);
      });
    }
  };
});