/**
 * Fachada para la invocaciónd de los servicios REST publicados
 * @returns
 */

function UserServiceFacade () {
	
	/*
	 * |||||||||||||||||||||||||||||||||||||||||||
	 * 
	 * La variable pwdrecoverWSContext se define como atributo de la clase /contenido/js/inet.js
	 * En el se estable de forma general el host donde está el servicio REST
	 * |||||||||||||||||||||||||||||||||||||||||||
	 */
	
	// -- url de invocación al cambio de changePassword
	this.urlRestChangePassword = inet.pwdrecoverWSContext+"/api/user/{loginOrEmployeeid}/changePassword";
	this.urlRestGetDeliveryType = inet.pwdrecoverWSContext+"/api/user/{loginOrEmployeeid}";
    this.urlRestSendPIN = inet.pwdrecoverWSContext+"/api/user/{loginOrEmployeeid}/sendPIN";
	this.urlRestSupportMail = inet.pwdrecoverWSContext+"/api/user/supportcontact";
	this.urlRestGetCountries = inet.pwdrecoverWSContext+"/api/user/countries";
	this.urlRestCheckCaptcha = inet.pwdrecoverWSContext+"/api/user/exceedsAttempts";
	this.urlRestUserLocation = inet.pwdrecoverWSContext+"/user-location-detection";
	this.urlRestRefreshCaptcha = inet.pwdrecoverWSContext+"/api/user/captchaImage";
}


/**
 * Invocación al método changePassword 
 */
UserServiceFacade.prototype.changePassword = function(loginOrEmployeeid, data, onSuccess, onError,context) {
	
	 var url = this.urlRestChangePassword.replace("{loginOrEmployeeid}", loginOrEmployeeid);
	
	 return $.ajax({
	    	async: true, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	cache: false,
	        timeout: 30000,
	        type: 'PUT',
	        dataType: 'json',
	        contentType : 'application/json',
	        data : JSON.stringify(data),
	        url: url,
	        success: onSuccess,
	        error: onError
	    });
	
};

/**
 * Invocación al método getDeliveryType 
 */
UserServiceFacade.prototype.getDeliveryType = function(loginOrEmployeeid, data, onSuccess, onError,context) {
	
	 var url = this.urlRestGetDeliveryType.replace("{loginOrEmployeeid}", loginOrEmployeeid); 
	 return $.ajax({
	    	async: true, // FIXME PONER A FALSE PARA DEPURAR
	    	cache: false,
	    	context: context,
	    	timeout: 30000,
	        type: 'GET',
	        dataType: 'json',
	        data: data,
	        url: url,
	        success: onSuccess,
	        error: onError
	    });
};


/**
 * Invocación al método sendPIN 
 */
UserServiceFacade.prototype.sendPIN = function(loginOrEmployeeid, data, onSuccess, onError,context) {
	
	 var url = this.urlRestSendPIN.replace("{loginOrEmployeeid}", loginOrEmployeeid);
	
	 return $.ajax({
	    	async: true, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	cache: false,
	        type: 'POST',
	        dataType: 'json',
	        url: url,
	        data : data,
	        success: onSuccess,
	        error: onError//,
	    });
};

/**
 * Invocación al método getCountries 
 */
UserServiceFacade.prototype.getCountries = function(onSuccess, onError,context) {
	
	 var url = this.urlRestGetCountries;
	
	 return $.ajax({
	    	async: false, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	timeout: 30000,
	    	cache: true,
	        type: 'GET',
	        dataType: 'json',
	        url: url,
	        success: onSuccess,
	        error: onError
	    });
};

/**
 * Invocación al método getSupportMail
 */
UserServiceFacade.prototype.getSupportMail = function(countryId, onSuccess, onError,context) {
	
	 var url = this.urlRestSupportMail;
	
	 return $.ajax({
	    	async: true, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	cache: true,
	        type: 'GET',
	        dataType: 'json',
	        data: {'country': countryId},
	        url: url,
	        success: onSuccess,
	        error: onError
	    });
};

/**
 * Invocación al método checkCaptcha
 */
UserServiceFacade.prototype.checkCaptcha = function(data, onSuccess, onError, context) {
	var url = this.urlRestCheckCaptcha;
	
	 return $.ajax({
	    	async: false, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	cache: false,
	        type: 'GET',
	        dataType: 'json',
	        url: url,
	        data: data,
	        success: onSuccess,
	        error: onError
	    });
};

UserServiceFacade.prototype.getUserLocation = function(onSuccess, onError, context) {
	
	 return $.ajax({
	    	async: true, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	timeout: 3000,
	    	cache: false,
	        type: 'GET',
	        dataType: 'json',
	        url: this.urlRestUserLocation,
	        success: onSuccess,
	        error: onError
	    });
};

UserServiceFacade.prototype.refreshCaptchaImg = function(data, onSuccess, onError, context) {
	var url = this.urlRestRefreshCaptcha;
	
	 return $.ajax({
	    	async: true, // FIXME PONER A FALSE PARA DEPURAR
	    	context: context,
	    	cache: false,
	        type: 'GET',
	        dataType: 'json',
	        url: url,
	        data: data,
	        success: onSuccess,
	        error: onError
	    });
}

//--- Singleton instance ---
var userServiceFacade = new UserServiceFacade();