poconsole.controller('tagValsCtrl', function($scope, $route, $http, $routeParams, Page, $log, tags, dateConversion) {
	Page.setTitle('Tag Series');
    Page.setPage('tags');

    var colors = ['#d7191c','#fdae61','#abdda4','#2b83ba'];

    if ($routeParams.endDatetime){
        $scope.endDatetime = Date.parse(dateConversion.sqliteDate($routeParams.endDatetime));
    } else {
        $scope.endDatetime = new Date();
    }

    if ($routeParams.startDatetime){
        $scope.startDatetime = Date.parse(dateConversion.sqliteDate($routeParams.startDatetime));
    } else {
        $scope.startDatetime = new Date();
        $scope.startDatetime.setHours($scope.endDatetime.getHours() - 2);
    }
    $log.info({start: $scope.startDatetime, end:$scope.endDatetime});

    $scope.loadTagVals = function(sDTime, eDTime){
        $scope.loading = true;
        var tags_to_get = $routeParams.tagID.split(",");
    	var getTag = tags.getTag(tags_to_get);
        $scope.tags_to_get = tags_to_get.toString();
    	getTag.then(function(tagData){
			$scope.tag = tagData.tag;
		    var getTagHistoryBetween = tags.getTagHistoryBetween(tags_to_get, sDTime, eDTime);
		    getTagHistoryBetween.then(function(data) {

		    	$scope.loading = false;
	    		$scope.data = data;

				$scope.table_vals = $scope.data.vals.map(function(x){
					var ob =  {id: x.id, tagID: x.tagID, dtime: new Date(x.createdAt)};
                    for(var t = 0; t< $scope.tag.length; t++){
                        if ($scope.tag[t].name === x.tagID.name){
                            ob[$scope.tag[t].name] = x.val;
                        } else {
                            ob[$scope.tag[t].name] = null;
                        }
                    }

                    return ob;
				});

                var split_vals = {};
                var all_datetimes = [];
                for(var t=0; t< $scope.tag.length; t++){
                    split_vals[$scope.tag[t].name] = {};
                    split_vals[$scope.tag[t].name].dtimes = [];
                    split_vals[$scope.tag[t].name].vals = [];
                    split_vals[$scope.tag[t].name].ob = {};
                }
                for (var i = 0; i< data.vals.length; i++){
                    split_vals[data.vals[i].tagID.name].ob[data.vals[i].createdAt]= data.vals[i].val;
                    split_vals[data.vals[i].tagID.name].dtimes.push(data.vals[i].createdAt);
                    split_vals[data.vals[i].tagID.name].vals.push(data.vals[i].val);
                    if (all_datetimes.indexOf(data.vals[i].createdAt) == -1){
                        all_datetimes.push(data.vals[i].createdAt);
                    }
                }
                $scope.data.vals = [{dtime:new Date(all_datetimes[0])}];
                for (var ta in split_vals){
                    $scope.data.vals[0][ta] = split_vals[ta].vals[0];
                }

                for (var j = 0; j < all_datetimes.length; j++){
                    var newOb = {dtime: new Date(all_datetimes[j])};
                    for(var tag in split_vals){
                        if(split_vals[tag].ob[all_datetimes[j]] === undefined){
                            newOb[tag] = $scope.data.vals[j][tag];
                        } else {
                            newOb[tag] = split_vals[tag].ob[all_datetimes[j]];
                        }
                    }
                    $scope.data.vals.push(newOb);
                }

	    		$scope.error = false;
	    		$scope.options = {
					series: [

					],
					axes: {
						x: {
							key: "dtime",
							type: "date"
						}
					}
				};
                for(var i2 = 0; i2< $scope.tag.length; i2++){
                    $scope.options.series.push({
                        axis: "y",
                        dataset: "vals",
                        key: $scope.tag[i2].name,
                        label: $scope.tag[i2].name,
                        // color: "#1f77b4",
                        color: colors[i2 % colors.length],
                        type: ['line'],
                        drawDots:false
                    });
                }
		    });
    	});
    };
    $scope.loadTagVals($scope.startDatetime, $scope.endDatetime);
});
