/**
 * ProjectsStatisticsCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('ProjectsStatisticsCtrl', ['$scope', 'Files', 'ErrorsHandler', function ($scope, Files, ErrorsHandler) {

	// ------------------------------------------------------------------------
	//
	//	SCOPE VARIABLES BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.duration_piechart = {
		type: 'PieChart',
		displayed: true,
		options: {
			title: 'Project duration breakdown',
			pieHole: 0.3,
			legend: { position: 'labeled' }
		},
		data: {
			cols: [
				{
					id: 'action',
					label: 'Action',
					type: 'string'
				},
				{
					id: 'count',
					label: 'Count',
					type: 'number'
				}
			],
			rows: [
				{
					"c": [{ v: "Planning" }, { v: 5, f: "5 days" }]
				},
				{
					"c": [{ v: "In progress" }, { v: 10, f: "10 days" }]
				},
				{
					"c": [{ v: "Send invoice" }, { v: 2, f: "2 days" }]
				},
				{
					"c": [{ v: "Paid" }, { v: 14, f: "14 days" }]
				},
				{
					"c": [{ v: "Feedback" }, { v: 1, f: "1 days" }]
				}
			]
		}
	};

	$scope.expenses_chart = {
		type: 'BarChart',
		displayed: true,
		options: {
			title: 'Project costs breakdown',
			isStacked: false
		},
		data: {
			cols: [
				{
					id: 'action',
					label: 'Action',
					type: 'string'
				},
				{
					id: 'count',
					label: 'Costs',
					type: 'number'
				},
				{
					id: 'budget',
					label: 'Budget',
					type: 'number'
				},
			],
			rows: [
				{
					"c": [{ v: "Planning" }, { v: 5, f: "£5" }, { v: 10 }]
				},
				{
					"c": [{ v: "In progress" }, { v: 10, f: "£10" }, { v: 15 }]
				},
				{
					"c": [{ v: "Send invoice" }, { v: 2, f: "£2" }]
				}
			]
		}
	};

	$scope.tasks_chart = {
		type: 'Timeline',
		displayed: true,
		options: {
			title: 'Tasks timeline',
			height: 300
		},
		data: {
			cols: [
				{
					id: 'task',
					label: 'Task',
					type: 'string'
				},
				{
					id: 'start',
					label: 'Date',
					type: 'date'
				},
				{
					id: 'end',
					label: 'End',
					type: 'date'
				},
			],
			rows: []
		}
	};

	// ------------------------------------------------------------------------
	//
	//	WATCHERS AND EVENT LISTENERS BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.$watch('data.tasks', function (value) {
		if (value) {
			var data = [];
			_.each(value, function (el) {
				data.push({
					c: [{ v: el.title }, { v: moment(el.created_at).toDate() }, { v: moment(el.updated_at).toDate() }]
				});
			});

			$scope.tasks_chart.data.rows = data;
		}
	}, true);

	// ------------------------------------------------------------------------
	//
	//	FUNCTIONS BELOW THIS LINE -->
	//
	// ------------------------------------------------------------------------


}]);
