'use strict';

var angular = require('angular');
var each = angular.forEach;

var fs = require('fs');

var template = fs.readFileSync(__dirname + '/processes.html', 'utf8');

module.exports = [
  'ViewsProvider',
function (
  ViewsProvider
) {
  ViewsProvider.registerDefaultView('cockpit.dashboard.section', {
    id: 'processes',
    label: 'Processes',
    template: template,
    pagePath: '#/processes',
    checkActive: function (path) {
      // matches "#/process/", "#/processes" or "#/migration"
      return path.indexOf('#/process') > -1 || path.indexOf('#/migration') > -1;
    },
    controller: [
      '$scope',
      'Data',
      'dataDepend',
      'Views',
      'camAPI',
    function(
      $scope,
      Data,
      dataDepend,
      Views,
      camAPI
    ) {
      var processData = $scope.processData = dataDepend.create($scope);

      Data.instantiateProviders('cockpit.dashboard.data', {
        $scope: $scope,
        processData: processData
      });

      var procStats = $scope.procDefStats = {
        definitions: {
          label: [
            'process definition',
            'process definitions'
          ],
          link: '#/processes?targetPlugin=process-definition'
        },
        instances: {
          label: [
            'running instance',
            'running instances'
          ],
          link: '#/processes?targetPlugin=search-process-instances&searchQuery=%5B%7B%22type%22:%22PIunfinished%22,%22operator%22:%22eq%22,%22value%22:%22%22,%22name%22:%22%22%7D%5D'
        },
        incidents: {
          label: [
            'incident',
            'incidents'
          ],
          link: '#/processes?targetPlugin=search-process-instances&searchQuery=%5B%7B%22type%22:%22PIwithIncidents%22,%22operator%22:%22eq%22,%22value%22:%22%22,%22name%22:%22%22%7D%5D'
        }
      };

      $scope.gimmeDaLabel = function (prop) {
        return prop.label[(prop.value === 1) ? 0 : 1];
      };
      $scope.gimmeDaValue = function (count) {
        if (!count) {
          return 'No';
        }
        if (count >= 1000000) {
          return Math.round(count / 1000000) + 'M';
        }
        if (count >= 1000) {
          return Math.round(count / 1000) + 'K';
        }
        return count;
      };
      $scope.loadingState = 'LOADING';
      // should I mention how much I love AngularJS?
      $scope.procDefStatsKeys = Object.keys($scope.procDefStats);

      var processDefinitionService = camAPI.resource('process-definition');
      processDefinitionService.list({
        latest: true
      }, function(err, data) {
        $scope.processDefinitionData = data.items;

        procStats.definitions.value = data.items.length;
      });

      processData.observe('processDefinitionStatistics', function (defStats) {
        $scope.loadingState = 'LOADED';

        procStats.incidents.value = 0;
        procStats.instances.value = 0;

        each(defStats, function (stats) {
          procStats.instances.value += stats.instances || 0;
          procStats.incidents.value += stats.incidents.length;
        });
      });

      $scope.hasMigrationPlugin = false;
      try {
        $scope.hasMigrationPlugin = !!angular.module('cockpit.plugin.migration');
      }
      catch (e) {}
    }],

    priority: 0
  });
}];

@@ -2833,47 +2833,8 @@
 ';%0A%0A
-        procStats.incidents.value = 0;%0A
     
@@ -2868,16 +2868,16 @@
 ue = 0;%0A
+
 %0A       
@@ -2980,28 +2980,41 @@
         
-  procStats.
+%7D);%0A      %7D);%0A%0A      var 
 incident
@@ -3018,52 +3018,181 @@
 ents
-.valu
+Servic
 e 
-+
 = 
-stats.incidents.length;%0A        %7D)
+camAPI.resource('incident');%0A      incidentsService.count(%7B%7D, function (err, data) %7B%0A        if (err) %7B throw err; %7D%0A        procStats.incidents.value = data.count
 ;%0A  

correct number of incidents