Ext.define('Ext.ux.form.field.XidItemGrid', {

	extend: "Ext.form.FieldContainer",
	mixins: {
		field: 'Ext.form.field.Field'
	},

	alias: [
		'widget.xpditemgrid'
	],
	config: {
		dmd: "dmd/webshop.dmd",
		entity: "WSHITMET00",
		viewName: "portfolio",
		xpd: "product.xpd",
		context: "grid",
		model: null
	},

	changeListener: function() {

		var i_self = this;
		var a_value = [];

		var value = a_value = i_self.getValue();

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

		var a_columns = i_self.createColumns();
		var a_fields = [];
		var a_keys = [];

		var i_grid = Ext.widget("gridpanel", {
			minHeight: 500,
			store: {
				model: i_self.getModel(),
				proxy: {
					type: 'memory',
					reader: {
						type: 'json',
						rootProperty: 'rows'
					}
				}
			},
			columns: a_columns
		});

		var i_store = i_grid.getStore();

		i_self.add(i_grid);

		i_self.getData(a_value, function(a_records) {
			i_store.loadRecords(a_records, false);
		});
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);
		i_self.createModel();
		i_self.changeListener();

		return this;
	},
	disable: function () {
		return this;
	},
	enable: function () {
		return this;
	},
	isDirty: function() {
		return false;
	},

	createModel: function() {

		var i_self = this;

		var i_model = i_self.getModel();

		if (!i_model) {

			var o_config = Ext.apply({}, {
				dmd: i_self.getDmd(),
				entityName: i_self.getEntity(),
				uiName: i_self.getViewName(),
				context: i_self.getContext()
			});

			i_model = ip2.model(o_config);

			i_self.setModel(i_model);
		}

		return i_model;
	},

	createColumns: function() {

		var i_self = this;
		var o_dmd = ip2.resource.get(i_self.getDmd());
		var o_xpd = ip2.resource.get(i_self.getXpd());
		var o_model = o_dmd.model;
		var s_entity = i_self.getEntity();
		var s_view = i_self.getViewName();
		var a_viewColumns = [];
		var a_entityColumns = [];
		var a_result = [];
		var i_model = i_self.getModel();
		var o_proto = i_model.prototype;

		var o_entity = Ext.Array.findBy(o_model.entities, function(o_value) {
			return o_value.name == s_entity;
		});

		if (o_entity) {

			a_entityColumns = a_entityColumns.concat(o_entity.columns);

			if (o_entity.ui.grids[s_view]) {

				var o_view = o_entity.ui.grids[s_view];

				var a_temp = Ext.Object.getValues(o_view.columns);

				Ext.Array.each(a_temp, function(o_value) {
					var o_clone = Ext.clone(o_value);
					a_viewColumns.push(o_clone);
				});
			}
		}

		var b_found = false;
		var o_uiTypes = ip2.getUiTypes();

		Ext.Array.each(a_viewColumns, function(o_viewColumn) {

			var o_clone = Ext.apply({}, o_viewColumn);

			if (!o_clone.hide) {

				Ext.Array.each(a_entityColumns, function(o_entityColumn) {
					if (o_clone.name == o_entityColumn.name) {
						Ext.apply(o_clone, o_entityColumn);
						return false;
					}
				});

				var o_protoColumn;

				if (Ext.isObject(o_proto.columns)) {
					o_protoColumn = o_proto.columns[o_clone.name];
				}
				else if (Ext.isArray(o_proto.columns)) {

					o_protoColumn = Ext.Array.findBy(o_proto.columns, function(o_column) {
						return o_column.name == o_clone.name;
					});
				}

				if (o_protoColumn) {
					Ext.applyIf(o_clone, o_protoColumn);
				}

				Ext.applyIf(o_clone, {
					dataIndex: o_clone.name,
					flex: o_clone.flex || 1
				});

				o_clone.header = ip2.i18n(o_clone.title) || ip2.i18n(o_clone.header);

				if (o_clone.uitype) {

					if (o_uiTypes.hasOwnProperty(o_clone.uitype)) {

						var o_uiType = o_uiTypes[o_clone.uitype];
						var o_grid = o_uiType.grid;

						var f_renderer = o_grid.renderer || function(value, i_meta, i_record) {
							return value;
						};

						o_clone.renderer = f_renderer;
					}
				}

				a_result.push(o_clone);
			}
		});

		return a_result;
	},

	getData: function(a_data, f_callback) {

		var i_self = this;
		var i_model = i_self.getModel();
		var a_batch = [];

		Ext.Array.each(a_data, function (o_value) {

			var o_keys = {};

			o_keys[i_model.idProperty] = o_value.itemToken;

			a_batch.push({
				type: 'get',
				dmd: i_self.getDmd(),
				entity: i_self.getEntity(),
				uiName: i_self.getViewName(),
				context: i_self.getContext(),
				key: o_keys
			});
		});

		Ext.Ajax.request({
			url: '/ip2-services/ip2dmdProxy.aspx',
			jsonData: {
				transaction: {
					batch: a_batch
				}
			},
			success: function (o_transaction) {

				var o_response = Ext.decode(o_transaction.responseText);
				var a_result = [];
				var a_records = [];

				Ext.Array.each(o_response.batch, function (o_batch) {

					if (o_batch.rows.length) {

						a_result = a_result.concat(o_batch.rows);
					}
				});

				Ext.Array.each(a_result, function(o_value) {
					var i_record = i_model.create(o_value);
					a_records.push(i_record);
				});

				f_callback(a_records);
			}
		});
	}
});
