Ext.define("Portfolio.util.Wsh", {

	singleton: true,

	alternateClassName: [
		"wsh",
		"wsh.i18n"
	],

	config: {
		primaryLanguage: null,
		enumValues: null,
		context: "portfolio",
		stored: {}
	},

	requires: [
		"Portfolio.util.Handler"
	],

	constructor: function(o_config) {

		var i_self = this;

		i_self.initConfig(o_config);
	},

	reload: function (s_context) {

		var i_self = this;

		s_context = s_context || i_self.getContext();

		var o_response = ip2.getEvaled({
			url: "/portfolio/cms/getLanguages?payload={\"module\":\""+ s_context +"\"}",
			class: "wshlangauges-evaled"
		});

		if (!o_response || (Ext.isObject(o_response) && !o_response.success)) {
			o_response = ip2.getEvaled("/wsh-services/wshGetLanguages.aspx");
		}

		var a_languages = [];

		if (Ext.isObject(o_response)) {
			a_languages = Ext.Array.from(o_response.languages);
		}

		if (Ext.isArray(o_response)) {
			a_languages = o_response;
		}


		a_languages = a_languages.filter(function (value) {
			return value;
		});

		var o_ns = ip2.resource.get("adm.ns");
		i_self.setEnumValues(o_ns.enums.languages.values);

		i_self.setLanguages(a_languages, s_context);
	},

	getLanguages: function(s_context) {

		var i_self = this;
		var o_stored = i_self.getStored();

		if (!s_context) {
			s_context = i_self.getContext();
		}

		if (!o_stored.hasOwnProperty(s_context)) {
			i_self.reload(s_context);
		}
		var value = o_stored[s_context] ||  [];
		return value;
	},

	setLanguages: function(value, s_context) {

		var i_self = this;
		s_context = s_context || i_self.getContext();
		var o_stored = i_self.getStored();
		o_stored[s_context] = value;
	},

	getAll: function (b_reload) {

		var i_self = this;

		var a_languages = i_self.getLanguages();

		if (!a_languages || b_reload) {
			i_self.reload();
			a_languages = i_self.getLanguages();
		}

		return a_languages;
	},

	getPrimary: function () {

		var i_user = ip2.getUser();
		return i_user.get("USRLNG");
	},

	getEnum: function (s_value, s_lang) {

		var i_self = this;

		var o_enumValues = i_self.getEnumValues();
		var s_enumValue = "";

		var o_enum = o_enumValues[s_value];

		if (!s_lang) {
			s_enumValue = ip2.i18n(o_enum);
		}
		else {
			s_enumValue = o_enum[s_lang];
		}

		return s_enumValue;
	}

}, function() {
	var i_self = this;
	i_self.reload();
});
