Ext.define('Ext.ux.form.field.KeywordDensityChecker', {

	extend: 'Ext.container.Container',

	alias: ['widget.ip2KeywordDensityChecker'],

	mixins: {
		field: 'Ext.form.field.Field'
	},

	ignorelist: 'du er i og på til af at har for med om fra sig jeg det se vi nu hos din et mod gør kan den vil før år her få var som må nok'.split(' '),
	maxCombinedWords: 4,

	changeListener: function() {

		var a_densities = this.calculateDensity(this.getValue());

		var a_items = [];

		Ext.Array.each(a_densities, function(a_wordList, key) {
			var n_words = key + 1;

			var o_grid = {
				xtype: 'grid',
				title: ip2.i18n({
					da: n_words + ' ord',
					en: n_words == 1 ? '1 word' : n_words + ' words'
				}),
				store: {
					fields: ['word', 'times', 'density'],
					data: {
						items: a_wordList
					},
					proxy: {
						type: 'memory',
						reader: {
							type: 'json',
							root: 'items'
						}
					}
				},
				columns: [
					{
						header: ip2.i18n({
							da: 'Sætning',
							en: 'Sentence'
						}),
						dataIndex: 'word'
					},
					{
						header: ip2.i18n({
							da: 'Antal matches',
							en: 'Match Count'
						}),
						dataIndex: 'times'
					},
					{
						header: ip2.i18n({
							da: 'Keyword tæthed',
							en: 'Keyword Density'
						}),
						dataIndex: 'density',
						xtype: 'templatecolumn',
						tpl: '{[values.density*100]}%'
					}
				]
			};

			a_items.push(o_grid);
		});

		this.removeAll();

		this.add({
			xtype: 'tabpanel',
			items: a_items
		});
	},

	_addDensityWord: function(list, word) {
		if (list[word]) {
			list[word]++;
		}
		else {
			list[word] = 1;
		}
	},

	calculateDensity: function(s_words) {
		var i_cmp = this;

		s_words = s_words.replace(/[\n\s]+/g, ' ').toLowerCase();

		a_words = s_words.split(' ');

		// used to store word counters:
		var o_density = {};

		var n_maxCombinedWords = this.maxCombinedWords;

		Ext.Array.each(a_words, function(s_word, n_key) {
			var a_wordList = [];

			for (var i = 0; i < n_maxCombinedWords; i++) {
				if (a_words[n_key - i] != null) {
					a_wordList.unshift(a_words[n_key - i]);

					i_cmp._addDensityWord(o_density, a_wordList.join(' '));
				}
				else {
					break;
				}
			}
		});

		// remove ignored words:
		Ext.Array.each(this.ignorelist, function(s_word) {
			delete o_density[s_word];
		});

		// calculate density
		var a_densities = [];

		Ext.Object.each(o_density, function(s_prop, n_times) {
			var n_length = s_prop.split(' ').length;

			if (a_densities[n_length - 1] == null) {
				a_densities[n_length - 1] = [];
			}

			a_densities[n_length - 1].push({
				word: s_prop,
				times: n_times,
				density: Math.round(n_times * n_length / a_words.length * 100) / 100
			});
		});

		Ext.Array.each(a_densities, function(a_wordList) {

			a_wordList.sort(function(a, b) {
				return b.density - a.density || b.times - a.times || b.word - a.word;
			});
		});

		return a_densities;
	},

	initComponent: function() {

		this.on('change', this.changeListener);

		this.callParent();

		return this;
	}
});
