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

	extend: "Ext.form.FieldContainer",

	alias: ["widget.mediagalleryfield"],

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

	requires: [
		"Ext.ux.gallery.Media",
		"Ext.ux.gallery.view.MediaGrid",
		"Ext.ux.gallery.view.DropBox",
		"Ext.ux.gallery.view.MediaEditor",
		"Ext.ux.themed.ActionColumn"
	],

	config: {
		path: null,
		pageSize: null,
		maxItems: -1,
		linkConfig: null,
		layoutConfig: null,
		editorConfig: null,
		extensions: null,
		dragText: null,
		dropText: null,
		ignoreStaticDir: false,
		progressUploadText: "",
		progressDoneText: "",
		progressCheckText: "",
		progressCheckDoneText: "",
		progressFailText: "",
		confirmRemoveRowText: "",
		confirmRemoveRowsText: "",
		itemCreateText: "",
		itemEditText: "",
		itemRemoveText: "",
		itemCreateTitle: "",
		itemEditTitle: "",
		actionColumns: null,
		galleryAlias: "mediagallery",
		language: "",
		mediaType: "",
		gallery: null
	},

	initComponent: function() {

		var i_cmp = this;
		var o_config = i_cmp.initialConfig;

		i_cmp.callParent(arguments);

		var i_gallery = Ext.widget(o_config.galleryAlias || i_cmp.galleryAlias, {
			path: o_config.path || i_cmp.path,
			maxItems: o_config.maxItems || i_cmp.maxItems,
			extensions: o_config.extensions || i_cmp.extensions,
			editorConfig: o_config.editorConfig || i_cmp.editorConfig,
			layoutConfig: o_config.layoutConfig || i_cmp.layoutConfig,
			linkConfig: o_config.linkConfig || i_cmp.linkConfig,
			pageSize: o_config.pageSize || i_cmp.pageSize,
			value: o_config.value || i_cmp.value,
			actionColumns: o_config.actionColumns || {},
			dragText: o_config.dragText || i_cmp.dragText,
			dropText: o_config.dropText || i_cmp.dropText,
			progressUploadText: o_config.progressUploadText,
			progressDoneText: o_config.progressDoneText,
			progressCheckText: o_config.progressCheckText,
			progressCheckDoneText: o_config.progressCheckDoneText,
			progressFailText: o_config.progressFailText,
			confirmRemoveRowText: o_config.confirmRemoveRowText,
			confirmRemoveRowsText: o_config.confirmRemoveRowsText,
			itemCreateTitle: o_config.itemCreateTitle,
			itemEditTitle: o_config.itemEditTitle,
			itemCreateText: o_config.itemCreateText,
			itemEditText: o_config.itemEditText,
			itemRemoveText: o_config.itemRemoveText,
			ignoreStaticDir: o_config.ignoreStaticDir || i_cmp.ignoreStaticDir,
			parentEntity: o_config.parentEntity || i_cmp.parentEntity,
			parentEnumType: o_config.parentEnumType || i_cmp.parentEnumType,
			childEntity: o_config.childEntity || i_cmp.childEntity,
			childEnumType: o_config.childEnumType || i_cmp.childEnumType,
			mediaType: i_cmp.getMediaType()
		});

		i_cmp.add(i_gallery);

		i_cmp.setGallery(i_gallery);

		i_cmp.initField();

		i_cmp.initEvents();

		return i_cmp;
	},

	initEvents: function () {

		var i_cmp = this;
		var i_gallery = i_cmp.getGallery();
		var i_store = i_gallery.getStore();

		i_store.on({
			datachanged: {
				fn: function () {
					i_cmp.checkChange();
				}
			},
			update: {
				fn: function () {
					i_cmp.checkChange();
				}
			}
		});

		return i_cmp;
	},

	initValue: function () {

		var i_cmp = this;
		var i_gallery = i_cmp.getGallery();
		var value = i_gallery.getValue();
		var b_empty = i_cmp.isEmptyValue(value);

		if (b_empty) {
			value = "";
		}

		i_cmp.value = value;

		i_cmp.mixins.field.initValue.call(i_cmp);
	},

	getValue: function () {

		var i_cmp = this;
		var i_gallery = i_cmp.getGallery();
		var value = i_gallery.getValue();
		var b_empty = i_cmp.isEmptyValue(value);

		if (i_cmp.isEmptyValue(value)) {
			value = "";
		}

		return value;
	},

	getSubmitValue: function () {

		var i_cmp = this;

		var value = i_cmp.getValue();

		if (Ext.isString(value)) {
			value = Ext.decode(value, true) || [];
		}

		return value;
	},

	setValue: function (value) {

		var i_cmp = this;
		var i_gallery = i_cmp.getGallery();

		i_gallery.setValue(value);

		i_cmp.value = value;

		i_cmp.checkChange();

		return i_cmp;
	},

	isEmptyValue: function (value) {

		var i_cmp = this;

		if (!value) {
			return true;
		}

		if (Ext.isObject(value) && !Ext.Object.getSize(value)) {
			return true;
		}

		if (Ext.isArray(value) && !value.length) {
			return true;
		}

		return false;
	},

	resetOriginalValue: function (value) {

		var i_cmp = this;
		var i_gallery = i_cmp.getGallery();
		var b_emptyValue;

		value = value || i_cmp.getValue();

		b_emptyValue = i_cmp.isEmptyValue(value);

		if (b_emptyValue) {
			value = '';
		}

		i_cmp.originalValue = value;

		return i_cmp;
	},

	checkChange: function () {

		if (!this.suspendCheckChange) {

			var c = this;
			var b = c.getValue();
			var a = c.lastValue;

			var a_stringed = Ext.encode(a);
			var b_stringed = Ext.encode(b);

			if (!c.isEqual(b_stringed, a_stringed) && !c.isDestroyed) {
				c.lastValue = b;
				c.fireEvent("change", c, b, a);
				c.onChange(b, a);
			}
		}
	},

	disable: function() {

		var i_self = this;
		i_self.callParent(arguments);
		var i_gallery = i_self.getGallery();
		if (i_self.isDisabled()) {
			i_gallery.disable();
		}
	},

	enable: function() {

		var i_self = this;
		i_self.callParent(arguments);
		var i_gallery = i_self.getGallery();
		if (!i_self.isDisabled()) {
			i_gallery.enable();
		}
	},

	isDirty: function() {

		var i_self = this;
		var i_gallery = i_self.getGallery();
		var originalValue = "";
		var value = i_gallery.getValue();

		if (i_gallery.maxItems == 1) {
			originalValue = i_self.originalValue || {};
		}
		else {
			originalValue = i_self.originalValue || [];
		}

		var s_originalValue = "";
		var s_value = "";

		try {
			s_originalValue = Ext.encode(originalValue);
			s_value = Ext.encode(value);
		}
		catch (err) {}
		var b_dirty = s_originalValue != s_value;
		return b_dirty;
	},

	changeLanguage: function(s_language) {
		var i_self = this;
		var i_gallery = i_self.getGallery();
		i_gallery.changeLanguage(s_language);
	}
});
