﻿(function () {
    'use strict';
    writeInForm.$inject = ['votingService', '$rootScope'];
    angular.module('judgeapp').directive('writeInForm', writeInForm);

    function writeInForm(votingService, $rootScope) {
        return {
            restrict: 'E',
            templateUrl: $rootScope.localizeUrl('/spa-app/views/fragments/html/writeInForm'),
            link: _link,
            scope: {
                categoryId: '=',
                ballotId: '='
            }
        };

        function _link(scope, element, attributes) {
            scope.votingService = votingService;
            scope.voteTypeConfiguration = {};
            scope.saveVote = _saveVote;
            scope.config = votingService.getCurrentVoteTypeConfiguration;

            scope.writeIn = {
                field1: '',
                field2: '',
                field3: '',
            };

            function _saveVote() {
                scope.writeIn.categoryId = scope.categoryId;
                votingService.setWriteInVote(scope.writeIn, scope.ballotId)
                    .then(function () {
                        scope.writeIn = {
                            field1: '',
                            field2: '',
                            field3: '',
                        };
                    });
            }
        }
    }
})();