'use strict';

var VoteDetailsController =  angular.module('polls.controllers.vote', [
    'system'
])
    .controller('VoteDetailsController', [
        '$scope',
        '$location',
        '$route',
        '$window',
        'System',
        'PollService',
        function ($scope, $location, $route, $window, System, PollService) {
            var id = $route.current.params["id"];
            $scope.query = $route.current.params || {};
            $scope.currentView = 'view';
            $scope.my = { favorite: "" };
            $(".tooltip").tooltip();
            System.Page.title($scope.$resource("polls.poll"));
            $scope.refresh = function () {
                PollService.questionForEdit(id).then(function (data) {
                    $scope.question = data;
                    PollService.answerByQuestionId($scope.question.ID).then(function (data) {
                        $scope.answers = data.answers;
                        var chart1 = {};
                        chart1.data = {
                            "cols": [
                      { id: "ID", label: "Pitanja", type: "string" },
                      { id: "IDVotePercentage", label: "Брј гласова", type: "number" },
                      { id: "IDVote", label: "Постотак", type: "number" }

                            ], "rows": []
                        };
                        for (var i = 0; i < $scope.answers.length; i++) {
                            chart1.data.rows.push({ c: [{ v: $scope.answers[i].Title }, { v: $scope.answers[i].VoteCount }, { v: $scope.answers[i].VotePercentage }] });

                            var formatCollection = [
                                     {
                                         name: "color",
                                         format: [
                                             {
                                                 columnNum: [i],
                                                 formats: [
                                                     {
                                                         from: 0,
                                                         to: 3,
                                                         color: $scope.answers[i].Color,
                                                         bgcolor: "red"
                                                     }
                                                 ]
                                             }
                                         ]
                                     }
                            ]
                        }
                        chart1.type = "PieChart";
                        chart1.displayed = false;
                        chart1.options = {
                            "title": $scope.question.Title,
                            "isStacked": "true",
                            "fill": 20,
                            "displayExactValues": true,
                            "vAxis": {
                                "title": $scope.question.Title, "gridlines": { "count": $scope.question.VoteCount }
                            },
                            "hAxis": {
                                "title": $scope.question.PublishedOn
                            }
                        };
                        $scope.formatCollection = formatCollection;
                        chart1.formatters = {};

                        $scope.chart = chart1;
                        $scope.cssStyle = "height:600px; width:100%;";
                    });

                });

            };

            $scope.chartSelectionChange = function ()
            {
                if (($scope.chart.type === 'Table' && $scope.chart.data.cols.length === 6 && $scope.chart.options.tooltip.isHtml === true) ||
                    ($scope.chart.type != 'Table' && $scope.chart.data.cols.length === 6 && $scope.chart.options.tooltip.isHtml === false)) {
                    $scope.chart.data.cols.pop();
                    delete $scope.chart.data.rows[0].c[5];
                    delete $scope.chart.data.rows[1].c[5];
                    delete $scope.chart.data.rows[2].c[5];
                }
                if ($scope.chart.type === 'Table') {
                    $scope.chart.options.tooltip.isHtml = false;
                    $scope.chart.data.cols.push({ id: "data-id", label: "Date", type: "date" });
                    $scope.chart.data.rows[0].c[5] = { v: "Date(2015,01,05)" };
                    $scope.chart.data.rows[1].c[5] = { v: "Date(2015,02,05)" };
                    $scope.chart.data.rows[2].c[5] = { v: "Date(2015,03,05)" };
                }

            }
            $scope.chartReady = function () {
                fixGoogleChartsBarsBootstrap();
            }
            function fixGoogleChartsBarsBootstrap() {
                $(".google-visualization-table-table img[width]").each(function (index, img) {
                    $(img).css("width", $(img).attr("width")).css("height", $(img).attr("height"));
                });
            };
            $scope.refresh();
        }
    ]);