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

	extend: "Ext.panel.Panel",

	alias: [
		"widget.ip2DomainModelEntityHierarchyManager"
	],

	title: ip2.i18n({
		da: "Hieraki",
		en: "Hierarchy"
	}),

	layout: {
		type: "border"
	},

	config: {
		treeManager: null,
		propertyManager: null,
		selected: null,
		entity: {},
		designer: {},
		entityManager: null,
		required: false
	},

	requires: [
		"Portfolio.designer.ui.domainModelEntityHierarchyTree",
		"Portfolio.designer.ui.domainModelEntityHierarchyProperties"
	],

	initComponent: function() {

		var i_self = this;
		var o_entity = i_self.getEntity();
		var i_entityManager = i_self.getEntityManager();
		var i_designer = i_self.getDesigner();

		i_self.dockedItems = [{
			xtype: "toolbar",
			dock: "top",
			items: [{
				xtype: "button",
				text: ip2.i18n({
					da: "Tilføj",
					en: "Add"
				}),
				iconCls: "pf-add",
				disabled: (o_entity.extend || i_self.getRequired()) ? true : false,
				handler: function() {

					var o_data = {};
					var i_propertyManager = i_self.getPropertyManager();

					i_propertyManager.updateValues(o_data);
					i_propertyManager.show();
				}
			}]
		}];

		var i_hierarchyTree = Ext.widget("ip2DomainModelEntityHierarchyTree", {
			region: "west",
			border: 0,
			width: 300,
			split: false,
			designer: i_designer,
			entityManager: i_entityManager,
			entity: o_entity,
			disabled: i_self.getRequired()
		});

		var i_hierarchyProperties = Ext.widget("ip2DomainModelEntityHierarchyProperties", {
			designer: i_designer,
			entityManager: i_entityManager,
			entity: o_entity,
			hierarchyManager: i_self,
			region: "center",
			border: 0,
			hidden: true,
			disabled: i_self.getRequired()
		});

		i_self.items = [
			i_hierarchyTree,
			i_hierarchyProperties
		];

		i_self.callParent();

		var i_selectionModel = i_hierarchyTree.getSelectionModel();
		i_selectionModel.on("select", i_self.onNodeSelect, i_self);

		i_self.setTreeManager(i_hierarchyTree);
		i_self.setPropertyManager(i_hierarchyProperties);

		i_self.on({
			updateTree: {
				fn: function(i_propertyManager, s_property, o_source) {
					if (s_property == "baseTable") {
						i_self.updateTree(null, o_source[s_property], o_source);
					}
					else {
						i_self.updateTree(o_source[s_property], null, o_source);
					}
				}
			}
		});
	},

	onNodeSelect: function(i_selectionModel, i_record) {

		var i_self = this;
		var i_properties = i_self.getPropertyManager();
		i_self.setSelected(i_record);

		var o_data = Ext.apply({}, i_record.get("extend"));

		i_properties.updateValues(o_data);

		i_properties.show();
	},

	updateTree: function(s_entity, s_baseTable, o_values) {

		var i_self = this;

		var i_tree = i_self.getTreeManager();

		var o_root = i_tree.buildHierarchy(s_entity, s_baseTable, o_values);

		i_tree.setRootNode(o_root);

		var i_root = i_tree.getRootNode();

		if (i_root.firstChild) {
			i_tree.getSelectionModel().select(i_root.firstChild);
		}
		else {
			i_self.setSelected(null);
		}
	}
});
