'use strict';

var VotedDetailsController = angular.module('polls.controllers.voted', [
    'system'
])
    .controller('VotedDetailsController', [
        '$scope',
        '$location',
        '$route',
        '$window',
        'System',
        'PollService',
        function ($scope, $location, $route, $window, System, PollService) {
            $scope.query = $route.current.params || {};
            $scope.my = { favorite: "" };
            $scope.activePoll = false;


            $scope.show = function (name) {
                $scope.currentView = name;
            };

            $scope.user = 'guest';
            $.getJSON("http://jsonip.com?callback=?", function (data) {
                $scope.ip = data.ip;

            });
         

            $scope.refresh = function () {
                PollService.query($scope.query).then(function (data) {
                    $scope.polls = data.polls;
                    if ($scope.polls.length > 0) {
                        if ($scope.polls[0].Status.DisplayName == "Aktivan")
                            $scope.activePoll = true;
                        PollService.questionByPoll($scope.polls[0].ID).then(function (data) {
                            $scope.questions = data.questions;
                            PollService.answerByQuestionId($scope.questions[0].ID).then(function (data) {
                                $scope.answers = data.answers;
                            });
                        });
                    }
                });
            };

        
            $scope.grafika = function (questions) {
                $location.url('/fat/' + questions.ID);
            };

            $scope.isCurrentView = function (name) {
                return $scope.currentView == name;
            };

            $scope.Vote = function (agent) {
                PollService.answerForEdit(agent).then(function (data) {
                    $scope.answer = data;
                    PollService.vote($scope.answer.ID, $scope.ip, $scope.user).then(function (data) {
                        if (data.Error) {
                            $scope.errors = data.Error;
                            System.Message.error($scope.$resource("polls.IpExist"));
                            $location.url('/vote/' + $scope.answer.QuestionID);
                        }
                        else {
                            System.Message.success($scope.$resource("polls.gotovo"));
                            $location.url('/vote/' + $scope.answer.QuestionID);
                        }
                    });
                });
            };

            $scope.refresh();
        }
    ]);