Ext.define("Ext.ux.form.field.i18nHook.Text", {

	extend: "Ext.ux.form.field.i18n.Text",

	alias: [
		"widget.i18nhooktextfield"
	],

	requires: [
		"Ext.ux.form.field.i18nHook.Combo",
		"Ext.ux.form.field.i18nHook.Varchar"
	],

	config: {
		action: "upsert",
		propertyHost: null,
		carrierValue: null,
		defaultProperty: "",
		objectComponent: null,
		varcharComponent: null
	},

	initComponent: function () {

		var i_self = this;

		i_self.setPropertyHost(i_self.name + "_CARRIER");

		i_self.callParent(arguments);

		return i_self;
	},

	initDefaultConfig: function () {

		var i_self = this;
		i_self.languages = wsh.getLanguages();

		var o_defaults = ip2.pick(i_self, [
			"value",
			"defaultLanguage",
			"languages",
			"iconCssFile",
			"iconClassName",
			"iconFieldStyle",
			"iconFieldCls",
			"vtype",
			"vtypeText",
			"maxLength",
			"enforceMaxLength",
			"preventMark",
			"carrierValue"
		]);

		i_self._defaults = Ext.apply(o_defaults, {
			anchor: "100%",
			hidden: true,
			isFormField: false,
			preventI18nTrigger: i_self.preventI18nTrigger
		});
	},

	setValue: function(value) {

		var i_self = this;
		return i_self;
	},

	setCarrierValue: function(s_value) {

		var i_self = this;
		s_value = s_value || "";

		if (Ext.isString(s_value)) {

			var o_value = Ext.decode(s_value, true) || {
				default: "",
				active: 0,
				text: {}
			};

			var i_objectfield = i_self.getOrCreateObjectComponent();
			var i_textfield = i_self.getOrCreateVarcharComponent();

			i_textfield.setValue(o_value.default);
			i_objectfield.setValue(o_value.text);

			var n_type = o_value.active ? i_self.outputTypes.JSON : i_self.outputTypes.VARCHAR;
			var value = o_value;

			i_self.setActiveComponent(n_type, value);

			i_objectfield.resetOriginalValue();
			i_textfield.resetOriginalValue();
			i_self.resetOriginalValue();
		}
	},

	setActiveComponent: function (s_type, value) {

		var i_self = this;
		var o_type = i_self.outputTypes;

		value = value || i_self.getValue();
		s_type = s_type || i_self.outputType;

		var i_active = i_self.getActiveComponent();
		var n_currentState = i_self.getCurrentState();
		var b_needReset = false;

		switch (s_type) {
			case o_type.VARCHAR:

				// If we were other than VARCHAR (object), then set store value
				var o_value;

				if (n_currentState == o_type.JSON) {
					if (value && Ext.isObject(value)) {
						o_value = value;
						value = o_value.default;
						i_self.setStoredObject(o_value);
					}
				}
				else {
					value = value.default;
				}

				i_self._activeComponent = i_active = i_self.getOrCreateVarcharComponent();
				i_self.getObjectComponent() && i_self.getObjectComponent().hide();
				i_self._activeComponent.show();

				i_active.setValue(value);

				if (b_needReset) {
					i_self.resetOriginalValue();
					i_active.resetOriginalValue();
				}

				break;

			default:
				i_self._activeComponent = i_active = i_self.getOrCreateObjectComponent({
					itemId: "jsonfield"
				});
				i_self.getVarcharComponent() && i_self.getVarcharComponent().hide();
				i_self._activeComponent.show();

				if (Ext.isObject(value)) {
					i_active.setValue(value.text);
				}
				else {
					i_active.setValue(value);
				}

				s_type = o_type.JSON;

				break;
		}

		i_self.setCurrentState(s_type);

		i_self.checkChange();
		i_self.validate();

		return i_active;
	},

	getOrCreateVarcharComponent: function (o_config) {

		o_config = o_config || {};

		var i_self = this;
		var i_component = i_self.getVarcharComponent();

		if (!i_component) {

			var o_defaults = Ext.apply({}, i_self._defaults);

			var o_component = Ext.Object.merge(o_defaults, {
				xtype: "i18nhookvarchar",
				defaultLanguage: i_self.defaultLanguage,
				itemId: i_self.outputTypes.VARCHAR,
				plugins: i_self._plugins || [],
				readOnly: i_self.readOnly || false,
				listeners: {
					i18ntrigger: {
						fn: function () {
							i_self.onTriggerClick();
						}
					},
					change: {
						fn: function () {
							i_self.checkChange();
						}
					},
					validitychange: {
						fn: function(item, b_valid) {
							i_self.validate();
						}
					},
					focus: {
						fn: function() {
							i_self.fireEvent("focus", i_self);
						}
					},
					blur: {
						fn: function() {
							i_self.fireEvent("blur", i_self);
						}
					}
				}
			}, o_config);

			o_component.margin = 0;

			var i_component = Ext.create(o_component);

			i_self.setVarcharComponent(i_component);

			i_self.add(i_component);

			i_component.getErrors = (function(f_old) {
				return function(value) {
					value = i_component.getValue();
					var a_errors = f_old.apply(i_component, [value]) || [];

					return a_errors;
				};
			})(i_component.getErrors);
		}

		return i_component;
	},

	getOrCreateObjectComponent: function (o_config) {

		o_config = o_config || {};

		var i_self = this;
		var i_component = i_self.getObjectComponent();

		if (!i_component) {

			var o_defaults = Ext.apply({}, i_self._defaults);

			var o_component = Ext.Object.merge(o_defaults, {
				xtype: "i18nhookcombo",
				itemId: i_self.outputTypes.JSON,
				defaultLanguage: i_self.defaultLanguage,
				plugins: i_self._plugins || [],
				readOnly: i_self.readOnly || false,
				iconFieldStyle: i_self.iconFieldStyle,
				iconFieldCls: i_self.iconFieldCls,
				iconClassName: i_self.iconClassName,
				listeners: {
					i18ntriggercombo: {
						fn: function () {
							i_component.collapse();
							i_self.onTriggerClick();
						}
					},
					change: {
						fn: function () {
							i_self.checkChange();
						}
					},
					validitychange: {
						fn: function(item, b_valid) {
							i_self.validate();
						}
					},
					focus: {
						fn: function() {
							i_self.fireEvent("focus", i_self);
						}
					},
					blur: {
						fn: function() {
							i_self.fireEvent("blur", i_self);
						}
					}
				}
			}, o_config);

			var i_component = Ext.create(o_component);

			i_self.setObjectComponent(i_component);

			i_self.add(i_component);

			i_component.getErrors = (function(f_old) {
				return function(value) {
					value = i_component.getValue();
					var a_errors = f_old.apply(i_component, [value]) || [];

					return a_errors;
				};
			})(i_component.getErrors);
		}

		return i_component;
	},

	getValue: function () {

		var i_self = this;
		var n_state = 0;
		var o_initial = i_self.initialConfig;

		var s_current = i_self.getCurrentState();

		switch (s_current) {
			case i_self.outputTypes.VARCHAR:
				n_state = 0;
				break;
			case i_self.outputTypes.JSON:
				n_state = 1;
				break;
		}

		var o_meta = {
			key: {},
			defaultProperty: i_self.getDefaultProperty(),
			active: n_state
		};

		if (i_self.getAction() === "copy") {
			o_initial.value = 0;
			i_self.setAction("upsert");
		}

		o_meta.key[i_self.name] = o_initial.value || 0;

		var i_objectfield = i_self.getOrCreateObjectComponent();
		var i_textfield = i_self.getOrCreateVarcharComponent();

		var o_text = i_objectfield.getValue();
		var s_value = i_textfield.getValue();

		if (o_text.en) {
			s_value = o_text.en;
		}
		else {
			o_text.en = s_value;
		}

		var o_value = {
			meta: o_meta,
			text: o_text,
			default: s_value
		};

		i_self._value = o_value;

		return o_value;
	},

	getOutputType: function (value) {

		var i_self = this;
		var n_type;

		if (Ext.isString(value) || Ext.isNumber(value)) {
			n_type = i_self.outputTypes.VARCHAR;
		}
		else {
			n_type = i_self.outputTypes.JSON;;
		}

		return n_type;
	}
});
