﻿"use strict";

// Service for voting REST calls
app.factory("votingDataService", ["$q", "$http", "ngAuthSettings", function ($q, $http, ngAuthSettings) {
    var votingDataFactory = {};
    votingDataFactory.voteTypeId = 0;
    votingDataFactory.voteTypeConfig = {};

    var _init = function (voteTypeId, voteTypeConfig) {
        votingDataFactory.voteTypeId = voteTypeId;
        votingDataFactory.voteTypeConfig = voteTypeConfig;
    }

    var _getVotes = function (catId, sortType) {
        var params = catId;
        if (sortType) {
            params += "?sortType=" + sortType;
        }
        return $http.get(ngAuthSettings.apiServiceBaseUri + "/categoryVote/" + params).then(function (results) {
            return results;
        },
        function (error) {
            console.error('votingDataService: error getting votes');
        });
    };
    var _getVotesForContact = function () {
        return $http.get(ngAuthSettings.apiServiceBaseUri + "/vote").then(function (results) {
            return results;
        },
        function (error) {
            console.error('votingDataService: error getting votes for contact');
        });
    };
    var _getVote = function () {
        console.i('get vote');
    };
    var _setVotes = function (votes, fingerprint, ballotId) {

        for (var i = 0; i < votes.length; i++) {
            votes[i].Fingerprint = fingerprint;
            votes[i].BallotId = ballotId;
        }

        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/", votes).then(function (results) {
            return results;
        });
    };
    var _setVote = function (subId, sortOrder, fingerprint, ballotId) {
        var postData = [{ SubmissionId: subId, SortOrder: sortOrder, Fingerprint: fingerprint, BallotId: ballotId }];

        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/", postData).then(function (results) {
            return results;
        },
        function (error) {
            console.error('votingDataService: error setting vote');
        });
    };
    var _setCriteriaVote = function (subId, criteriaVotes, singleBest, fingerprint, ballotId) {
        var postData = [];
        if (criteriaVotes) {
            for (var key in criteriaVotes) {
                var postDataEntry = {
                    Fingerprint: fingerprint,
                    SubmissionId: subId,
                    CriteriaName: key,
                    CriteriaValue: criteriaVotes[key],
                    BallotId: ballotId
                };
                if (singleBest)
                    postDataEntry.SingleBest = singleBest;
                postData.push(postDataEntry);
            }
        }        
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/", postData).then(function (results) {
            return results;
        },
        function (error) {
            console.error('votingDataService: error setting vote');
        });
    };
    function _setWriteInVote(writeIn, sortOrder, fingerprint, ballotId) {
        var data = [{
           SortOrder: sortOrder,
           WriteInCategoryId: writeIn.categoryId,
           WriteInField1: writeIn.field1,
           WriteInField2: writeIn.field2,
            WriteInField3: writeIn.field3,
            Fingerprint: fingerprint,
            BallotId: ballotId

        }];
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/", data);
    }
    var _swapVotes = function (votes) {
        return $http.put(ngAuthSettings.apiServiceBaseUri + "/vote/swap/", votes).then(function (results) {
            return results;
        });
    };
    function _swapVotesById(data) {
        return $http.put(ngAuthSettings.apiServiceBaseUri + "/vote/swapById/", data);
    }
    var _deleteVotes = function () {
        console.i('delete votes');
    };

    var _deleteVotesByCategoryId = function (categoryId) {
        console.i('delete votes by category id ' + categoryId);

        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/deleteVotesByCategoryId/" + categoryId);
    };

    var _deleteVote = function (voteId) {
        return $http["post"](ngAuthSettings.apiServiceBaseUri + "/vote/deleteVote/1", { VoteId: voteId });
    };
    var _submitVote = function (voteId) {
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/submitCriteriaVote/" + voteId).then(function (results) {
            return results;
        });
    };
    var _submitVotes = function (catId) {
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/submit/" + catId).then(function (results) {
            return results;
        });
    };
    var _submitVotesByRoundIds = function (rounds) {
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/submitByRoundIds/1", rounds).then(function (results) {
            return results;
        });
    };
    var _submitVotesByRoundId = function (roundId) {
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/submitByRoundId/" + roundId).then(function (results) {
            return results;
        });
    };

    var _submitVotesByBallotId = function (ballotId) {
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/submitByBallotId/" + ballotId).then(function (results) {
            return results;
        });
    };

    var _unsubmitVote = function (voteId) {
        return $http.post(ngAuthSettings.apiServiceBaseUri + "/vote/unSubmitCriteriaVote/" + voteId).then(function (results) {
            return results;
        });
    };

    //var _getSubmittedBallots = function (roundId) {
    //    return $http.get(ngAuthSettings.apiServiceBaseUri + "/vote/getSubmittedBallots/" + roundId).then(function (results) {
    //        return results;
    //    });


    //    return $http.get(ngAuthSettings.apiServiceBaseUri + "/vote").then(function (results) {
    //        return results;
    //    },
    //    function (error) {
    //        console.error('votingDataService: error getting votes for contact');
    //    });
    //};

    votingDataFactory.init = _init;
    votingDataFactory.getVotes = _getVotes;
    votingDataFactory.getVotesForContact = _getVotesForContact;
    votingDataFactory.getVote = _getVote;
    votingDataFactory.setVotes = _setVotes;
    votingDataFactory.setVote = _setVote;
    votingDataFactory.setCriteriaVote = _setCriteriaVote;
    votingDataFactory.setWriteInVote = _setWriteInVote;
    votingDataFactory.swapVotes = _swapVotes;
    votingDataFactory.swapVotesById = _swapVotesById;
    votingDataFactory.deleteVotes = _deleteVotes;
    votingDataFactory.deleteVotesByCategoryId = _deleteVotesByCategoryId;
    votingDataFactory.deleteVote = _deleteVote;
    votingDataFactory.submitVotes = _submitVotes;
    votingDataFactory.submitVotesByRoundIds = _submitVotesByRoundIds;
    votingDataFactory.submitVotesByRoundId = _submitVotesByRoundId;
    votingDataFactory.submitVotesByBallotId = _submitVotesByBallotId;
    votingDataFactory.submitVote = _submitVote;
    votingDataFactory.unsubmitVote = _unsubmitVote;
    //votingDataFactory.getSubmittedBallots = _getSubmittedBallots;

    return votingDataFactory;
}]);