"use strict";

angular.module('arethusa-core').controller('MainController', function($scope, configurator, state) {
  var conf = configurator.configurationFor('MainController');

  // This is a really really bad solution right now. Using the controller
  // to insert stuff into the state object is not good. Can only stay as
  // a temporary prototype solution.
  var partitionPlugins = function(plugins) {
    $scope.mainPlugins = [];
    $scope.subPlugins = [];

    angular.forEach(plugins, function(plugin, name) {
      var toPush;
      if (plugin.external) {
        /* global externalPlugins */
        toPush = window.externalPlugins[name];
        toPush = angular.extend(toPush, configurator.configurationFor(name));
        $scope.pushPlugin(toPush, toPush.main);
      } else {
        $scope.pushPlugin(name, plugin.main);
      }
    });
  };

  $scope.pushPlugin = function(plugin, main) {
    if (main) {
      $scope.mainPlugins.push(plugin);
    } else {
      $scope.subPlugins.push(plugin);
    }
  };

  $scope.addPlugin = function() {
    $scope.plugins.push("comment");
  };

  $scope.state = state;
  $scope.plugins = Object.keys(conf.plugins);

  partitionPlugins(conf.plugins);

  $scope.template = conf.template;

  $scope.switchTemplate = function() {
    if ($scope.template === "templates/main.html") {
      $scope.template = "templates/main2.html";
    } else {
      $scope.template = "templates/main.html";
    }
  };
});

@@ -85,16 +85,27 @@
 ($scope,
+ $injector,
  configu
@@ -189,448 +189,412 @@
 %0A%0A  
-// This is a really really bad solution right now. Using the controller%0A  // to insert stuff into the state object is not good. Can only stay as%0A  // a temporary prototype solution.%0A  var partitionPlugins = function(plugins) %7B%0A    $scope.mainPlugins = %5B%5D;%0A    $scope.subPlugins = %5B%5D;%0A%0A    angular.forEach(plugins, function(plugin, name) %7B%0A      var toPush;%0A      if (plugin.external) %7B%0A        /* global externalPlugins */%0A        toPush = 
+var partitionPlugins = function(plugins) %7B%0A    $scope.mainPlugins = %5B%5D;%0A    $scope.subPlugins = %5B%5D;%0A%0A    angular.forEach(plugins, function(plugin, name) %7B%0A      var toPush = $scope.retrievePlugin(name, plugin);%0A%0A      $scope.pushPlugin(toPush);%0A      $scope.registerListener(toPush);%0A    %7D);%0A  %7D;%0A%0A  $scope.retrievePlugin = function(name, plugin) %7B%0A    if (plugin.external) %7B%0A      return angular.extend(
 wind
@@ -617,17 +617,17 @@
 ns%5Bname%5D
-;
+,
 %0A       
@@ -631,39 +631,27 @@
     
-toPush = angular.extend(toPush,
+                   
  con
@@ -693,129 +693,58 @@
     
-    $scope.pushPlugin(toPush, toPush.main);%0A      %7D else %7B%0A        $scope.pushPlugin(name, plugin.main
+%7D else %7B%0A      return  $injector.get(name
 );%0A
-  
     %7D%0A
-    %7D);%0A
   %7D;
@@ -786,14 +786,8 @@
 ugin
-, main
 ) %7B%0A
@@ -794,16 +794,23 @@
     if (
+plugin.
 main) %7B%0A
@@ -917,221 +917,387 @@
 %0A%0A  
-$scope.addPlugin = function() %7B%0A    $scope.plugins.push(%22comment%22);%0A  %7D;%0A%0A  $scope.state = state;%0A  $scope.plugins = Object.keys(conf.plugins);%0A%0A  partitionPlugins(conf.plugins);%0A%0A  $scope.template = conf.template
+// This is a really really bad solution right now. Using the controller%0A  // to insert stuff into the state object is not good. Can only stay as%0A  // a temporary prototype solution.%0A  $scope.registerListener = function(plugin) %7B%0A    if (plugin.listener) %7B%0A      state.registerListener(plugin);%0A    %7D%0A  %7D;%0A%0A  $scope.addPlugin = function() %7B%0A    $scope.plugins.push(%22comment%22);%0A  %7D
 ;%0A%0A 
@@ -1505,13 +1505,154 @@
   %7D%0A  %7D;
+%0A%0A  $scope.state = state;%0A  $scope.plugins = Object.keys(conf.plugins);%0A  $scope.template = conf.template;%0A%0A  partitionPlugins(conf.plugins);
 %0A%7D);%0A

Use injector in MainController