'use strict';

angular.module('myApp.task', ['ngRoute','timer','appFilters'])

.controller('taskController', ['$scope', '$http', function($scope, $http) {

	function timerDataToUnix(data) {
		var msPsec = 1000;
		var secPmin = 60;
		var minPhr = 60;
		var hrPday = 24;
		return data.millis + msPsec*data.seconds + msPsec*secPmin*data.minutes + msPsec*secPmin*minPhr*data.hours + msPsec*secPmin*minPhr*hrPday *data.days;
	}

	$scope.isActive=false;
	$scope.timerStarted=false;
	
	$scope.taskLink = function(){	
	}

	$scope.getTaskTime = function(){
		if($scope.task.key){
			// var taskID = ;
			$http({
				method:  'GET',
				url: 	 '/task/getTaskTime',
				params:  { task_id: $scope.task.key},
				headers: {'Content-Type': 'application/json'}

			}).then(function successCallback(res){		
				if(res.data.logged_time > 0 ) $scope.timeLogged = res.data.logged_time;
			}, function errorCallback(res){
				console.log(res);
			});	
		}
	}

	$scope.getTaskTime();
	
	$scope.stateLog = function(){
		console.log('Timer started:'+$scope.timerStarted);
		console.log('Active state:'+$scope.isActive);
	}

	$scope.$on('timer-stopped', function (event, logged_time){
		var date = new Date();
		var response = {
			task_id: $scope.task.key,
			start_time: date.getTime(),
			end_time: date.getTime()+timerDataToUnix(logged_time)
		}
		// send data to databse
		$http({
			method: 'POST',
			url: 	'/task/trackTime',
			data: 	JSON.stringify(response),
			headers: {
			  	'Content-Type': 'application/json',
			}

		}).then(function successCallback(res){
			console.log(res.data);
			// console.log('Timer Stopped - data = ', data);
			$scope.usrAccountData = res.data;

		}, function errorCallback(res){
			console.log('Warning Will Robinson');
		});
	});
	
	$scope.timerToggle = function(){
		if(!$scope.timerStarted){
			$scope.$broadcast('timer-start');
			$scope.timerStarted=true;
			$scope.isActive=true;
			$scope.getTaskTime();
		}else{
			$scope.$broadcast('timer-stop');
			$scope.timerStarted=false;
			$scope.isActive=false;
			$scope.getTaskTime();
		}
	}

}]);
@@ -1155,16 +1155,41 @@
 _time)%7B%0A
+%09%09console.log('logged');%0A
 %09%09var da
@@ -1205,16 +1205,16 @@
 Date();%0A
-
 %09%09var re
@@ -1345,16 +1345,17 @@
 me)%0A%09%09%7D%0A
+%0A
 %09%09// sen
@@ -1482,15 +1482,8 @@
 s: %7B
-%0A%09%09%09  %09
 'Con
@@ -1512,21 +1512,16 @@
 on/json'
-,%0A%09%09%09
 %7D%0A%0A%09%09%7D).
@@ -1586,60 +1586,8 @@
 a);%0A
-%09%09%09// console.log('Timer Stopped - data = ', data);%0A
 %09%09%09$
@@ -1706,17 +1706,16 @@
 );%0A%09%7D);%0A
-%09
 %0A%09$scope
@@ -1737,24 +1737,25 @@
 function()%7B%0A
+%0A
 %09%09if(!$scope

Update timer filter