Ext.define("Portfolio.login.util.Handler", {

	singleton: true,

	alternateClassName: [
		"ip2.login"
	],

	config: {
		settings: null
	},

	constructor: function(o_config) {

		var i_self = this;

		i_self.initConfig(o_config);

		return i_self;
	},

	checkLogin: function(f_callback) {

		var i_self = this;

		f_callback = f_callback || Ext.emptyFn;

		Ext.Ajax.request({
			url: "/ip2-services/ip2getSettings.aspx",
			params: {
				func: "getSystemSettings",
				monolith: 1
			},
			success: function (i_response, i_request) {

				var o_settings = i_self.getSettings();

				var o_result = Ext.decode(i_response.responseText, true) || {};

				Ext.apply(o_result.system, o_settings);

				Ext.apply(Portfolio.util.Handler.navigator, o_result);

				f_callback(o_result);
			},
			failure: function(o_error) {

				var o_result = Ext.decode(o_error.responseText, true) || {
					currentUser: {
						anonymous: true
					},
					user: {
						id: null,
						language: "en"
					}
				};

				Ext.apply(Portfolio.util.Handler.navigator, o_result);

				Portfolio.util.Handler.messageBox({
					closable: false,
					title: "Server error",
					message: o_result.msg || o_result.message || [o_error.status, o_error.statusText].join(" - "),
					yesText: "Ok"
				});

				f_callback(o_result);
			}
		});
	},

	doLogin: function(o_values, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		Ext.Ajax.request({
			url: "/ip2-services/ip2navigator.aspx",
			params: {
				func: "checkUserProfile",
				devId: o_values.devId,
				userid: Ext.util.Base64.encode(o_values.username),
				password: Ext.util.Base64.encode(o_values.password)
			},
			success: function (response, request) {

				var o_result = Ext.decode(response.responseText, true) || {};

				f_callback(o_result);
			},
			failure: function(o_error) {

				var o_result = Ext.decode(o_error.responseText, true) || {};

				Ext.MessageBox.show({
					title: "Login error",
					icon: Ext.MessageBox.ERROR,
					msg: o_result.msg || "Server error",
					buttons: Ext.MessageBox.OK
				});

				f_callback(o_result);
			}
		});
	},

	doDeviceAuth: function(o_values, f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/ip2navigator.aspx",
			params: {
				func: "authMfa",
				devId: o_values.devId,
				oneTimeKey: o_values.key
			},
			success: function (response, request) {

				var o_result = Ext.decode(response.responseText, true) || {};

				f_callback(o_result);
			},
			failure: function(o_error) {

				var o_result = Ext.decode(o_error.responseText, true) || {};

				Ext.MessageBox.show({
					title: "Authentication error",
					icon: Ext.MessageBox.ERROR,
					msg: o_result.msg || "Server error",
					buttons: Ext.MessageBox.OK
				});

				f_callback(o_result);
			}
		});
	},

	getLoginConfig: function(f_callback) {

		var i_self = this;

		f_callback = f_callback || Ext.emptyFn;

		Ext.Ajax.request({
			url: "/custom-config/config.json",
			success: function (response, request) {

				var o_result = Ext.decode(response.responseText, true) || {};

				i_self.setSettings(o_result);

				f_callback(o_result);
			},
			failure: function(o_error) {

				var o_result = Ext.decode(o_error.responseText, true) || {};

				Ext.MessageBox.show({
					title: "Login error",
					icon: Ext.MessageBox.ERROR,
					msg: o_result.msg || "Server error",
					buttons: Ext.MessageBox.OK
				});

				f_callback(o_result);
			}
		});
	},

	getTemplate: function(s_tpl, o_data) {
		return s_tpl.replace(/\$\{([^}]+)\}/g, function(_, q) {
			return eval("with (o_data) {" + q + "}");
		});
	},

	getXTemplate: function(s_tpl, o_data) {

		var i_tpl = new Ext.XTemplate(s_tpl);
		i_tpl.compile();

		return i_tpl.apply(o_data);
	},

	isLogedOn: function() {

		var i_self = this;

		var i_user = Portfolio.util.Handler.getUser();

		return i_user.get("USRUSRTKN") !== null;
	},

	onResetPassword: function(value, f_callback) {

		Ext.Ajax.request({
			type: "POST",
			url: "/ip2-services/ip2navigator.aspx",
			params: {
				func: "resetpassword",
				password: Ext.util.Base64.encode(value)
			},
			success: function(i_response) {

				var o_result = Ext.decode(i_response.responseText, true) || {};
				f_callback(o_result);
			}
		});
	}
});
