Source: directives/briefing/resposta-briefing/resposta-briefing.js

/**
 * @ngdoc directives
 * @name resposta-briefing
 * @module s4c.directives.briefing.resposta-briefing.resposta-briefing
 * 
 * @description `RespostaBriefing` Controller da funcão de Resposta de Briefing
 *              
 *
 */
(function () {
    angular.module('s4c.components.resposta-briefing', ['angular-momentjs'])
        .directive('respostaBriefing', RespostaBriefing);

    function RespostaBriefing() {
        return {
            restrict: 'E',
            templateUrl: 'app/directives/briefing/resposta-briefing/resposta-briefing.html',
            replace: true,
            scope: {},
            controller: RespostaBriefingCtrl
        }
    }

    RespostaBriefingCtrl.$inject = [
        '$scope',
        '$rootScope',
        'Briefing',
        '$mdDialog',
        'AuthService',
        'localize'
    ];

    function RespostaBriefingCtrl($scope, $rootScope, Briefing, $mdDialog, AuthService, localize) {
        $scope.res = $rootScope.res;
        $scope.user = AuthService.user.info;
        $scope.countdownFlag = false;
        angular.extend($scope, {
            respostaBriefing: { texto: '' },
            responder: responder,
            cancelar: cancelar,
            selecionarBriefing: selecionarBriefing,
            calcularTamanho: calcularTamanho,
            exibirForm: true
        });

       /**
        * Faz o calculo do tamanho do texto da resposta do briefing 
        *
        * @method calcularTamanho
		*        
        * @param texto {String} Texto
		*               
        */  
        function calcularTamanho(texto) {
            var count = 0;
            var brCount = texto.split('<br />').length - 1;
            var charArray = texto.split('');

            count += brCount * 250;

            charArray.forEach(function (c) {
                if (c === '\r' || c === '\n') {
                    count += 250;
                }
            });

            return texto.length + count;
        }

        $scope.briefingSelecionado;
        Briefing.getBriefings().then(function (briefings) {
            $scope.briefings = briefings;
        });

        $scope.dia;
        $scope.hora;
        
       /**
        * Faz a seleção do Briefing  
        *
        * @method selecionarBriefing
		*        
        * @param id {Integer} Identificador do Briefing
		*               
        */          
        function selecionarBriefing(id) {

            angular.forEach($scope.briefings, function (briefing) {
                if (id == briefing.id) {
                    $scope.briefingSelecionado = briefing;
                    carregarUltimaResposta($scope.briefingSelecionado.id);
                    $scope.dia = briefing.periodicidade.dia ? briefing.periodicidade.dia : formattedDate(briefing);
                    $scope.hora = briefing.periodicidade.hora;
                    return;
                }
            })

        }

       /**
        * Faz a formatação da data  
        *
        * @method formattedDate
		*        
        * @param briefing {Object} Briefing
		*               
        */ 
        function formattedDate(briefing) {

            var d = new Date(briefing.data.substring(0, 10));
            return d.toLocaleDateString("pt-BR")
        }

       /**
        * Carrega a ultinma resposta do briefing  
        *
        * @method carregarUltimaResposta
		*        
        * @param id {Integer} Identificador do briefing
		*               
        */ 
        function carregarUltimaResposta(id) {
            Briefing
                .getReposta(id)
                .then(function (data) {
                    var respostaBriefing = data.ultimaRespostaUsuario;

                    if (respostaBriefing != null && typeof respostaBriefing === 'object') {
                        delete respostaBriefing.id;
                        $scope.respostaBriefing = respostaBriefing;
                        tratarContagemRegressiva();
                    } else {
                        $scope.respostaBriefing = data.respostasAgencia;
                        delete $scope.respostaBriefing.id;
                        $scope.respostaBriefing.texto = "";

                    }
                }, function () {
                    $scope.exibirForm = false;
                })
        }

       /**
        * Inicia a opção de responder ao briefing  
        *
        * @method responder
		*        
		*               
        */ 
        function responder() {
            if ($scope.respostaBriefing.texto
                && $scope.respostaBriefing.texto.length >= 0) {
                $scope.respostaBriefing.BriefingConsolidado.id = $scope.briefingSelecionado.id;
                Briefing.postResposta($scope.respostaBriefing);
            }
            $mdDialog.hide();
        }

       /**
        * Fecha o diálogo de resposta ao briefing  
        *
        * @method cancelar
		*        
		*               
        */ 
        function cancelar() {
            $mdDialog.hide();
        }

        $scope.countdown = undefined;

       /**
        * Faz o tratamento da contagem regressiva para encerrar o briefing
        *
        * @method tratarContagemRegressiva
		*        
		*               
        */ 
        function tratarContagemRegressiva() {

            var interval = setInterval(function () {
                $scope.countdown = millisecondsRemaining();
                $scope.countdownMinutes = Math.ceil($scope.countdown / 60);
                $scope.$apply();

                if ($scope.countdown < 301) {
                    $scope.countdownFlag = true;
                } else {
                    $scope.countdownFlag = false;
                }

                if ($scope.countdown < 0) {
                    clearInterval(interval);
                }
            }, 1000);

        }

       /**
        * Retorna a quantidade de segundos restantes
        *
        * @method millisecondsRemaining
		* @param offsetInMinutes {Date} Minutos 
		*               
        */ 
        function millisecondsRemaining(offsetInMinutes) {

            if (offsetInMinutes) {
                return moment($scope.briefingSelecionado.data).subtract(offsetInMinutes, 'minutes').diff(moment(), 'seconds');
            }
            else {
                return moment($scope.briefingSelecionado.data).diff(moment(), 'seconds');
            }
        }

    }
}());