'use strict';

var forEach              = require('es5-ext/object/for-each')
  , ensureObject         = require('es5-ext/object/valid-object')
  , capitalize           = require('es5-ext/string/#/capitalize')
  , resolve              = require('path').resolve
  , _                    = require('mano').i18n.bind('Statistics time per role pdf')
  , db                   = require('../../db')
  , resolveFullStepPath  = require('../../utils/resolve-processing-step-full-path')
  , getDurationDaysHours = require('../../view/utils/get-duration-days-hours')
  , htmlToPdf            = require('../html-to-pdf')

  , root = resolve(__dirname, '../../..')
  , templatePath = resolve(root, 'apps-common/pdf-templates/statistics-time-per-role.html');

module.exports = function (result, config) {
	var processingStepsMeta = ensureObject(ensureObject(config).processingStepsMeta);
	var inserts = { data: [], locale: db.locale,
		logo: config.logo, currentDate: db.DateTime().toString() };

	forEach(result.steps.byStep, function (data, key) {
		var step = data.processing;
		step.label =  db['BusinessProcess' +
			capitalize.call(processingStepsMeta[key]._services[0])].prototype
			.processingSteps.map.getBySKeyPath(resolveFullStepPath(key)).label;

		inserts.data.push(step);
		step.avgTime = getDurationDaysHours(step.avgTime);
		step.minTime = getDurationDaysHours(step.minTime);
		step.maxTime = getDurationDaysHours(step.maxTime);
	});

	var total, processingTotal, correctionTotal, correctionByUsers;
	correctionTotal         = result.steps.all.correction;
	correctionTotal.label   = _("Total correcting time");
	correctionByUsers       = result.steps.all.correction;
	correctionByUsers.label = _("Corrections by the users");
	processingTotal         = result.businessProceesses.processing;
	processingTotal.label   = _("Total process without corrections");

	total                   = result.businessProcesses.processing;
	total.label             = _("Total process");

	[correctionTotal, correctionByUsers, processingTotal, total].forEach(
		function (totalItem) {
			if (!totalItem.count) {
				totalItem.avgTime = totalItem.minTime = totalItem.maxTime = _("N/A");
			} else {
				totalItem.avgTime = getDurationDaysHours(totalItem.avgTime);
				totalItem.minTime = getDurationDaysHours(totalItem.minTime);
				totalItem.maxTime = getDurationDaysHours(totalItem.maxTime);
			}
			inserts.data.push(totalItem);
		}
	);

	return htmlToPdf(templatePath, '', {
		width: "297mm",
		height: "210mm",
		streamable: true,
		templateInserts: inserts
	});
};

@@ -11,16 +11,74 @@
 t';%0A%0Avar
+ copy                 = require('es5-ext/object/copy')%0A  ,
  forEach
@@ -696,19 +696,16 @@
 , '../..
-/..
 ')%0A  , t
@@ -1328,32 +1328,45 @@
 %09%09step.avgTime =
+ step.count ?
  getDurationDays
@@ -1380,24 +1380,30 @@
 tep.avgTime)
+ : '-'
 ;%0A%09%09step.min
@@ -1400,32 +1400,45 @@
 %09%09step.minTime =
+ step.count ?
  getDurationDays
@@ -1452,24 +1452,30 @@
 tep.minTime)
+ : '-'
 ;%0A%09%09step.max
@@ -1472,32 +1472,45 @@
 %09%09step.maxTime =
+ step.count ?
  getDurationDays
@@ -1528,16 +1528,22 @@
 maxTime)
+ : '-'
 ;%0A%09%7D);%0A%0A
@@ -1737,32 +1737,37 @@
 ByUsers       = 
+copy(
 result.steps.all
@@ -1769,32 +1769,33 @@
 s.all.correction
+)
 ;%0A%09correctionByU
@@ -1885,17 +1885,16 @@
 essProce
-e
 sses.pro
@@ -1989,32 +1989,37 @@
               = 
+copy(
 result.businessP
@@ -2037,16 +2037,17 @@
 ocessing
+)
 ;%0A%09total
@@ -2279,16 +2279,11 @@
 e = 
-_(%22N/A%22)
+%22-%22
 ;%0A%09%09

Fix statistics per role pdf render