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

	extend: "Ext.grid.Panel",

	alias: [
		"widget.ip2DomainModelEntityAffiliationGrid"
	],

	border: 0,

	height: 150,

	autoScroll: true,

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

	config: {
		entity: {},
		designer: null,
		entityManager: null,
		toolBar: null,
		required: false
	},

	listeners: {
		itemcontextmenu: {
			fn: function(i_view, i_record, i_element, n_index, i_event) {

				i_event.stopEvent();

				var a_coords = i_event.getXY();

				var i_self = this;

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

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

	initComponent: function() {

		var i_self = this;

		var o_entity = i_self.getEntity();
		var i_entityManager = i_self.getEntityManager();

		i_self.columns = i_self.createColumns();

		var a_affiliations = Ext.Array.from(o_entity.affiliations || []);

		i_self.store = i_self.createStore(a_affiliations);

		i_self.plugins = Ext.create("Ext.grid.plugin.CellEditing", {
			pluginId: "celledit",
			clicksToEdit: 1,
			listeners: {
				edit: function(i_editor, i_context) {

					var i_record = i_context.record;

					i_record.commit();
					i_entityManager.fireEvent("updateAffiliation", i_record.getData());
				}
			}
		});

		i_self.callParent();

		var i_toolbar = i_self.createToolbar();

		i_self.addDocked(i_toolbar);

		var i_plugin = i_self.getPlugin("celledit");

		i_plugin.on({
			beforeedit: {
				fn: function(i_plugin, i_context) {

					if (i_self.getRequired()) {
						return false;
					}
				}
			}
		});
	},

	createStore: function(a_affiliations) {

		var i_self = this;

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

		return i_store;
	},

	createColumns: function() {

		var i_self = this;

		var i_desinger = i_self.getDesigner();

		var o_model = i_desinger.model;

		var i_entityManager = i_self.getEntityManager();

		var a_columns = [
			{
				header: ip2.i18n("name"),
				dataIndex: "name",
				flex: 1,
				editor: {
					xtype: "textfield"
				}
			},
			{
				header: ip2.i18n("title"),
				dataIndex: "title",
				flex: 1,
				editor: {
					xtype: "i18n-textfield",
					model: o_model,
					entityManager: i_entityManager
				}
			},
			{
				header: ip2.i18n("url"),
				dataIndex: "url",
				flex: 2,
				editor: {
					xtype: "textfield"
				}
			}
		];

		return a_columns;
	},

	createToolbar: function() {

		var i_self = this;

		var i_toolbar = Ext.widget("toolbar", {
			dock: "top",
			items: [{
				xtype: "button",
				text: ip2.i18n("addNew"),
				iconCls: "pf-add",
				handler: function() {

					var i_store = i_self.getStore();
					var i_model = i_store.getModel();
					var i_entityManager = i_self.getEntityManager();

					var i_record = i_model.create({
						renderAs: "tab"
					});

					i_store.add(i_record);

					i_entityManager.fireEvent("newAffiliations");

					var i_plugin = i_self.getPlugin("celledit");

					i_plugin.startEditByPosition({
						row: i_store.count() - 1,
						column: 0
					});
				}
			}]
		});

		i_self.setToolBar(i_toolbar);

		return i_toolbar;
	},

	renderAsRenderer: function(value, meta, record) {

		var i_self = this;
		var i_desinger = i_self.getDesigner();
		var o_normal = i_desinger.normal;

		return o_normal.renderAs[value].title;
	},

	deleteRow: function() {

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

		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 = i_self.getSelectionModel().getSelection();

				Ext.Array.each(a_records, function (i_record) {
					var s_name = i_record.get("name");

					i_entityManager.fireEvent("removeAffiliation", s_name);
				});

				i_store.remove(a_records);

				i_self.getSelectionModel().select(0);
			}
		});
	},

	doSave: function(o_entity) {

		var i_self = this;

		if (!i_self.isValid()) {
			return false;
		}

		var o_affiliations = {};
		var i_store = i_self.getStore();
		var a_records = i_store.getRange();

		Ext.Array.each(a_records, function(i_record) {

			var o_value = {};
			var s_name = "";
			var o_data = i_record.getData();

			Ext.Object.each(o_data, function(s_key, value) {
				switch (s_key) {
					case "drop":
						break;
					case "name":
						s_name = value;
						break;
					default:
						o_value[s_key] = value;
						break;
				}
			});

			o_affiliations[s_name] = o_value;
		});

		if (Ext.Object.isEmpty(o_affiliations)) {
			delete o_entity.affiliations;
		}
		else {

			Ext.apply(o_entity, {
				affiliations: o_affiliations
			});
		}

		return true;
	},

	isValid: function() {
		return true;
	}
});
