(function () {
    "use strict";

    angular.module('pos').factory('securityService', factory);
    factory.$inject = ['$http', '$q'];

    function factory($http, $q) {

        var _signIn = function (login, chaveAcesso, manterConectado) {
            var deferred = $q.defer();
            var args = {
                login: login,
                chaveAcesso: chaveAcesso,
                manterConectado: manterConectado
            };
            $http.post('/security/signin', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        var _AutomaticSignIn = function () {
            debugger
            var deferred = $q.defer();
            var args = {
            };
            $http.post('/security/automaticsignin')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        var _signOut = function () {
            var deferred = $q.defer();
            $http.post('/security/signout')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _getIdentityInfo = function () {
            var deferred = $q.defer();
            $http.post('/security/getidentityinfo')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _checkLicenca = function () {
            var deferred = $q.defer();
            $http.post('/security/checklicenca')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        var _saveLicenca = function (licenca) {
            var deferred = $q.defer();
            var args = { licenca: licenca };
            $http.post('/security/savelicenca', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _getAutorizacao = function (chave, pin, request, reason, motivoPreDefinido) {
            var deferred = $q.defer();
            var args = {
                chave: chave,
                pin: pin,
                request: request,
                reason: reason,
                motivoPreDefinido: motivoPreDefinido
            };
            $http.post('/security/getautorizacao', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _resetPassword = function (email) {
            var deferred = $q.defer();
            var args = {
                email: email,
            };
            $http.post('/security/resetpassword', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        }

        var _revokeResetPassword = function (hash) {
            var deferred = $q.defer();
            var args = {
                hash: hash,
            };
            $http.post('/security/revokeresetpassword', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        }

        var _changeInfoPassword = function (hash, password) {
            var deferred = $q.defer();
            var args = {
                hash: hash,
                password: password
            };
            $http.post('/security/changeinfopassword', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        }
        var _GetAllMotivos = function () {
            var deferred = $q.defer();
            $http.post('/security/GetAllMotivos')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        return {
            signIn: _signIn,
            signOut: _signOut,
            getIdentityInfo: _getIdentityInfo,
            checkLicenca: _checkLicenca,
            saveLicenca: _saveLicenca,
            getAutorizacao: _getAutorizacao,
            resetPassword: _resetPassword,
            changeInfoPassword: _changeInfoPassword,
            revokeResetPassword: _revokeResetPassword,
            GetAllMotivos: _GetAllMotivos,
            AutomaticSignIn: _AutomaticSignIn
        };
    }

})();


