(function () {
    "use strict";

    angular.module('pos').controller('loginController', controller);


    function controller($scope, securityService, $state) {
        $scope.$parent.initializeLayout('Login');

        $scope.initialize = function () {
            //debugger
            //var v = "00001901";
            //var expires = new Date();
            //expires.setFullYear(expires.getFullYear() + 20);

            //document.cookie =
            //    "APP_CONFIGMarket=" + encodeURIComponent("00001901") +
            //    "; path=/" +
            //"; expires=" + new Date().toUTCString() +
            //    "; SameSite=Lax";
            //debugger
            const cfg = getCookie("APP_CONFIGMarket");
            if (cfg) {
                $scope.CarregandoAutomaticSignIn = true;
                $scope.AutomaticSignIn();
            }
        }
        $scope.CarregandoAutomaticSignIn = false;
        controller.$inject = ['$scope', 'securityService', '$state'];

        function getCookie(name) {
            const match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
            return match ? decodeURIComponent(match[2]) : null;
        }

        $scope.AutomaticSignIn = function () {
          
            $scope.licenciamento.mensagem = '';
            $scope.login.carregando = true;

            securityService
                .AutomaticSignIn()
                .then(
                    function (response) {
                        if (response.Status == -2) { //licenca expirada ou inválida
                            $scope.licenciamento.licenciar = true;
                            $scope.licenciamento.mensagem = response.Message;
                            $scope.login.carregando = false;
                        } else if (response.Status == -1) { //erro
                            $scope.toastr.error(response.Message, 'Login de Acesso');
                            $scope.login.carregando = false;
                        } else { //credenciais ok
                            $scope.environment.perfil = response.Data;
                            /* localStorage.setItem("FORCE_THEME_RELOAD", "1");*/

                            debugger
                            $state.go('state-venda-autonoma-add', {}, { reload: true, location: 'replace' });
                            $scope.getConfig();
                            $scope.login.chaveAcesso = '';
                            if ($scope.config.ApresentarNotasPendentes) {
                                $scope.showPendingInvoices = true;
                            }
                            if (response.Status == 1) {
                                $scope.toastr.info(reponse.Message);
                            }

                            licencaService.exibirLicenca();
                            $scope.checkLicenca();
                            $scope.avisoLicenca();
                        }
                    },
                    function (error) {
                        $scope.toastr.error(error, 'Comunicação com o servidor.');
                        $scope.login.carregando = false;
                    }
                );
        }

        $scope.initialize();
    }

})();