'use strict';

var SimpleTableDirective = function(scope, element, attrs, $log){
  this.scope = scope;
  this.element = element;
  this.attrs = attrs;
  this.$log = $log;
  this.plugins = [];

  this.init();
  $log.log("SimpleTable created: ", this.scope);
};
SimpleTableDirective.prototype.init = function(){
  this.addListeners();
  this.validateConfig();
};
SimpleTableDirective.prototype.validateConfig = function() {
  //this.validateFilter();
};
SimpleTableDirective.prototype.validateFilter = function() {
  // Currently not required
  this.$log.log("validateFilter...", this.scope.tableConfig);
  this.scope.$watch("tableConfig.filter", angular.bind(this, this.onFilterUpdated) );
};
SimpleTableDirective.prototype.onFilterUpdated = function(newValue, oldValue) {
  // Currently not required
  if((!newValue && !oldValue) || newValue === oldValue){
    return;
  }
  this.$log.log("onFilterUpdated:", this.scope.tableConfig.filter);
};
SimpleTableDirective.prototype.addListeners = function(){
  //this.scope.$on("$destroy", this.removeListeners());
};
SimpleTableDirective.prototype.removeListeners = function(){
  this.$log.log("removing listeners...", this);
};

SimpleTableDirective.prototype.registerPlugin = function(plugin){
  this.$log.log("initializing plugins...");
  this.plugins.push(plugin);
  _.defer(angular.bind(this, this.initPlugins) );
};

SimpleTableDirective.prototype.initPlugins = function(){
  _.forEach(this.plugins, function(plugin){
    if(plugin.isInitialized() ){ return; }
    plugin.onRegistered();
  });
};


angular.module('simpletable.table', [])
  .directive('stTable', ['$log', function($log) {

    return {
      restrict: 'AE',
      scope: {
        tableConfig: "=",
        tableData: "="
      },
      controller: function($scope, $element, $attrs) {
        return new SimpleTableDirective($scope, $element, $attrs, $log);
      },
      template:
      "<table ng-class='tableConfig.classes'>" +
      "<thead>" +
      " <tr>" +
      "   <th ng-repeat='hcol in tableConfig.columns' ng-style='hcol.style' ng-class='hcol.headerClass'>{{hcol.title}}</th>" +
      " </tr>" +
      "</thead>" +
      "<tbody>" +
      " <tr ng-repeat='row in tableData | filter:tableConfig.filter'>" +
      "   <td ng-repeat='col in tableConfig.columns' ng-class='col.cellClass'>" +
      "     <span ng-if='!col.template'>{{row[col.field]}}</span>     " +
      "     <span ng-if='!!col.template' ng-include='col.template'></span>     " +
      "   </td>" +
      " </tr>" +
      "</tbody>" +
      "</table>"
    };


}]);

@@ -71,16 +71,26 @@
 rs, $log
+, $timeout
 )%7B%0A  thi
@@ -174,16 +174,44 @@
 = $log;%0A
+  this.$timeout = $timeout;%0A
   this.p
@@ -1350,15 +1350,21 @@
 ;%0A  
-_.defer
+this.$timeout
 (ang
@@ -1396,17 +1396,19 @@
 Plugins)
- 
+, 0
 );%0A%7D;%0A%0AS
@@ -1469,9 +1469,15 @@
 %7B%0A  
-_
+angular
 .for
@@ -1666,16 +1666,28 @@
 %5B'$log',
+ '$timeout',
  functio
@@ -1692,16 +1692,26 @@
 ion($log
+, $timeout
 ) %7B%0A%0A   
@@ -1870,16 +1870,16 @@
 ttrs) %7B%0A
-
         
@@ -1940,16 +1940,26 @@
 rs, $log
+, $timeout
 );%0A     

Remove lodash dependency.