'use strict';

/** 
 * @controller PollDetailsController
 * @property scope 
 * @property route
 * @property location
 * @property window
 * @property System
 * @property PollService
 * @property Resource
 */

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();

            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                PollService.questionForEdit(id).then(function (data) {
                    $scope.question = data;
                });
                PollService.answerByQuestionId(id).then(function (data) {
                    $scope.answers = data.answers;   
                });
            };

          
            /**
            * @action back returns to location /polls
            */
            $scope.back = function () {
                $location.url('/polls/' + $scope.questions.PollID);
            };
            /**
           * @action back returns to location /polls
           */
            $scope.grafika = function (questions) {
                $location.url('/fat/' + questions.ID);
            };
            /**
            * @method isCurrentView
            * @description returns value that indicates if currentView is initialized
            */
            $scope.isCurrentView = function (name) {
                return $scope.currentView == name;
            };

            /**
            * @method show
            * @description shows current view
            */
            $scope.show = function (name) {
                $scope.currentView = name;
            };

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

            /*GLASAJ*/
            $scope.Vote = function (agent) {
                PollService.answerForEdit(agent).then(function (data) {
                    $scope.answer = data;
                    if (data.Error) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('GRESKA SA IP ADRESOM'));
                    } else {
                       PollService.vote($scope.answer.ID, $scope.ip, $scope.user).then(function (data) {
                           $scope.refresh();
                       });
                        System.Message.success($scope.$resource('GOTOVO'));
                    }
               
                });
              
            };

            $scope.$watch("pager.page", function () {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

           

            $scope.refresh();
        }
    ]);