'use strict';

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

var PollDetailsController = angular.module('polls.controllers.details', [
    'system'
])
    .controller('PollDetailsController', [
        '$scope',
        '$location',
        '$route',
        '$window',
        'System',
        'PollService',
        function ($scope, $location, $route, $window, System, PollService) {
            var id = $route.current.params["PollID"];
            $scope.query = $route.current.params || {};
            $scope.currentView = 'view';
            $scope.DisplayAnswer = false;
            $(".tooltip").tooltip();

            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                PollService.get(id).then(function (poll) {
                    $scope.poll = poll;
                    $scope.existquestion = {
                        ID: poll.ID,
                        Title: poll.Title,
                        Summary: poll.Summary
                    };
                });
                
                PollService.questionByPoll(id).then(function (data) {
                    $scope.questions = data.questions;
                });
            };

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

            $scope.refreshAnswers = function (agent) {
                $scope.DisplayAnswer = true;
                PollService.answerByQuestionId(agent).then(function (data) {
                    $scope.answers = data.answers;
                });
            };
            /**
            * @action back returns to location /polls
            */
            $scope.back = function () {
                $location.url('/polls');
            };

            /**
           * @action //vote
           */
            $scope.idiVote = function (question) {
                $location.url('/vote/' + question.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;
            };

            /**
            * @action newPost returns to location /question/id/create
            */
            $scope.newQuestion = function() {
                $location.url('/question/' + $scope.poll.ID + '/create');
            };

            /**
            * @action editPost returns to location /question/id/edit
            */
            $scope.editQuestion = function(question) {
                $location.url('/question/' + question.ID + '/edit');
            };
            
            /*GLASAJ*/
            $scope.startVote = function (answer) {
                PollService.vote(answer, $scope.ip, $scope.user).then(function (data) {
                    $scope.refreshAnswers();
                });
            };
            /**
          * @action select selects single poll and redirects user to poll details
          * @param poll poll instance to view details for
          */
            $scope.selectQuestion = function (agent) {
                $scope.DisplayAnswer = true;
                PollService.answerByQuestionId(agent).then(function (data) {
                    $scope.answers = data.answers;
                });
            };
            /**
            * @method removePost
            * @param id
            * @description Removes poll question by its id
            */
            $scope.removeQuestion = function (question, index) {
                if ($window.confirm($scope.$resource('utility.areYouShure'))) {
                    PollService.removeQuestion({ id: question.ID }).then(function (response) {
                        if (response.Error) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            $scope.questions.splice(index, 1);
                            System.Message.success($scope.$resource('utility.deleted'));
                        }
                    });
                }
            };
            /**
           * @method removePost
           * @param id
           * @description Removes poll question by its id
           */
            $scope.removeAnswer = function (answer, index) {
                if ($window.confirm($scope.$resource('utility.areYouShure'))) {
                    PollService.removeAnswer({ id: answer.ID }).then(function (response) {
                        if (response.Error) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            var id = $scope.answer.QuestionID
                            $scope.refresh(id);
                            $scope.refreshAnswers(id)
                            $scope.questions.splice(index, 1);
                            System.Message.success($scope.$resource('utility.deleted'));    
                        }
                    });
                }
            };
            $scope.$watch("pager.page", function () {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

            //novi odgovor

            $scope.startNewDialog = function (question) {
                $scope.question = question;
                $scope.dialogNew.dialog("open");
            };
            /**
           *zatvori novo pitanje
           */
            $scope.cancelNew = function () {
                $scope.dialogNew.dialog("close");
            };

            $scope.ui.dialogNew = {
                width: 700,
                title: $scope.$resource('polls.odgovor'),
                create: function () {
                    $scope.dialogNew = $(this);
                }
            };

            /*izmjena odgovora*/
            
            $scope.startEditDialog = function (answer) {
                $scope.answer = answer;
                $scope.dialogEdit.dialog("open");
            };
            /**
           *zatvori novo pitanje
           */
            $scope.cancelEdit = function () {
                $scope.dialogEdit.dialog("close");
            };

            $scope.ui.dialogEdit = {
                width: 700,
                title: $scope.$resource('polls.edit'),
                create: function () {
                    $scope.dialogEdit= $(this);
                }
            };
            /**
            * @method dodaj odgovora
             */
            $scope.startNewAnswer= function () {
                $scope.answer.QuestionID = $scope.question.ID;
                PollService.createAnswer($scope.answer).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('polls.AnswerCreatedError'));
                    } else {
                        var id = $scope.question.ID
                        $scope.refresh(id);
                        $scope.refreshAnswers(id)
                        $scope.dialogNew.dialog("close");
                        System.Message.success($scope.$resource('polls.AnswerCreated'));
                    }
                });        
            };

            $scope.EditPublish = function () {
                PollService.updateAnswer($scope.answer).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('polls.QuestionCreatedError'));
                    } else {
                        var id = $scope.answer.QusionID
                        $scope.refresh(id);
                        System.Message.success($scope.$resource('polls.QuestionUpdated'));
                    }
                });
            };

            /**
          * @action back returns to location /polls
          */
            $scope.grafika = function (questions) {
                $location.url('/fat/' + questions.ID);
            };

            $scope.refresh();
        }
    ]);