Ext.define("Portfolio.ux.form.field.i18n.text.AceTextArea", {

	extend: "Ext.ux.form.field.AceTextArea",

	xtype: "portfolio-i18n-acetextarea",

	requries: [
		"Ext.ux.form.field.AceTextArea"
	],

	alias: [
		"widget.i18n-acetextarea"
	],

	triggers: {
		"type": {
			cls: "",
			onClick: function() {
				var me = this;
				var args = arguments;
				var e = me.clickRepeater ? args[1] : args[0];
				var handler = me.handler;
				var field = me.field;
				if (handler && me.isFieldEnabled()) {
					Ext.callback(me.handler, me.scope, [
						field,
						me,
						e
					], 0, field);
				}
			},
			handler: function () {

				var i_self = this;

				var o_bodySize = Ext.getBody().getSize();

				var i_trigger = i_self.getTrigger("type");
				var i_menu = i_self.getSelections();

				i_menu.show();
				i_menu.alignTo(i_trigger.getEl(), "tr-tr");

				var n_bottom = i_menu.body.getBottom();

				if (n_bottom > o_bodySize.height) {
					i_menu.alignTo(i_self.getEl(), "br-tr");
				}
			}
		}
	},

	listeners: {
		afterrender: {
			fn: function (i_field) {

				var i_self = this;
				var b_active = i_self.getLanguageActive();

				var a_currrent = i_self.getLanguages();
		
				Ext.Array.each(a_currrent, function (s_lang) {

					var i_typeTrigger = i_self.getTrigger("type");

					var i_element = i_typeTrigger.getEl();

					if (i_element.hasCls("icon trigger flag " + s_lang + "-default")) {
						i_element.removeCls("icon trigger flag " + s_lang + "-default");
					}

				});

				var i_typeTrigger = i_self.getTrigger("type");

				var i_element = i_typeTrigger.getEl();

				if (!i_self.disabled) {
					i_element.show();
				}

				i_self.updateLanguageActive(b_active);

			}
		},
		editorready: {
			fn: function(i_field) {

				var i_self = this;
				i_self.setValue(i_self.getStored());
			}
		},
		writeablechange: {
			fn: function(i_field, b_readOnly) {

				var i_self = this;

				var i_trigger = i_self.getTrigger("type");

				if (i_self.rendered) {
					i_trigger.el.show();
				}
			},
			buffer: 10
		},
		enable: {
			fn: function(i_field) {

				var i_self = this;

				var i_trigger = i_self.getTrigger("type");

				if (i_self.rendered) {
					i_trigger.el.show();
				}
			},
			buffer: 10
		},
		disable: {
			fn: function(i_field) {

				var i_self = this;

				var i_trigger = i_self.getTrigger("type");

				if (i_self.rendered) {
					i_trigger.el.hide();
				}	
			},
			buffer: 10
		}
	},

	config: {
		languageActive: false,
		language: "en",
		primary: "en",
		languages: [],
		stored: {},
		selections: null,
		asString: false,
		langContext: "portfolio"
	},

	initComponent: function() {

		var i_self = this;

		var a_currrent = wsh.getLanguages(i_self.getLangContext());
		
		i_self.setLanguages(a_currrent);

		var s_current = ip2.getUser().get("USRLNG") || "en";

		i_self.setLanguage(s_current);
		i_self.setPrimary(s_current);

		i_self.callParent(arguments);

		i_self.createMenu();
	},

	initValue: function() {

		var i_self = this;
		var value = i_self.value;
		var s_current = i_self.getLanguage();
		var b_active = i_self.getLanguageActive();
		var a_languages = i_self.getLanguages();

		var o_stored = i_self.getStored();

		var s_raw = value;

		if (ip2.isStringedObject(s_raw)) {
			s_raw = Ext.decode(value, true) || value;
		}

		if (Ext.isObject(s_raw)) {

			o_stored = Ext.clone(s_raw);

			s_raw = o_stored[s_current];

			b_active = true;
		}
		else {
			o_stored[s_current] = s_raw;
		}

		Ext.Array.each(a_languages, function (s_key) {
			var s_value = o_stored[s_key] || "";
			o_stored[s_key] = s_value;
		});

		i_self.setStored(o_stored);
		i_self.setValue(s_raw);

		if (b_active) {
			i_self.setLanguageActive(true);
		}

		i_self.originalValue = i_self.lastValue = i_self.getValue();
		i_self.resetOriginalValue();
	},

	updateLanguageActive: function(b_active) {

		var i_self = this;
		var i_typeTrigger = i_self.getTrigger("type");

		var s_current = i_self.getLanguage();
		var s_primary = i_self.getPrimary();

		if (i_typeTrigger) {
			if (i_typeTrigger.rendered) {
				var i_element = i_typeTrigger.getEl();
				if (b_active) {
					i_element.removeCls("pf-earth2");
					i_element.addCls("icon trigger flag " + s_current);
				}
				else {
					i_element.removeCls("icon trigger flag " + s_current);
					i_element.addCls("pf-earth2");
				}
			}
			else {
				if (b_active) {
					i_typeTrigger.cls = " icon trigger flag " + s_current;
				}
			}
		}

		if (!b_active) {
			i_self.setLanguage(s_primary);
		}
	},

	createMenu: function() {

		var i_self = this;

		var a_items = [];

		var i_typeTrigger = i_self.getTrigger("type");

		var a_languages = i_self.getLanguages();

		Ext.Array.each(a_languages, function (s_key) {

			var i_menuItem = Ext.widget("menuitem", {
				itemId: s_key,
				text: s_key,
				iconCls: "icon flag " + s_key,
				handler: function (i_item) {

					var i_element = i_typeTrigger.getEl();
					var b_active = i_self.getLanguageActive();
					var s_current = i_self.getLanguage();

					i_element.removeCls("icon flag " + s_current);

					i_self.setLanguage(i_item.itemId);

					if (!b_active) {
						i_self.setLanguageActive(true);
					}
					else {
						i_self.updateLanguageActive(b_active);
					}

					i_self.checkChange();
				}
			});

			a_items.push(i_menuItem);
		});

		var i_menuItem = Ext.widget("menuitem", {
			itemId: "off",
			text: "Default",
			iconCls: "pf-earth2",
			handler: function (i_item) {
				i_self.setLanguageActive(false);
				i_self.checkChange();
			}
		});

		a_items.push(i_menuItem);

		var i_menu = Ext.widget("menu", {
			items: a_items,
			listeners: {
				beforeshow: {
					fn: function (i_menu) {

						var a_items = i_menu.query("menuitem");

						var s_current = i_self.getLanguage();

						var b_active = i_self.getLanguageActive();

						Ext.Array.each(a_items, function (i_item) {

							i_item.enable();

							if (i_item.itemId == "off" || i_self.readOnly) {
								if (!b_active) {
									i_item.disable();
								}
							}
						});

						if (b_active) {

							var i_found = Ext.Array.findBy(a_items, function (i_item) {
								return s_current == i_item.itemId;
							});

							i_found.disable();
						}
					}
				}
			}
		});

		i_self.setSelections(i_menu);
	},

	getValue: function() {

		var i_self = this;
		var s_current = i_self.getLanguage();
		var i_editor = i_self.getEditor();
		var s_value = i_editor ? i_editor.getValue() : i_self.getRawValue();

		var b_active = i_self.getLanguageActive();

		if (b_active) {

			var o_stored = i_self.getStored();
			var s_raw = i_editor ? i_editor.getValue() : i_self.getRawValue();

			s_value = Ext.decode(s_raw, true) || s_raw;

			if (Ext.isObject(s_raw)) {
				s_value = JSON.stringify(s_raw, null, "\t");
			}

			o_stored[s_current] = s_value;

			s_value = Ext.clone(o_stored);
		}

		if (i_self.getAsString()) {

			if (Ext.isObject(s_value)) {
				s_value = Ext.encode(s_value);
			}
		}

		return s_value;
	},

	getSubmitValue: function () {

		var i_self = this;
		var s_current = i_self.getLanguage();
		var s_value = i_self.getRawValue();

		var b_active = i_self.getLanguageActive();

		if (b_active) {

			var o_stored = i_self.getStored();
			var s_raw = i_self.getRawValue();

			o_stored[s_current] = s_raw;

			s_value = Ext.clone(o_stored);
		}

		if (i_self.getAsString()) {

			if (Ext.isObject(s_value)) {
				s_value = Ext.encode(s_value);
			}
		}

		return s_value;
	},

	setValue: function(value) {

		var i_self = this;

		i_self.callOverridden([value]);

		var s_current = i_self.getLanguage();

		var o_stored = i_self.getStored();
		var i_editor = i_self.getEditor();

		var s_raw = value;

		if (ip2.isStringedObject(s_raw)) {
			s_raw = Ext.decode(value, true) || value;
		}

		if (Ext.isObject(s_raw)) {

			s_raw = JSON.stringify(s_raw[s_current], null, "\t");
		}
		else {
			o_stored[s_current] = s_raw;
		}

		if (i_editor) {
			i_editor.setValue(s_raw);
		}
	},

	isDirty: function() {

		var i_self = this;

		var value = i_self.getValue();

		var s_originalValue = Ext.encode(i_self.originalValue, true) || i_self.originalValue;
		var s_value = Ext.encode(value, true) || value;

		return s_originalValue != s_value;
	},

	didValueChange: function (newValue, oldValue) {

		var s_originalValue = Ext.encode(newValue, true) || newValue;
		var s_value = Ext.encode(oldValue, true) || oldValue;

		return s_originalValue != s_value;
	},

	updateLanguage: function(value) {

		var i_self = this;

		if (i_self.rendered) {
			var o_stored = i_self.getStored();

			var s_value = o_stored[value];

			i_self.setValue(s_value);
		}
	},

	clear: function() {

		var i_self = this;

		i_self.suspendCheckChange++;
		i_self.setValue("");
		i_self.suspendCheckChange--;
	}
});
