poconsole.controller('cardListCtrl', function($scope, $window, $routeParams, Card, Page) {
    Page.setTitle('Card List');
    Page.setPage('cards');
    var multipleCardList = [];
    $scope.checkStatus = false;
    var getDateList = Card.getCardDates();
    getDateList.then(function(dates) {
        $scope.dates = dates;

        var date_param;
        if (!$routeParams.date_param){
            date_param = dates[0].ind;
        } else {
            date_param = $routeParams.date_param;
        }

        var page;
        if (!$routeParams.page){
            page = 1;
        } else {
            page = $routeParams.page;
        }

        $scope.getDatePage(date_param, page);
    });

    $scope.getDatePage = function(date_p, page_number){
        var getCardPage = Card.getCardPageForDate(date_p, page_number);
        getCardPage.then(function(d){
            for(var j = 0; j < d.cards.length; j++){
                d.cards[j].created_on = Date.create(d.cards[j].created_on);
            }
            $scope.cards = d.cards;
            $scope.num_pages = d.num_pages;
            $scope.per_page = d.per_page;
            $scope.page_list = [];
            $scope.page_num = page_number;
            $scope.total = d.total;

            $scope.date_param = date_p;
        });
    };

    $scope.addCardToMultiple = function(card_id){
        var mcl_index = multipleCardList.indexOf(parseInt(card_id));
        var inMultiple = mcl_index != -1;
        if (!inMultiple){
            multipleCardList.push(parseInt(card_id));
        } else {
            multipleCardList.splice(mcl_index, 1);
        }
    };

    $scope.compareMultipleCards = function(){
        if (multipleCardList.length > 1){
            $window.location.href = '/#/multiplecards/' + multipleCardList.join(',');
        }
    };
});

poconsole.controller('cardDataCtrl', function($scope, $routeParams, Card, Page, Tag) {
    Page.setTitle('Card Data');
    Page.setPage('cards');

    var getDateList = Card.getCardDates();
    getDateList.then(function(dates) {
        $scope.dates = dates;
    });


    var getCard = Card.getCard($routeParams.cardID);
    getCard.then(function(cData){
        console.log(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();
        console.log($scope.current_date);

        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 getTagsAtTime = Tag.getTagsAtTime(cData.created_on);
        getTagsAtTime.then(function(tagdata){
            $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;
            }
            console.log($scope.tagData);

        });
    });
});


poconsole.controller('multipleCardDataCtrl', function($scope, $routeParams, Card, Page, Tag) {
    Page.setTitle('Card Data');
    Page.setPage('cards');

    var getDateList = Card.getCardDates();
    getDateList.then(function(dates) {
        $scope.dates = dates;
    });

    var makeCard = function(position_array, load_array){
        /**
         * [makes makes an array of {load:x, position:y} objects]
         * @position_array {[array]}
         * @load_array {{array}}
         */
        return position_array.map(function(a, i){
            return {position:position_array[i], load:load_array[i]};
        });
    };

    var getCards = Card.getMultipleCards($routeParams.cardIDs.split(','));
    getCards.then(function(cData){
        $scope.card_graph_data = {};
        $scope.cards = [];
        for(var i = 0; i < cData.length; i++){
            var card = {};
            card.stroke_time = new Date(cData[i].created_on);
            card.stroke_number = cData[i].stroke_number;
            card._id = cData[i]._id;
            card.stroke_type = cData[i].stroke_type;
            var surf_pos = JSON.parse(cData[i].surf_pos);
            var surf_lod = JSON.parse(cData[i].surf_lod);
            var down_pos = JSON.parse(cData[i].down_pos);
            var down_lod = JSON.parse(cData[i].down_lod);
            var surface_card = makeCard(surf_pos, surf_lod);
            var downhole_card = makeCard(down_pos, down_lod);
            $scope.cards.push(card);

            $scope.card_graph_data['surface_' + cData[i]._id] = surface_card;
            $scope.card_graph_data['downhole_' + cData[i]._id] = downhole_card;

        }
        
        var graphOptions = Card.getCardGraphOptionsMultiple($scope.card_graph_data);
        $scope.surfaceOptions = graphOptions.surf;
        $scope.downholeOptions = graphOptions.down;

    });
});
