poconsole.controller('dashboardCtrl', function($scope, $route, $http, Page, $log, Tag) {
	Page.setTitle('Dashboard');
    Page.setPage('dashboard');
    $scope.loadDashboard = function(){
        $scope.loading = true;
        var getCurrentValues = Tag.getCurrentValues();
        getCurrentValues.then(function(data) {
        	$scope.loading = false;
        	$scope.vals = data;
            $log.info($scope.vals);
        });
    };
    $scope.loadDashboard();
});

poconsole.controller('todaysTotalsCtrl', function($scope, $route, $http, Page, $log, Tag) {
	Page.setTitle('Totals');
    Page.setPage('dashboard');
    $scope.loadTotals = function(){
        $scope.loading = true;
        var getTodayTotals = Tag.getDailyTotals();
        getTodayTotals.then(function(d){
            $scope.loading = false;
            $scope.total_values = d;
        });
    };
    $scope.loadTotals();
});


poconsole.controller('latestStrokeCtrl', function($scope, $routeParams, Card, Page, Tag) {
    Page.setTitle('Latest Card');
    Page.setPage('dashboard');


    var getLatestCard = Card.getLatestCard();
    getLatestCard.then(function(cData){
        $scope.stroke_time = new Date(cData.created_on);
        $scope.stroke_number = cData.stroke_number;
        $scope._id = cData._id;
        $scope.stroke_type = cData.stroke_type;
        var surf_pos = JSON.parse(cData.surf_pos);
        var surf_lod = JSON.parse(cData.surf_lod);
        var down_pos = JSON.parse(cData.down_pos);
        var down_lod = JSON.parse(cData.down_lod);

        var cd = $scope.stroke_time;
        $scope.current_date = cd.getFullYear() + "-" + (cd.getMonth()+1) + "-" + cd.getDate();

        var surface_card = surf_pos.map(function(a, i){
            return {position:surf_pos[i], load:surf_lod[i]};
        });

        var downhole_card = down_pos.map(function(a, i){
            return {position:down_pos[i], load:down_lod[i]};
        });

        $scope.card_graph_data = {
          surface: surface_card,
          downhole: downhole_card
        };

        var graphOptions = Card.getCardGraphOptions(surface_card, downhole_card);
        $scope.surfaceOptions = graphOptions.surf;
        $scope.downholeOptions = graphOptions.down;

        var getLatestTags = Tag.getCurrentValues();
        getLatestTags.then(function(tagdata){
			console.log(tagdata);
			tagdata = tagdata.vals;
            $scope.tagData = {};
            for(var i=0; i < tagdata.length; i++){
                $scope.tagData[tagdata[i].tag_name] = tagdata[i];
                $scope.tagData[tagdata[i].tag_name].created_on = Date.create($scope.tagData[tagdata[i].tag_name].created_on);
                $scope.tagData[tagdata[i].tag_name].percentage = (tagdata[i].max_expected - tagdata[i].value) / (tagdata[i].max_expected - tagdata[i].min_expected) * 100.0;
            }

        });
    });
});
