Ext.define("Ext.ux.markdown.Field", {

	statics: {
		markdownConfig: [
			/**
			 * @cfg output
			 */
			"output",
			/**
			 * @cfg iconCss
			 */
			"iconCss",
			/**
			 * @cfg previewCss
			 */
			"previewCss",
			/**
			 * @cfg actions
			 */
			"actions",
			/**
			 * @cfg actiontip
			 */
			"actiontip",
			/**
			 * @cfg actionText
			 */
			"actionText",
			/**
			 * @cfg action
			 */
			"action",
			/**
			 * @cfg textareaConfig
			 */
			"textareaConfig",
			/**
			 * @cfg textareaConfig
			 */
			"value"
		],
		textareaConfig: [
			"grow",
			"growAppend",
			"growMax",
			"growMin",
			"allowBlank",
			"vtype",
			"vtypeText",
			"emptyText",
			"preventScrollbars",
			"disableKeyFilter",
			"maskOnDisable"
		],
		eventConfig: [
			"autosize",
			"blur", "focus",
			"change",
			"dirtychange", "errorchange", "validitychange",
			"keydown", "keypress", "keyup",
			"disable", "enable"
		],
		transferTextAreaConfig: function (o_markdown, o_config) {

			var Field = Ext.ux.markdown.Field;
			var a_transfer = Field.textareaConfig;
			var o_transfer = {};

			Ext.Array.each(a_transfer, function (s_transfer) {

				var value = o_config[s_transfer];

				if (value != null) {
					o_transfer[s_transfer] = value;
				}
			});

			o_markdown = Ext.Object.merge(o_markdown, {
				textareaConfig: o_transfer
			});

			return o_markdown;
		},
		transferMarkdownConfig: function (o_markdown, o_config) {

			var Field = Ext.ux.markdown.Field;
			var a_transfer = Field.markdownConfig;
			var o_transfer = {};

			Ext.Array.each(a_transfer, function (s_transfer) {

				var value = o_config[s_transfer];

				if (value) {
					o_transfer[s_transfer] = value;
				}
			});

			o_markdown = Ext.Object.merge(o_markdown, o_transfer);

			return o_config;
		},
		createMarkdown: function (o_config) {

			var Field = Ext.ux.markdown.Field;

			var o_markdown = {
				xtype: "markdown"
			};

			Field.transferMarkdownConfig(o_markdown, o_config);
			Field.transferTextAreaConfig(o_markdown, o_config);

			return o_markdown;
		},
		relayEvents: function (i_field) {

			var Field = Ext.ux.markdown.Field;
			var a_events = Field.eventConfig;
			var i_markdown = i_field.getMarkdownContainer();
			var i_textarea = i_markdown.getTextarea();

			i_field.relayEvents(i_textarea, a_events);
		}
	},

	alias: "widget.markdownfield",

	extend: "Ext.form.FieldContainer",

	mixins: {
		field: "Ext.form.field.Field"
	},

	config: {
		markdownContainer: null,
		rewriter: null
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		i_self.setRewriter(Ext.ux.markdown.Field);

		var i_rewriter = i_self.getRewriter();

		var o_config = Ext.apply({}, i_self.initialConfig);
		var o_markdown = i_rewriter.createMarkdown(o_config);

		var i_markdown = Ext.widget("markdown", o_markdown);

		i_self.setMarkdownContainer(i_markdown);

		i_self.add(i_markdown);

		i_self.initField();

		i_self.getRewriter().relayEvents(i_self);

		i_self.on({
			resize: {
				fn: function(i_field) {
					var i_markdown = i_field.getMarkdownContainer();

					var f_layout = i_markdown.doLayout || i_markdown.updateLayout;

					f_layout.call(i_markdown);
				}
			},
			change: {
				fn: function(i_field, value) {
					i_field.checkChange();
				}
			}
		});

		return i_self;
	},

	initValue: function() {

		var i_self = this;
		var value = i_self.value;

		i_self.suspendCheckChange++;
		i_self.setValue(value);
		i_self.originalValue = i_self.lastValue = value;
		i_self.resetOriginalValue();
		i_self.suspendCheckChange--;

	},

	getSubmitValue: function (s_type) {

		var i_self = this;
		var i_markdown = i_self.getMarkdownContainer();
		var s_value = i_self.value;

		if (i_markdown) {
			s_value = i_markdown.getValue(s_type);
		}

		return s_value;
	},

	getConverter: function () {

		var i_self = this;
		var i_markdown = i_self.getMarkdownContainer();
		var i_converter;

		if (i_markdown) {
			i_converter = i_markdown.getConverter();
		}

		return i_converter;
	},

	getEditor: function () {

		var i_self = this;
		var i_markdown = i_self.getMarkdownContainer();
		var i_editor;

		if (i_markdown) {
			i_editor = i_markdown.getEditor();
		}

		return i_editor;
	},

	disable: function() {

		var i_self = this;
		i_self.callParent(arguments);

		if (i_self.isDisabled()) {

			var i_container = i_self.getMarkdownContainer();

			if (i_container) {

				var i_textarea = i_container.getTextarea();
				i_textarea.disable();
			}
		}
	},

	enable: function() {

		var i_self = this;

		i_self.callParent(arguments);

		if (!i_self.isDisabled()) {

			var i_container = i_self.getMarkdownContainer();

			if (i_container) {

				var i_textarea = i_container.getTextarea();
				i_textarea.enable();
			}
		}
	},

	toSource: function () {

		return this.getMarkdownContainer().toSource();
	}
});
