Ext.define("IceCap.ux.form.field.Field", {

	extend: "Ext.form.FieldContainer",

	alias: [
		"widget.iField"
	],

	config: {
		theme: "default",
		field: null
	},

	border: false,

	layout: "fit",

	mixins: [
		"Ext.form.field.Field"
	],

	listeners: {
		focus: {
			fn: function(i_container) {

				var i_field = i_container.getField();
				i_field.focus(true, 10);
			}
		}
	},

	initComponent: function () {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var n_inputWidth = o_config.width || 80;

		var n_default = IceCap.util.Handler.getDefaultWidth();
		var n_width = n_default;
		if (o_config.len > 1) {
			n_width = ((o_config.len - 1) * IceCap.util.Handler.getColSize()) + n_default;
		}

		if (o_config.len > n_inputWidth) {
			var n_temp = n_inputWidth - o_config.col;
			n_width = ((n_temp - 1) * IceCap.util.Handler.getColSize()) + n_default;
		}

		Ext.apply(i_self, {
			width: n_width
		});

		i_self.callParent();

		i_self.createField();

		var i_field = i_self.getField();

		if (o_config.atr == 25) {

			if (o_field.readOnly) {
				i_self.addCls("icecap-invalid");
			}
			else {
				i_field.markInvalid(true);
			}
		}
	},

	focus: function() {

		var i_self = this;
		var i_field = i_self.getField();

		i_field.focus(true, 10);
		i_self.fireEvent("fieldfocus", i_self);
	},

	createField: function() {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var o_meta = o_config.metaData || {};

		var o_field = {
			inputType: "text",
			xtype: "textfield"
		};

		Ext.apply(o_field, i_self.applyFFW(o_config));

		var s_type = o_field.xtype || "textfield";
		var b_password = o_config.atr == "27";

		if (o_field.readOnly) {

			o_field.xtype = s_type = "displayfield";

			Ext.apply(o_field.fieldStyle, {
				"padding": "1px 3px 2px 3px",
				"margin-top": IceCap.util.Handler.getMargin() + "px !important",
				"margin-left": "1px",
				"border": "1px solid transparent"
			});
		}

		var o_listeners = Ext.apply({}, o_meta.listeners);
		delete o_meta.listeners;

		var a_field = ["icecap-field", i_self.getTheme()];

		var a_pass = [b_password ? "iceCap-input-password" : "iceCap-input-" + (o_config.atr || 20)];

		a_pass.push(i_self.getTheme());

		var a_cls = [a_field.filter(Boolean).join("-"), a_pass.filter(Boolean).join("-")];

		var s_cls = a_cls.join(" ");

		Ext.Object.merge(o_field, {
			enforceMaxLength: true,
			maxLength: o_config.len,
			flex: 1,
			minWidth: IceCap.util.Handler.getDefaultWidth(),
			lin: o_config.lin,
			col: o_config.col,
			emuId: o_config.name,
			value: o_config.dta || "",
			cls: s_cls,
			enableKeyEvents: true,
			selectOnFocus: true,
			listeners: {
				afterrender: {
					fn: function(i_field) {

						if (i_field.inputEl && !i_field.metaField) {
							i_field.inputEl.setMinHeight(0);
							i_field.inputEl.setHeight(IceCap.util.Handler.getDefaultHeight());

							i_field.inputEl.set({
								autocomplete: "off"
							});
						}
					}
				},
				specialKey: {
					fn: function(i_field, i_event) {

						if (i_event.getKey() == Ext.event.Event.ENTER) {
							i_self.fireEvent("specialKey", i_self, i_event);
						}
					}
				},
				focus: {
					fn: function(i_field) {

						i_self.fireEvent("fieldfocus", i_self);
					}
				}
			}
		}, o_meta);

		var o_clone = Ext.clone(o_field);

		if (o_meta.xtype) {

			o_clone.metaField = true;

			o_clone.xtype = s_type = o_meta.xtype;

			delete o_clone.cls;
			delete o_clone.fieldStyle;
			delete o_clone.inputType;
		}

		delete o_clone.width;

		var i_field = Ext.widget(o_clone.xtype, o_clone);

		i_self.setField(i_field);

		i_self.add(i_field);
	},

	getValue: function() {

		var i_self = this;
		var i_field = i_self.getField();
		return i_field.getValue();
	},

	setValue: function(value) {

		var i_self = this;
		var i_field = i_self.getField();
		return i_field.setValue(value);
	},

	applyFFW: function(o_config) {

		var i_self = this;
		var s_hex = o_config.ffw;
		var b_fixed = IceCap.util.Handler.getFixed();

		var o_field = {
			xtype: "textfield"
		};

		var o_fieldStyle = {
			font: "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif"
		};

		if (b_fixed) {

			Ext.apply(o_fieldStyle, {
				font: "normal " + IceCap.util.Handler.getTextSize() + "px Lucida Console, Courier New, monospace"
			});
		}

		if (o_config.ta) {

			Ext.apply(o_fieldStyle, {
				"text-align": o_config.ta
			});
		}

		if (s_hex) {

			var n_hex = parseInt(s_hex);

			var n_sanHex = IceCap.util.Handler.createIndicators(s_hex);

			if (n_sanHex & IceCap.util.Handler.FFW.BYPASS) {

				Ext.apply(o_field, {
					readOnly: true
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.ALPHA_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[a-z A-Z ,.-]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.NUMERIC_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[+\\-,. 0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.DIGITS_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MOD_DATA) {

				Ext.apply(o_field, {
					listeners: {
						afterrender: {
							fn: function(i_field) {

								i_field.originalValue = 0xFF;
							},
							single: true
						}
					}
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.AUTO_ENTER) {

				// THIS WE DONT NEED IN THE MODERN WORLD...

				// Ext.apply(o_field, {
				// 	listeners: {
				// 		blur: {
				// 			fn: function(i_field) {
				// 				i_self.doKeyEvent("ENTER");
				// 			}
				// 		}
				// 	}
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MONOCASE) {

				Ext.apply(o_fieldStyle, {
					"text-transform": "uppercase"
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MAND_ENTER) {

				Ext.apply(o_field, {
					allowBlank: false
				});
			}

			if (o_config.atr == "27" && n_hex & (1 << 13)) {

				Ext.apply(o_field, {
					hidden: true,
					disabled: true
				});
			}
		}

		Ext.apply(o_field, {
			fieldStyle: o_fieldStyle
		});

		return o_field;
	}
});
