'use strict';

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

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

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

	return deferred.map(Object.keys(result.byStep), function (key) {
		var step = {}, total;
		step.label =  db['BusinessProcess' +
			capitalize.call(processingStepsMeta[key]._services[0])].prototype
			.processingSteps.map.getBySKeyPath(resolveFullStepPath(key)).label;

		inserts.steps.push(step);
		total = result.byStep[key].processing;
		total.fullName = _("Total & times");
		total.avgTime = getDurationDaysHours(total.totalTime / total.count);
		total.minTime = getDurationDaysHours(total.minTime);
		total.maxTime = getDurationDaysHours(total.maxTime);
		return deferred.map(Object.keys(result.byStepAndProcessor[key]), function (userId) {
			var item = result.byStepAndProcessor[key][userId].processing;
			item.avgTime = getDurationDaysHours(item.avgTime);
			item.minTime = getDurationDaysHours(item.minTime);
			item.maxTime = getDurationDaysHours(item.maxTime);

			return getUserFullName(userId)(function (fullName) {
				item.fullName = fullName;
				return { processing: item };
			});
		})(function (data) {
			step.data = data;
			data.push({ processing: total });
		});
	})(function () {
		return htmlToPdf(templatePath, '', {
			width: "297mm",
			height: "210mm",
			streamable: true,
			templateInserts: inserts
		});
	});
};

@@ -1415,32 +1415,46 @@
 %09total.avgTime =
+ total.count ?
  getDurationDays
@@ -1469,32 +1469,22 @@
 tal.
-totalTime / total.count)
+avgTime) : '-'
 ;%0A%09%09
@@ -1490,32 +1490,46 @@
 %09total.minTime =
+ total.count ?
  getDurationDays
@@ -1544,24 +1544,30 @@
 tal.minTime)
+ : '-'
 ;%0A%09%09total.ma
@@ -1565,32 +1565,46 @@
 %09total.maxTime =
+ total.count ?
  getDurationDays
@@ -1619,24 +1619,30 @@
 tal.maxTime)
+ : '-'
 ;%0A%09%09return d
@@ -1796,24 +1796,37 @@
 em.avgTime =
+ item.count ?
  getDuration
@@ -1844,24 +1844,30 @@
 tem.avgTime)
+ : '-'
 ;%0A%09%09%09item.mi
@@ -1869,24 +1869,37 @@
 em.minTime =
+ item.count ?
  getDuration
@@ -1921,16 +1921,22 @@
 minTime)
+ : '-'
 ;%0A%09%09%09ite
@@ -1946,16 +1946,29 @@
 axTime =
+ item.count ?
  getDura
@@ -1994,16 +1994,22 @@
 maxTime)
+ : '-'
 ;%0A%0A%09%09%09re

Fix per person times stats render