Ext.define("Portfolio.designer.ui.domainModelEntityColumnGrid", {
	extend: "Ext.grid.Panel",

	alias: [
		"widget.ip2DomainModelEntityColumnGrid"
	],

	viewConfig: {
		plugins: {
			ptype: "gridviewdragdrop"
		}
	},

	config: {
		entityManager: null,
		designer: null,
		entity: null,
		required: false,
		fieldNo: 1
	},

	initComponent: function() {

		var i_self = this;

		var i_plugin = Ext.create("Ext.grid.plugin.CellEditing", {
			clicksToEdit: 1,
			listeners: {
				edit: {
					fn: function(i_editor) {

						var i_context = i_editor.context;
						var i_grid = i_context.grid;
						var i_view = i_grid.getView();
					}
				}
			}
		});

		//i_self.plugins = [i_plugin];

		// grid columns
		i_self.columns = i_self.createColumns();
		i_self.store = i_self.createStore();

		// toolbar
		i_self.tbar = [{
			xtype: "button",
			text: ip2.i18n("addNew"),
			iconCls: "pf-add",
			disabled: i_self.getRequired(),
			handler: function() {

				var i_store = i_self.getStore();
				var i_model = i_store.getModel();
				var i_designer = i_self.getDesigner();
				var o_normal = i_designer.normal;
				var o_types = o_normal.types;

				var i_record = i_model.create({
					type: "varchar",
					name: "new" + i_self.getFieldNo()
				});

				var o_type = o_types.varchar;

				if (o_type && o_type.defaults) {
					Ext.Object.each(o_type.defaults, function(s_key, o_value) {
						i_record.set(s_key, o_value);
					});
				}

				i_store.loadData([i_record], true);

				i_self.getSelectionModel().select(i_record);
			}
		}];

		i_self.callParent();

		i_self.on({
			commonPropertyRendered: {
				fn: function(i_grid, i_columnManager) {

					var i_store = i_self.getStore();
					var a_records = i_store.getRange();

					Ext.Array.each(a_records, function(i_record) {
						if (i_record.get("name") == i_self.columnToEdit) {
							var i_selectionModel = i_self.getSelectionModel();
							i_selectionModel.select(i_record);
							i_columnManager.onRowSelect(i_selectionModel, [i_record]);
							return false;
						}
					});
				}
			},
			itemcontextmenu: {
				fn: function(i_view, i_record, i_html, n_index, i_event) {

					i_event.stopEvent();
					var a_coords = i_event.getXY();

					var i_menu = Ext.create("Ext.menu.Menu", {
						items: [
							{
								xtype: "menuitem",
								text: ip2.i18n("delete"),
								iconCls: "pf-delete",
								disabled: i_self.getRequired(),
								handler: function() {
									i_self.deleteRow();
								}
							}
						]
					});

					i_menu.showAt(a_coords[0], a_coords[1]);
				}
			}
		});

		i_self.enableBubble("removeColumn");
	},

	createColumns: function() {

		var i_self = this;

		var a_columns = [
			{
				header: ip2.i18n("name"),
				dataIndex: "name",
				flex: 1,
				editor: {
					xtype: "textfield"
				}
			},
			{
				header: "System Object Name",
				dataIndex: "sysname",
				width: 150
			},
			{
				header: ip2.i18n("title"),
				dataIndex: "title",
				flex: 1,
				renderer: i_self.titleRenderer
			},
			{
				header: ip2.i18n("TYPE"),
				dataIndex: "type",
				width: 100,
				renderer: i_self.typeRenderer
			},
			{
				header: ip2.i18n("LENGTH"),
				dataIndex: "len",
				width: 100,
				align: "right",
				renderer: i_self.lengthRenderer
			},
			{
				header: ip2.i18n("likeFrom"),
				dataIndex: "like",
				width: 100,
				renderer: i_self.likeRenderer
			},
			{
				header: ip2.i18n("uitype"),
				dataIndex: "uitype",
				width: 100
			}
		];

		return a_columns;
	},

	createStore: function() {

		var i_self = this;
		var o_entity = i_self.getEntity();
		var a_columns = [];

		if (Ext.isObject(o_entity.columns)) {

			Ext.Object.each(o_entity.columns, function(s_key, o_value) {
				a_columns.push(o_value);
			});
		}
		else {
			a_columns = o_entity.columns || [];
		}

		var i_store = Ext.create("Ext.data.Store", {
			model: "Portfolio.designer.model.domainModelEntityColumnManagerDataModel",
			data: a_columns
		});

		return i_store;
	},

	titleRenderer: function(value, i_meta, i_record) {

		if (ip2.isStringedObject(value)) {
			value = Ext.decode(value, true) || "";
		}
		if (Ext.isObject(value)) {
			value = ip2.i18n(value).replace(/,/g, " ");
		}

		return value;
	},

	typeRenderer: function(value, i_meta, i_record) {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var o_normal = i_designer.normal;
		var o_types = o_normal.types;
		var s_like = i_record.get("like");

		if (s_like && o_types) {
			var o_globalType = o_types[s_like];
			if (o_globalType) {
				return o_globalType.type;
			}
			else {
				return value;
			}
		}
		else {
			return value;
		}
	},

	lengthRenderer: function(value, i_meta, i_record) {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var o_normal = i_designer.normal;
		var o_types = o_normal.types;
		var s_like = i_record.get("like");

		if (s_like && o_types) {

			var o_globalType = o_types[s_like];

			if (o_globalType) {
				return o_globalType.len;
			}
			else {
				return value;
			}
		}
		else if (isNaN(value) || value === 0) {
			return "";
		}
		else if (i_record.get("dec")) {
			return value + "," + i_record.get("dec");
		}
		else {
			return value;
		}
	},

	likeRenderer: function(value, meta, record) {

		if (record.get("type") == "like" || record.get("like")) {
			return record.get("like");
		}
		else {
			return record.get("from");
		}
	},

	booleanRenderer: function(v) {
		if (v) {
			return "<div class=\"x-form-field x-form-checkbox\" style=\"" +
					"background-image:url(/system/images/icons/16x16/check2.png); margin:0 0 0px 22px;\"></div>";
		}
		else {
			return "";
		}
	},

	deleteRow: function() {

		var i_self = this;
		var i_store = i_self.getStore();

		var a_records = i_self.getSelectionModel().getSelection();
		var i_record = a_records[0];

		ip2.messageBox({
			title: ip2.i18n({
				da: "Bekræft slet",
				en: "Confirm Delete"
			}),
			message : ip2.i18n({
				da: "Er du sikker på du vil slette..?",
				en: "Are you sure you want to delete"
			}),
			yesText: ip2.i18n({
				da: "Ja",
				en: "Yes"
			}),
			noText: ip2.i18n({
				da: "Nej",
				en: "No"
			}),
			yesFn: function() {
				i_self.fireEvent("removeColumn", i_record.get("name"));

				i_store.remove(i_record);
				i_self.getSelectionModel().select(0);
			}
		});
	}
});
