Ext.define("Ext.ux.grid.filter.Base", {
	extend: "Ext.button.Split",

	alias: "widget.filterbase",

	iconCls: "pf-arrow_up",

	dataIndex: "",
	dataType: "string",
	overRuleable: true,
	toolbar: undefined,
	menu: undefined,
	fields: undefined,
	config: undefined,
	sortInfo: undefined,
	isFilter: true,
	sortDirection: "ASC",
	iconAlign: "right",
	useRaw: false,

	config: {
		allowMenu: true
	},

	listeners: {
		mouseover: {
			fn: function () {

				var i_self = this;
				var s_iconCls = i_self.sortInfo.direction == "ASC" ? "pf-arrow_up" : "pf-arrow_down";
				i_self.setIconCls(s_iconCls);
			}
		},
		mouseout: {
			fn: function () {

				var i_self = this;
				var s_iconCls = i_self.sortInfo.direction == "ASC" ? "pf-arrow_up" : "pf-arrow_down";
				i_self.setIconCls(s_iconCls);
			}
		},
		click: {
			fn: function() {

				var i_self = this;
				i_self.changeSort();
				var o_config = i_self.getConfig();
				o_config.applyFn();
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		i_self.iconCls = i_self.sortDirection == "ASC" ? "pf-arrow_up" : "pf-arrow_down";
		i_self.callParent(arguments);

		if (i_self.getAllowMenu()) {
			i_self.createMenu();
		}
		else {
			i_self.setMenu(null);
		}

		i_self.createSortInfo();

		return i_self;
	},

	createMenu: function() {

		var i_self = this;

		var i_menu = Ext.widget("menu", {
			plain: true,
			enableKeyNav: false,
			listeners: {
				beforehide: {
					fn: function() {

						var i_menu = this;

						var a_fields = i_menu.query("combobox");

						var b_close = true;

						Ext.Array.each(a_fields, function(i_field) {
							if (i_field.isExpanded) {
								b_close = false;
								return false;
							}
						});

						return b_close;
					}
				}
			}
		});

		i_menu.button = i_self;

		i_self.setMenu(i_menu);
	},

	createSortInfo: function() {

		var i_self = this;

		var o_sortInfo = {
			property: i_self.sortProperty || i_self.dataIndex,
			direction: i_self.sortDirection || "ASC"
		};

		i_self.sortInfo = o_sortInfo;
	},

	changeSort: function() {

		var i_self = this;
		i_self.sortInfo.direction = i_self.sortInfo.direction == "ASC" ? "DESC" : "ASC";
		var s_iconCls = i_self.sortInfo.direction == "ASC" ? "pf-arrow_up" : "pf-arrow_down";
		i_self.setIconCls(s_iconCls);
	},

	getNot: function() {

		var i_self = this;
		var o_config = i_self.getConfig();

		var b_not = false;
		if (o_config.checkBoxNot) {
			b_not = o_config.checkBoxNot.getValue();
		}

		return b_not;
	},

	setNot: function(value) {

		var i_self = this;
		var o_config = i_self.getConfig();

		o_config.checkBoxNot.setValue(value);
	},

	getFilterValue: function() {
		// Implement in extended class
	},

	getSortInfo: function() {

		var i_self = this;
		return i_self.sortInfo;
	},

	getConfig: function() {

		var i_self = this;
		return i_self.config;
	},

	onFocusLeave: function(e) {

		var i_self = this;
		var i_menu = i_self.getMenu();
		var event = e.event;

		if (i_menu && !event.within(i_menu.getEl())) {
			i_menu.hide();
		}
	},

	getDataIndex: function() {

		var i_self = this;
		var o_config = i_self.getConfig();
		var s_dataIndex = o_config.dataIndex;

		if (s_dataIndex.indexOf(".") > -1) {
			var a_splitData = s_dataIndex.split(".");
			var s_root = a_splitData.shift();

			var i_templatet = Ext.create("Ext.XTemplate",
				"jsonGetStr({root},'{path}')"
			);

			i_templatet.compile();

			s_dataIndex = i_templatet.apply({
				root: s_root,
				path: a_splitData.join(".")
			});
		}

		return s_dataIndex;
	}
});
