
Ext.define("Ext.ux.gallery.view.LinkPageGrid", {

	extend: "Ext.grid.Panel",

	alias: "widget.mediagallerylinkpagegrid",

	primaryId: null,
	primaryKey: null,
	titleId: null,
	titleKey: null,
	value: "",

	_touched: false,

	config: {
		entity: "",
		dmd: "",
		uiName: "",
		model: null,
		info: null,
		where: ""
	},

	initComponent: function () {

		var i_self = this;
		var s_dmd = i_self.getDmd();
		var s_entity = i_self.getEntity();
		var s_uiName = i_self.getUiName() || "portfolio";
		var o_dmd = ip2.resource.get(s_dmd);

		var i_model = ip2.model({
			dmd: o_dmd._path,
			entityName: s_entity
		});

		var o_Info = ip2.dmd.getInfo({
			dmd: s_dmd,
			view: s_uiName,
			entity: s_entity,
			context: "grid"
		});

		i_self.setInfo(o_Info);

		i_self.setModel(i_model);

		// Get the primary column ID from model
		i_self.primaryId = i_self.getPrimaryKey();

		// Get the title column ID from model
		i_self.titleId = i_self.getPrimaryTitleKey();

		// Get the title from entity for this grid
		var s_title = (function (o_entity) {
			return ip2.i18n(o_entity.title || "No title for this entity!!");
		})(i_model.prototype.entity);

		var i_store = Ext.create("Ext.data.BufferedStore", {
			model: i_model,
			proxy: ip2.getProxyByEntity({
				dmd: s_dmd,
				entity: s_entity,
				view: s_uiName
			}),
			remoteSort: true,
			autoSync: false,
			pageSize: 100,
			autoLoad: true
		});

		var i_proxy = i_store.getProxy();
		var o_extraParams = i_proxy.getExtraParams();

		Ext.apply(o_extraParams, {
			action: "getRows",
			context: "grid",
			uiName: s_uiName,
			dmdFile: o_dmd._path
		});

		if (i_self.where) {
			o_extraParams.where = i_self.where;
		}

		Ext.apply(i_self, {
			title: s_title,
			selModel: Ext.create("Ext.selection.CheckboxModel", {
				mode: "SINGLE",
				allowDeselect: true
			}),
			store: i_store,
			columns: (function (i_model) {

				var a_column = [];
				var o_dmdInfo = o_Info;
				var o_entity = o_Info.entity;

				Ext.Array.each(o_entity.ui.grids[s_uiName].columns, function (o_field, n_index) {

					var s_name = o_field.name;
					var o_column = i_model.columns[s_name] || {};
					var o_config;

					o_config = Ext.Object.merge(o_column.column || {}, o_column, o_field);

					// THIS COULD BE A XPD FIELD - AND AS SUCH THE TITLE IS TO BE FOUND IN THE XPD
					if (o_config.xpd) {

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

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

					if (o_config.title && !o_config.header) {
						o_config.header = o_config.title;
					}

					o_config.header = ip2.i18n(o_config.header);

					if (!o_config.width) {
						o_config.flex = 2;
					}

					if (o_config.dataIndex == "PLGPROP___CID__") {

						o_config.flex = 1;

						o_config.renderer = function(value, meta, record, rowIndex, colIndex) {

							var o_xpd = ip2.resource.get("pages.xpd", true);
							var o_collection = o_xpd.collections;
							var s_title = "";

							if (o_collection[value]) {
								s_title = o_collection[value].title;
								s_title = ip2.xpd.translate(o_xpd, s_title);
							}

							return s_title;
						};
					}

					if (!o_config.dataIndex) {
						o_config.dataIndex = o_config.name;
					}

					a_column.push(o_config);
				});

				return a_column;
			})(i_model.prototype)
		});

		i_self.callParent(arguments);

		i_self.on({
			afterrender: {
				fn: function(i_self) {
					var i_selectionModel = i_self.getSelectionModel();
					if (i_selectionModel.mode == "SINGLE") {
						i_selectionModel.allowDeselect = true;
					}
				}
			},
			select: {
				fn: function () {
					i_self._touched = true;
				},
				single: true
			},
			deselect: {
				fn: function () {
					i_self._touched = true;
				},
				single: true
			}
		});

		i_self.onInitialize && i_self.onInitialize();

		return i_self;
	},

	getPrimaryTitleKey: function () {

		var i_self = this;
		var o_Info = i_self.getInfo();
		var o_entity = o_Info.entity;
		var s_uiName = i_self.getUiName();
		var s_id;
		var o_ui = o_entity.ui.grids[s_uiName];
		var b_found = false;

		Ext.Array.some(o_ui.columns, function (o_column, n_index) {
			if (o_column.isTitle) {

				if (o_column.isPrimaryTitle) {
					s_id = o_column.name;
					return true;
				}
				else {
					s_id = o_column.name;
				}
			}
		});

		return s_id;
	},
	getPrimaryKey: function () {

		var i_self = this;
		var o_Info = i_self.getInfo();
		var o_entity = o_Info.entity;

		return o_entity.idColumn;
	},

	getValue: function () {

		var i_self = this;
		var s_primaryId = i_self.primaryId;
		var s_titleId = i_self.titleId;
		var o_value;
		var i_record;

		var a_records = i_self.getSelectionModel().getSelection();

		if (a_records.length > 0) {

			i_record = a_records[0];

			o_value = {
				type: i_self.name,
				token: i_record.get(s_primaryId)
			};

			if (s_titleId && i_record.get(s_titleId)) {
				o_value.title = i_record.get(s_titleId);
			}
		}
		else if (!i_self._touched) {
			o_value = i_self.value;
		}
		else {
			o_value = "";
		}

		return o_value;
	},
	setValue: function (o_value) {

		var i_self = this;
		var s_primaryId = i_self.primaryId;
		var s_titleId = i_self.titleId;
		var i_store = i_self.getStore();
		var i_proxy = i_store.getProxy();
		var o_extraParams = i_proxy.getExtraParams();
		var n_token = o_value.token;

		i_self.value = o_value;

		if (n_token) {

			i_self.primaryKey = n_token;
			i_self.titleKey = o_value.title || null;

			o_extraParams.union = Ext.encode([{
				property: s_primaryId,
				value: n_token
			}]);

			i_store.load();
		}

		return i_self;
	}
});
