Ext.define("Ext.ux.call2action.EntityGrid", {

	extend: "Ext.grid.Panel",

	alias: ["widget.ip2Call2ActionEntityGrid"],

	border: false,

	multiSelect: false,

	config: {
		dmd: {},
		viewReady: false,
		dmdInfo: {},
		pagingBar: null
	},

	initComponent: function() {

		var i_self = this;

		var o_dmdConfig = i_self.getDmd();

		// create model and store
		var i_model = ip2.model({
			dmd: o_dmdConfig.dmd,
			entityName: o_dmdConfig.entity,
			uiName: (o_dmdConfig.ui) ? o_dmdConfig.ui : "portfolio"
		});

		var i_protoModel = i_model.prototype;

		var o_dmdInfo = ip2.dmd.getInfo({
			dmd: o_dmdConfig.dmd,
			entity: o_dmdConfig.entity,
			view: o_dmdConfig.ui || "portfolio",
			context: "grid"
		});

		var i_proxy = ip2.getProxyByEntity({
			dmd: o_dmdConfig.dmd,
			entity: o_dmdConfig.entity,
			view: o_dmdConfig.ui || "portfolio",
			context: "grid"
		});

		var o_extraParams = i_proxy.getExtraParams();

		Ext.apply(o_extraParams, {
			action: "getRows",
			context: "grid",
			uiName: o_dmdConfig.ui || "portfolio",
			dmdFile: ip2.resource.getFullPath(o_dmdConfig.dmd)
		});

		var i_store = Ext.create("Ext.data.Store", {
			model: i_model,
			remoteSort: true,
			autoSync: false,
			proxy: i_proxy,
			pageSize: 14,
			listeners: {
				beforeload: {
					fn: function(i_store, i_operation) {

						var i_toolbar = i_self.getPagingBar();

						var s_search = i_toolbar.filterField.getValue();
						var i_proxy = i_store.getProxy();

						var o_extraParams = i_proxy.getExtraParams();

						if (s_search) {
							o_extraParams.search = s_search;
						}
						else {
							delete o_extraParams.search;
						}

						//store.proxy.extraParams.search = i_self.tb.filterField.getValue();

						if (o_dmdConfig.filter) {
							Ext.apply(i_proxy.extraParams, {
								filter: (Ext.isString(o_dmdConfig.filter)) ? o_dmdConfig.filter : Ext.encode(o_dmdConfig.filter)
							});
						}
						else {
							if (o_extraParams.filter) {
								delete o_extraParams.filter;
							}
						}
						if (o_dmdConfig.union && o_dmdConfig.union.length > 0) {
							Ext.apply(i_proxy.extraParams, {
								union: (Ext.isString(o_dmdConfig.union)) ? o_dmdConfig.union : Ext.encode(o_dmdConfig.union)
							});
						}
						else {
							if (o_extraParams.union) {
								delete o_extraParams.union;
							}
						}
					}
				},
				load: {
					fn: function(i_store, a_records) {

						if (o_dmdConfig.union && o_dmdConfig.union.length > 0) {

							var union = (Ext.isString(o_dmdConfig.union)) ? Ext.decode(o_dmdConfig.union) : o_dmdConfig.union;
							var s_prop = union[0].property;
							var n_token = union[0].value;

							Ext.Array.each(a_records, function(i_record) {
								if (i_record.get(s_prop) == n_token) {
									i_self.selModel.select(i_record, false);
								}
							});
						}
					}
				}
			}
		});

		var i_selModel = Ext.create("Ext.selection.CheckboxModel", {
			mode: "SINGLE",
			allowDeselect: true
		});

		var o_columns = i_protoModel.columns;
		var a_columns = o_dmdConfig.columns;
		var o_uiTypes = ip2.getUiTypes();

		var a_uiColumns = [];

		Ext.Array.each(o_columns, function (o_definition, s_key) {

			var o_found;
			var b_isDefined = false;

			Ext.Array.each(a_columns, function (o_col) {
				if (o_col.dataIndex == s_key) {
					o_found = o_col;
					b_isDefined = true;
					return false;
				}
			});

			if (!b_isDefined) {

				var s_uiType = o_definition.uitype || o_definition.uiType;
				var o_uiType = o_uiTypes[s_uiType];

				// Prep the config object from the definition
				var o_config = Ext.apply({}, o_definition.column);

				if (o_config.xpd) {

					var o_xpdInfo = ip2.getFieldXpdInfo(s_key, o_dmdInfo.entity, o_config);

					if (o_xpdInfo) {
						ip2.applyFieldXpdInfo(o_config, o_xpdInfo);
					}
				}

				var o_column = Ext.apply({}, o_config);

				// Could have a renderer applied from uiTypes
				if (o_uiType && o_uiType.grid && o_uiType.grid.renderer) {
					o_column.renderer = o_uiType.grid.renderer;
				}

				o_column.header = o_config.header || ip2.i18n(o_definition.title) || ip2.i18n(o_definition.header) || ip2.i18n({
					da: "Beskrivelse",
					en: "Description"
				});

				Ext.apply(o_column, o_found);

				a_uiColumns.push(o_column);
			}
		});

		i_self.columns = a_uiColumns;

		var i_toolbar = Ext.create("Ext.ux.call2action.PagingToolbar", {
			wrapper: i_self,
			dock: "top",
			store: i_store,
			displayInfo: true
		});

		var o_listeners = i_self.listeners = i_self.listeners || {};

		Ext.apply(o_listeners, {
			afterrender: {
				fn: function(i_self) {
					var i_selectionModel = i_self.getSelectionModel();
					if (i_selectionModel.mode == "SINGLE") {
						i_selectionModel.allowDeselect = true;
					}
				}
			},
			deselectAll: {
				fn: function() {
					i_self.selModel.deselectAll();
				}
			},
			viewready: {
				fn: function(view) {

					i_self.setViewReady(true);
					i_self.wrapper.fireEvent("childviewready");
				}
			}
		});

		i_self.store = i_store;

		i_self.selModel = i_selModel;

		i_self.callParent(arguments);

		i_self.addDocked(i_toolbar);
		i_self.setPagingBar(i_toolbar);

		i_store.load({
			params: {
				start: 0
			}
		});
	}
});
