/**
 * Notification text command class
 * @author Andrey Lugovtsov
 */
var notificationTextCommand = Class.create(baseTextCommand, {
	
	initialize: function($super, el, indicator, autoCompleter) {
		$super(new RegExp('@$|[\\s]+@[\\s]+|@[\\w\\d-_]+|@\\[[l|t|p]\\]\'[\\s\\w\\d-_]*[^\']$|@\\[[l|t|p]\\]\'$', 'gi'), el, indicator);
		this.autoCompleter = autoCompleter;
		this.lastAutoComplete = null;
		this.lastAutoCompleteType = null;
		this.matches = null;
	},
	
	onFirstSuccessTest: function(matches) {
		var s = this.getIndicatorText(matches);
		return; 
		if (s) {
			this.indicator.update(s);
			this.showIndicator();
		}
	},
	
	onFirstFailureTest: function() {
//		window.console.log('onFirstFailureTest');
		this.hideIndicator();
	},
	
	onFailureTest: function() {
		if (this.autoCompleter) {
			this.autoCompleter.hideResults();
			this.hideTypingAppeal();
		}
	},
	
	onSuccessTest: function(matches) {
		var s = this.getIndicatorText(matches);
		return; 
		if (s) {
			this.indicator.update(s);
			if (! this.indicator.visible()) {
				this.showIndicator();
			}
		}
		else if (this.indicator.visible()) {
			this.hideIndicator();
		}
	},
	
	getIndicatorText: function(rawMatches) {
		var s = this.el.getValue(), matches = [];
		var types = [];
		
		rawMatches.each(function(m) {
			var type = m.match('\\[(t|p|l)\\]');
			types.push(type ? type[1].toLowerCase() : null);
			matches.push(m.gsub('\\[[p|t|l]\\][\'\"]*', ''));
		});
		
		$A(matches).each(function(possibleLogin, index) {
			var r = new RegExp('[\\d\\w]+[-\\.]*[\\d\\w]*' + possibleLogin, 'i');
			matches[index] = possibleLogin.replace('@', '');
			if (r.test(s)) {
				matches[index] = null;
				s = s.replace(r, '');
			}
		});
		matches = matches.compact().uniq();
		if (matches.size() > 0) {
			this.autoCompleterQueryWrapper(matches.last(), types.last());
			return 'Mention ' + (matches.size() > 1 ? 's ' : ' ') + matches.join(', ');
		}
		else {
			return false;
		}
	},
	
	showTypingAppeal: function() {
		//window.console.log('showTypingAppeal');
		$(this.autoCompleter.config.typingAppeal).show();
	},
	
	hideTypingAppeal: function() {
		//window.console.log('hideTypingAppeal');
		if ($(this.autoCompleter.config.typingAppeal)) { 
			$(this.autoCompleter.config.typingAppeal).hide();
		}
	},
	
	autoCompleterQueryWrapper: function(query, type) {
		if (query.length < 1 && !type) {
			this.autoCompleter.hideResults();
			this.showTypingAppeal();
			return false;
		}
		this.hideTypingAppeal();
		if (this.autoCompleter) {
			if (this.lastAutoComplete === null || (this.lastAutoComplete != query || this.lastAutoCompleteType != type)) {
				this.autoCompleter.query(this.lastAutoComplete = query, null, {'type': this.lastAutoCompleteType = type});
			}
			else if (query.strip().blank() && this.lastAutoComplete !== null) {
				//this.autoCompleter.query('');
				//this.autoCompleter.showResults();
			}
		}
	}
});
