'use strict';

var PollService = angular.module('polls.services', [
    'system'
])
    .factory('PollService', [
        '$http',
        '$q',
        'System',
        'PollFactory',
        function ($http, $q, System, PollFactory) {

            var URL = {
                QUERY: '/cms/api/polllaktasi/Query',
                QUESTIONS_BY_POLL: '/cms/api/polllaktasi/GetQuestioniByPollId',
                GET_QUESTION_FOR_EDIT: '/qusion/api/polllaktasi/QuestionForEdit',
                ANSWER_BY_QUESTIONS: '/cms/api/polllaktasi/GetAnswerByQuestionId',
                GET_ANSWER_FOR_EDIT: '/qusion/api/polllaktasi/AnswerForEdit',
                VOTE: '/cms/api/polllaktasi/ExecuteVote'
            };
            
            var query = function (params) {
                var deferred = $q.defer();
                $http.get(URL.QUERY, {
                    params: params
                })
                    .success(function(data) {
                        var polls = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            polls.push(PollFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ polls: polls, total: data.Count });
                    });

                return deferred.promise;
            };
            
            var questionByPoll = function (pollid) {
                var deferred = $q.defer();
                $http.get(URL.QUESTIONS_BY_POLL, {
                    params: { pollid: pollid }
                })
                    .success(function (data) {
                        var questions = [];
                        for (var i = 0; i < data.length; i++) {
                            questions.push(PollFactory.create(data[i]));
                        }
                        deferred.resolve({ questions: questions, total: data.Count });
                    });

                return deferred.promise;
            };
            
            var questionForEdit = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET_QUESTION_FOR_EDIT, {
                    params: { id: id }
                }).success(function (data) {
                    var question = data;
                    deferred.resolve(question);
                });
                return deferred.promise;
            };

            var answerByQuestionId = function (questionid) {
                var deferred = $q.defer();
                $http.get(URL.ANSWER_BY_QUESTIONS, {
                    params: {questionid: questionid }
                })
                    .success(function (data) {
                        var answers = [];
                        for (var i = 0; i < data.length; i++) {
                            answers.push(PollFactory.create(data[i]));
                        }
                        deferred.resolve({ answers: answers, total: data.Count });
                    });

                return deferred.promise;
            };

            var answerForEdit = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET_ANSWER_FOR_EDIT, {
                    params: { id: id }
                }).success(function (data) {
                    var answer = data;
                    deferred.resolve(answer);
                });
                return deferred.promise;
            };
       
            var vote = function (answer,ip,user) {
                var deferred = $q.defer();
                $http.get(URL.VOTE, {
                    params: { answer: answer , ip:ip,user:user  }
                })
                    .success(function (data) {
                        var answer = data;
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };
          
            return {
                query: query,
                questionByPoll: questionByPoll,
                questionForEdit:questionForEdit,
                answerByQuestionId: answerByQuestionId,
                answerForEdit:answerForEdit,
                vote:vote
            };
        }
    ]);
