'use strict';

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

            var URL = {
                QUERY: '/qusion/api/poll/Query',
               
                GET: '/qusion/api/poll/get',
                CREATE: '/qusion/api/poll/Create',
                EDIT: '/qusion/api/poll/Edit',
                DELETE: '/qusion/api/poll/Delete',
                QUESTIONS_BY_POLL: '/qusion/api/poll/GetQuestioniByPollId',
                DELETE_QUESTION: '/qusion/api/poll/DeleteQuestion',
                CREATE_QUESTION: '/qusion/api/poll/CreateQuestion',
                GET_QUESTION_FOR_EDIT: '/qusion/api/poll/QuestionForEdit',
                UPDATE_QUESTION: '/qusion/api/poll/UpdateQuestion',
                ANSWER_BY_QUESTIONS: '/qusion/api/poll/GetAnswerByQuestionId',
                DELETE_ANSWER: '/qusion/api/poll/DeleteAnswer',
                CREATE_ANSWER: '/qusion/api/poll/CreateAnswer',
                GET_ANSWER_FOR_EDIT: '/qusion/api/poll/AnswerForEdit',
                UPDATE_ANSWER: '/qusion/api/poll/UpdateAnswer',
                STATUS: '/qusion/api/poll/Status',
                STATUSIP: '/qusion/api/poll/GetVoteByIp',
                VOTE: '/qusion/api/poll/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 get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                }).success(function (data) {
                    var poll = PollFactory.create(data);
                    deferred.resolve(poll);
                });
                
                return deferred.promise;
            };
            
            var create = function (newPoll) {
                var deferred = $q.defer();
                $http.post(URL.CREATE, newPoll)
                    .success(function (data) {
                        newPoll = PollFactory.create(data);
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            var edit = function(poll) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, poll)
                    .success(function(data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
           
            
            var remove = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE, {
                    params: params
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            var createQuestion = function (question) {
                var deferred = $q.defer();
                $http.post(URL.CREATE_QUESTION, question)
                    .success(function (data) {
                        deferred.resolve(data);
                    });
                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 updateQuestion = function (question) {
                 var deferred = $q.defer();
                 $http.post(URL.UPDATE_QUESTION, question)
                     .success(function (data) {
                         var answer = data;
                         deferred.resolve(data);
                     });
                 return deferred.promise;
             };


            var removeQuestion = function (params) {
                var deferred = $q.defer();

                $http.get(URL.DELETE_QUESTION, {
                    params: params
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            var setLockStatus = function (id) {
                var deferred = $q.defer();
                $http.get(URL.STATUS, {
                    params: { id: id }
                })
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };


            var createAnswer = function (answer) {
                var deferred = $q.defer();
                $http.post(URL.CREATE_ANSWER, answer)
                    .success(function (data) {
                        deferred.resolve(data);
                    });
                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 updateAnswer = function (answer) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE_ANSWER, answer)
                    .success(function (data) {
                        var answer = data;
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };


            var removeAnswer = function (params) {
                var deferred = $q.defer();

                $http.get(URL.DELETE_ANSWER, {
                    params: params
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });
                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 statusip = function (ip) {
                var deferred = $q.defer();
                $http.get(URL.STATUSIP, {
                    params: { ip: ip }
                })
                    .success(function (data) {
                        var votes = [];
                        for (var i = 0; i < data.length; i++) {
                            votes.push(PollFactory.create(data[i]));
                        }
                        deferred.resolve({ votes: votes, total: data.Count });
                    });

                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,
                get: get,
                create: create,
                edit: edit,
                remove: remove,
                removeQuestion: removeQuestion,
                createQuestion: createQuestion,
                questionForEdit: questionForEdit,
                updateQuestion: updateQuestion,
                questionByPoll: questionByPoll,
                setLockStatus: setLockStatus,
                answerByQuestionId: answerByQuestionId,
                removeAnswer: removeAnswer,
                createAnswer: createAnswer,
                answerForEdit: answerForEdit,
                updateAnswer: updateAnswer,
                statusip: statusip,
                vote:vote
            };
        }
    ]);
