Ext.define('Ext.ux.stackorganizer.view.StackTree', {

	extend: 'Ext.tree.Panel',

	alias: ['widget.stacktree'],

	mixins: {
		'keyMapping': 'Ext.ux.util.KeyMapping'
	},

	_silence: false,

	initComponent: function () {

		var i_cmp = this;

		Ext.apply(i_cmp, {
			viewConfig: {
				plugins: {
					ptype: 'treeviewdragdrop',
					enableDrag: true,
					enableDrop: true,
					dropGroup: "grid2tree",
					dragGroup: "grid2tree",
					allowContainerDrops: true,
					appendOnly: false,
					dropZone: {
						isValidDropPoint: function(node, position, dragZone, e, data) {

							var i_view = this;
							var a_records = data.records;
							var b_allowed = true;
							if (i_view.id == data.view.id) {
								ip2.each(a_records, function(i_record) {
									if (!i_record.isLeaf()) {
										b_allowed = false;
									}
								});
							}
							return b_allowed;
						},
						onContainerOver: function(dd, e, data) {

							var i_view = this;
							var a_records = data.records;
							var b_allowed = true;
							if (i_view.id == data.view.id) {

								ip2.each(a_records, function(i_record) {
									if (!i_record.isLeaf()) {
										b_allowed = false;
									}
								});
							}
							return b_allowed ? i_view.dropAllowed : e.getTarget('.' + i_view.indicatorCls) ? i_view.currentCls : i_view.dropNotAllowed;
						},
						onContainerDrop: function(dragZone, e, data) {

							var i_view = this;
							var a_records = data.records;
							var b_allowed = true;
							if (i_view.id == data.view.id) {
								ip2.each(a_records, function(i_record) {
									if (!i_record.isLeaf()) {
										b_allowed = false;
									}
								});
							}
							if (i_view.allowContainerDrops && b_allowed) {
								i_view.valid = true;
								i_view.currentPosition = 'append';
								i_view.overRecord = this.view.store.getRoot();
								i_view.onNodeDrop(this.overRecord, dragZone, e, data);
							}
							return b_allowed;
						}
					}
				},
				listeners: {
					beforedrop: {
						fn: function(node, data, overModel, dropPosition, dropHandler, e) {
							var i_view = this;
							var a_records = data.records;
							if (i_view.id != data.view.id) {
								data.copy = true;
							}
						}
					},
					drop: {
						fn: function(node, data, overModel, posistion) {
							var i_view = this;
							var a_records = data.records;
							if (i_view.id != data.view.id) {
								var i_owner = data.view.ownerCt;
								ip2.each(a_records, function(i_record) {
									i_owner.fireEvent('stackadd', i_owner, i_record, false);
								});
							}
						}
					}
				}
			},
			enableColumnResize: false,
			sortableColumns: false,
			enableColumnHide: false,
			columns: [
				{
					text: ip2.i18n({
						da: 'Titel',
						en: 'Title'
					}),
					hidden: true,
					dataIndex: 'title',
					renderer: function (value) {

						var s_title = value,
							o_title,
							s_lang = ip2.session.getLanguage();

						try {
							o_title = Ext.decode(value);
						}
						catch (e) {}

						if (o_title) {
							s_title = o_title[s_lang];
						}

						return s_title;
					}
				},
				{
					text: ip2.i18n({
						da: 'Token',
						en: 'Token'
					}),
					hidden: true,
					dataIndex: 'token'
				},
				{
					text: ip2.i18n({
						da: 'Stak ID',
						en: 'Stack ID'
					}),
					hidden: true,
					dataIndex: 'stackId'
				},
				{
					xtype: 'treecolumn',
					text: ip2.i18n({
						da: 'Toplink',
						en: 'Toplink'
					}),
					tooltip: ip2.i18n({
						da: 'Marker for at sætte som et toplink',
						en: 'Check to set as toplink'
					}),
					width: 80,
					tdCls: 'x-ux-stack-column-checkbox'
				},
				{
					xtype: 'stackiconcolumn',
					text: ip2.i18n({
						da: 'Type',
						en: 'Type'
					}),
					dataIndex: 'ptype',
					width: 110
				},
				{
					xtype: 'stackpropertiescolumn',
					dataIndex: 'properties',
					text: ip2.i18n({
						da: 'Specifikationer',
						en: 'Specifications'
					}),
					flex: 1
				},
				{
					xtype: 'actioncolumn',
					text: ip2.i18n({
						da: 'Flyt op',
						en: 'Move up'
					}),
					tooltip: ip2.i18n({
						da: 'Flyt op',
						en: 'Move up'
					}),
					iconCls: 'pf-arrow_up',
					width: 25,
					handler: function (i_view, n_row, n_col, o_item, o_event) {

						var i_store = i_cmp.getStore(),
							i_root = i_store.getRootNode(),
							i_node = i_root.getChildAt(n_row);

						i_cmp.moveUp(i_node, i_store, i_root, n_row, n_col, o_item, o_event);
					},
					tdCls: 'x-ux-stack-column-action'
				},
				{
					xtype: 'actioncolumn',
					text: ip2.i18n({
						da: 'Flyt ned',
						en: 'Move down'
					}),
					tooltip: ip2.i18n({
						da: 'Flyt ned',
						en: 'Move down'
					}),
					iconCls: 'pf-arrow_down',
					width: 25,
					handler: function (i_view, n_row, n_col, o_item, o_event) {

						var i_store = i_cmp.getStore();
						var i_root = i_store.getRootNode();
						var i_node = i_root.getChildAt(n_row);

						i_cmp.moveDown(i_node, i_store, i_root, n_row, n_col, o_item, o_event);
					},
					tdCls: 'x-ux-stack-column-action'
				},
				{
					xtype: 'actioncolumn',
					text: ip2.i18n({
						da: 'Fjern',
						en: 'Remove'
					}),
					tooltip: ip2.i18n({
						da: 'Fjern',
						en: 'Remove'
					}),
					iconCls: 'pf-navigate_cross',
					width: 25,
					handler: function (i_view, n_row) {
						var i_tree = i_view.ownerCt;
						var i_root = i_tree.getRootNode();
						var i_node = i_root.getChildAt(n_row);
						i_node.remove(false);
						i_tree.fireEvent("tree_delete", i_tree, i_node);
					},
					tdCls: 'x-ux-stack-column-action'
				}
			]
		});

		i_cmp.callParent();

		i_cmp.initKeyMapping();
		i_cmp.enableBubble("tree_delete");

		return i_cmp;
	},
	moveUp: function (i_node, i_store, i_root, n_row, n_col, o_item, o_event) {

		var i_cmp = this,
			i_prev = i_node.previousSibling;

		i_cmp._silence = true;

		// Select this node we are moving
		i_cmp.getSelectionModel().select(i_node);

		if (i_prev) {
			i_root.insertBefore(i_node, i_prev);
		}

		i_cmp._silence = false;

		return i_cmp;
	},
	moveDown: function (i_node, i_store, i_root, n_row, n_col, o_item, o_event) {

		var i_cmp = this,
			i_next = i_node.nextSibling;

		i_cmp._silence = true;

		// Select this node we are moving
		i_cmp.getSelectionModel().select(i_node);

		if (i_next) {
			i_root.insertBefore(i_next, i_node);
		}

		i_cmp._silence = false;

		return i_cmp;
	},
	getLastSelectedNode: function () {

		var i_cmp = this,
			a_selected = i_cmp.getSelectionModel().getSelection(),
			i_node;

		if (a_selected.length) {
			i_node = a_selected[a_selected.length - 1];
		}
		return i_node;
	},

	setNodeFocus: function (i_node) {

		var i_cmp = this,
			i_root = i_cmp.getRootNode();

		if (i_node) {
			i_cmp.getSelectionModel().select(i_node);
		}
		else {
			i_node = i_cmp.getLastSelectedNode();
		}

		if (i_node) {
			i_cmp.getView().focusRow(i_node);
		}

		return i_cmp;
	},
	setActiveFocus: function () {

		var i_cmp = this,
			i_view = i_cmp.getView(),
			i_model = i_view.getSelectionModel(),
			a_selected = i_model.getSelection(),
			i_selected;

		if (!a_selected.length) {
			i_selected = i_view.getStore().getAt(0);
			a_selected = i_selected ? [i_selected] : [];
		}

		i_view.focus();
		i_model.select(a_selected);

		return i_cmp;
	},

	cleanIndexes: function () {

		var i_cmp = this,
			i_root = i_cmp.getRootNode();

		Ext.Array.each(i_root.childNodes, function (o_child, n_index) {
			o_child.set('index', n_index);
		});

		return i_cmp;
	}
});
