﻿using System.Collections.Generic;
using System.Linq;
using Hqub.Autocompleate.Core.DataTypes;

namespace Hqub.Autocompleate.Core
{
    /// <summary>
    /// 
    /// </summary>
    public class Engine
    {
        #region Fields

        public const int MaxAutocompleateResultCount = 10;

        private readonly IndexTree _index;

        #endregion

        #region .ctor

        /// <summary>
        /// Makes engine object
        /// </summary>
        public Engine()
        {
            _index = new IndexTree();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Search all matches by word
        /// </summary>
        /// <param name="word"></param>
        public IList<Word> Compleate(string word)
        {
            var foundNode = _index.Get(word);
            if (foundNode == null)
                return new Word[] {};

            var words = foundNode.LinkedWords;
            return words.ToList();
        }

        #region Add a new word to index

        public void Add(IList<Word> words)
        {
            foreach (var word in words)
            {
                Add(word);
            }
        }

        public void Add(string word, int frequency)
        {
            Add(new Word(word, frequency));
        }

        public void Add(Word word)
        {
            _index.Add(word);
        }

        #endregion

        #endregion
    }
}
