﻿"use strict";

app.controller("reviewMyBallotController", ["$rootScope", "$scope", "$routeParams", "categoryDataService", "categorySubmissionDataService", "submissionDataService", "judgeRulesService", "votingDataService", "votingService",
    function ($rootScope, $scope, $routeParams, categoryDataService, categorySubmissionDataService, submissionDataService, judgeRulesService, votingDataService, votingService) {
        $scope.judgeRulesService = judgeRulesService;

        $scope.categories = [];
        $scope.submissions = [];
        $scope.votes = [];
        $scope.votingService = votingService;

        // Override at show level.
        $scope.showSubmissionRunningTime = false;

        // ----- Helpers
        $scope.init = function () {
            $scope.getCategories();
        };
        $scope.isActive = function (catId) {
            return ($scope.currentCategory.id == catId);
        };
        $scope.getCategories = function () {
            categoryDataService.getCategories().then(function (results) {
                if (results.data.length > 0) {
                    for (var i = 0, iCount = results.data.length; i < iCount; i++) {
                        results.data[i].categoryName = judgeRulesService.getCategoryName(results.data[i]);
                        results.data[i].votes = [];
                    }

                    $scope.categories = results.data;

                    var categoriesWithVotes = [];
                    for (var i = 0; i < results.data.length; i++) {
                        if (results.data[i].voteCount > 0) {
                            categoriesWithVotes.push(results.data[i]);
                        }
                    }
                    $scope.categoriesWithVotes = categoriesWithVotes;

                    $scope.getVotes();
                }
            },
            function (error) {
                console.error('error on getCategories');
            }).finally(function () {
                $scope.finishedLoading = true;
            });
        };
        $scope.getVotes = function () {
            votingDataService.getVotesForContact().then(function (results) {
                if (results !== void 0) {
                    for (var i = 0; i < results.data.length; i++) {
                        results.data[i].thirdName = judgeRulesService.getThirdName(results.data[i].nomineeDetails);
                        results.data[i].bio = judgeRulesService.getBio(results.data[i].nomineeDetails);
                    }

                    $scope.votes = results.data;

                    for (var i = 0; i < $scope.categoriesWithVotes.length; i++) {
                        for (var j = 0; j < $scope.votes.length; j++) {
                            if ($scope.votes[j].categoryId == $scope.categoriesWithVotes[i].id) {
                                $scope.categoriesWithVotes[i].votes.push($scope.votes[j]);
                            }
                        }
                    }
                }
            },
            function (error) {
                console.error('error on getVotesForContact');
            }).finally(function () {
                $scope.finishedLoading = true;
            });
        };

        // ----- Main code
        $scope.init();
    }]);