Ext.define("Ext.ux.grid.filter.String", {
	extend: "Ext.ux.grid.filter.Base",
	alias: "widget.filterstring",

	initComponent: function() {

		var i_self = this;
		i_self.callParent(arguments);
	},

	createMenu: function() {

		var i_self = this;
		i_self.callParent(arguments);

		var config = {
			dataIndex: i_self.dataIndex,
			dataType: "string",
			overRuleable: i_self.overRuleable != null ? i_self.overRuleable : true,
			applyFn: i_self.applyFn || Ext.emptyFn,
			removeFn: i_self.removeFn || Ext.emptyFn
		};

		var i_panel = Ext.ux.grid.filter.util.create(config);

		i_self.config = config;
		i_self.menu.add(i_panel);
	},

	getFilterValue: function() {

		var i_self = this;
		var o_config = i_self.getConfig();
		var a_checkbox = o_config.checkboxes;
		var a_field = o_config.fields;
		var b_not = i_self.getNot();
		var b_filled = i_self.getFilled();
		var s_value = "";
		var s_where = "";
		var s_criteria = "";
		var s_type = "";
		var n_index = 0;
		var s_dataIndex = i_self.getDataIndex();

		Ext.Array.each(a_checkbox, function(i_checkbox) {

			var i_field = a_field[n_index];

			if (i_checkbox.checked) {
				s_value = i_field.getValue();
				s_criteria = i_checkbox.criteria;
				s_type = i_checkbox.type;
			}
			n_index++;
		});

		s_value = s_value || "";

		var a_split = [];

		var a_valueSplit = s_value.split(",");

		if (a_valueSplit.length > 1) {
			a_split = s_criteria.split(" ");
			s_where = i_self.getInStatement(s_dataIndex, a_split[0], a_split[1], s_type, b_not, a_valueSplit);
		}
		else {
			if (s_value) {
				a_split = s_criteria.split(" ");
				a_split[1] = "" + a_split[1] + "";

				s_criteria = a_split.join(" ");
				if (s_type == "eq") {
					s_criteria = s_criteria.replace("{0}", "'" + s_value + "'");
				}
				else {
					s_criteria = s_criteria.replace("{0}", s_value);
				}

				if (o_config.dataIndex.indexOf(".") > -1) {

					var s_root = o_config.dataIndex.split(".").shift();

					s_dataIndex  = s_root + " like '%" + s_value +"%'" + " AND " + s_dataIndex;
				}

				s_where = "" + s_dataIndex + "" + (b_not ? " NOT " + s_criteria : " " + s_criteria);
			}
			else {
				if (b_filled) {
					var i_template = new Ext.XTemplate( 
						"{dataIndex}",
						"<tpl if=\"this.hasValue(not)\">",
							"<tpl if=\"this.hasValue(filled)\">",
								" IS NULL OR {dataIndex} = ''",
							"</tpl>",
							"<tpl if=\"this.notValue(filled)\" >",
								" IS NOT NULL AND {dataIndex} <> '' ",
							"</tpl>",
						"</tpl>",
						"<tpl if=\"this.notValue(not)\">",
							"<tpl if=\"this.hasValue(filled)\">",
								" IS NOT NULL AND {dataIndex} <> ''",
							"</tpl>",
						"</tpl>",
						{
							hasValue: function(value) {
								return value ? 1 : 0;
							},
							notValue: function(value) {
								return value ? 0 :1;
							}
						}
					);

					i_template.compile();

					s_where = i_template.apply({
						dataIndex: "" + s_dataIndex + "",
						not: b_not,
						filled: b_filled
					});
				}
			}
		}
		return s_where;
	},

	getFilled: function() {

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

		var b_blank = false;
		if (o_config.checkBoxBlank) {
			b_blank = o_config.checkBoxBlank.getValue();
		}

		return b_blank;
	},

	getInStatement: function(s_dataIndex, s_criteria, s_replace, s_type, b_not, a_split) {

		var s_where = "";
		var a_result = [];

		Ext.Array.each(a_split, function(s_value) {
			if (s_value) {
				if (s_type == "eq") {
					a_result.push("'" + s_value.trim() + "'");
				}
				else {
					a_result.push("'%" + s_value.trim() + "%'");
				}
			}
		});

		if (s_type == "eq" ) {
			s_where = "" + s_dataIndex + "" + (b_not ? " NOT " : " ") + "IN " + "(" + a_result.join(", ") + ")";
		}
		else {
			s_where = "" + s_dataIndex + " LIKE " +
				a_result.join(" OR " + s_dataIndex + "" + (b_not ? " NOT LIKE " : " LIKE "));
		}

		return s_where;
	}
});
