 Ext.define("Portfolio.designer.ui.domainModelBaseDatatypeGrid", {

	extend: "Ext.grid.Panel",

	alias: [
		"widget.ip2DomainModelBaseDatatypeGrid"
	],

	border: 0,

	autoScroll: true,

	config: {
		designer: null
	},

	requires: [
		"Portfolio.designer.model.domainModelBaseDatatypeManagerDataModel"
	],

	initComponent: function() {
		var me = this;

		var titleRenderer = function(v) {
			return ip2.i18n(v);
		};
		me.columns = [
			{
				header: ip2.i18n("name"),
				dataIndex: "name"
			},
			{
				header: ip2.i18n("title"),
				dataIndex: "title",
				flex: 3,
				renderer: titleRenderer
			},
			{
				header: ip2.i18n("TYPE"),
				dataIndex: "type",
				width: 66
			},
			{
				header: ip2.i18n("LENGTH"),
				dataIndex: "len",
				width: 60,
				align: "right"
			}
		];

		var o_designer = me.getDesigner();
		var o_model = o_designer.model;

		var a_types = [];

		Ext.Object.each(o_model.types, function(s_key, o_value) {
			o_value.name = s_key;
			a_types.push(o_value);
		});

		me.store = Ext.create("Ext.data.Store", {
			model: "Portfolio.designer.model.domainModelBaseDatatypeManagerDataModel",
			data: a_types,
			proxy: {
				type: "memory",
				reader: {
					type: "json"
				}
			}
		});

		var rowMenu = Ext.create("Ext.menu.Menu", {
			items: [
				{
					xtype: "menuitem",
					text: ip2.i18n({
						da: "Slet",
						en: "Delete"
					}),
					iconCls: "pf-delete",
					handler: function() {
						me.deleteRow();
					}
				}
			]
		});

		me.listeners = {
			itemcontextmenu: function(view, record, item, index, e) {
				e.stopEvent();
				var coords = e.getXY();
				rowMenu.showAt([coords[0], coords[1]]);
			}
		};

		me.tbar = [{
			xtype: "button",
			text: ip2.i18n({
				da: "Tilføj",
				en: "Add"
			}),
			iconCls: "pf-add",
			handler: function() {
				var store = me.getStore();
				var model = store.getProxy().getModel();
				var record = model.create({});
				store.add(record);
				me.getSelectionModel().select(record);
				if (!me.wasDirty) {
					me.fireEvent("basedatatypechanged", me, me.isDirty());
				}
			}
		}];
		me.callParent();
		me.resetOriginalValue();
		me.enableBubble("basedatatypechanged");
	},

	getValue: function() {

		var i_self = this;
		var value;

		var a_records = i_self.store.getRange();
		var a_value = [];

		Ext.Array.each(a_records, function(i_record) {
			a_value.push(i_record.data);
		});

		i_self.value = value = Ext.encode(a_value);
		return value;
	},

	isDirty: function() {

		var i_self = this;
		var value = i_self.getValue();
		var b_dirty = i_self.originalValue != value;
		i_self.wasDirty = b_dirty;
		return b_dirty;
	},

	resetOriginalValue: function() {

		var i_self = this;

		var a_records = i_self.store.getRange();
		var a_value = [];

		Ext.Array.each(a_records, function(i_record) {
			a_value.push(i_record.data);
		});

		i_self.originalValue = i_self.value = Ext.encode(a_value);
		i_self.wasDirty = false;
	},

	deleteRow: function() {
		var me = this;

		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 () {
				var a_records = me.getSelectionModel().getSelection();
				me.store.remove(a_records);

				me.fireEvent("basedatatypechanged", me, me.isDirty());
			}
		});
	}
  });
