//Add's a new Entry add the end of the file
    public static void  write(DictEntrySingleWord dicEntry)
    {
        createPathIfNotExists();
        StreamWriter writer = new StreamWriter(path, true);

        if (dicEntry is DictEntrySingleWord)
        {
            DictEntrySingleWord entry = ((DictEntrySingleWord)dicEntry);
            writer.WriteLine(entry.getWord() + "," + entry.getRate());
        }
        writer.Close();
    }
    //Write's a complete new File
    public static void  write(DictEntry dicEntry)
    {
        createPathIfNotExists();
        StreamWriter writer = new StreamWriter(path, false);

        if (dicEntry is DictEntrySingleWord)
        {
            DictEntrySingleWord entry = ((DictEntrySingleWord)dicEntry);
            writer.WriteLine(entry.getWord() + "," + entry.getRate());
        }
        else
        {
            List <DictEntrySingleWord> list = ((DictEntryMultyWord)dicEntry).getAllSubWords();
            foreach (DictEntrySingleWord entry in list)
            {
                writer.WriteLine(entry.getWord() + "," + entry.getRate());
            }
        }
        writer.Close();
    }
    //called by the keyboard to add eventually new words to the dictionary
    public void enteredText(string text)
    {
        string[] words = this.getWordsFromInput(text);
        if (words != null)
        {
            for (int i = 0; i < words.Length; i++)
            {
                DictEntrySingleWord entry = autoCompleteDic.insert(words [i]);
                if (entry != null)
                {
                    FileHandlerDictEntry.write(entry);
                }
            }
        }
        //TestDictionary t = new TestDictionary ();

        /*List<DictEntrySingleWord> stringlist = autoCompleteDic.getSortedLikelyWordsAfterRate("");
         * foreach (DictEntrySingleWord s in stringlist)
         *      Debug.Log( "Words: " + s.getWord() );
         */
    }
 public override DictEntrySingleWord insert(string word, int rate = 0, int level = 0)
 {
     //Debug.Log ("Test" + level);
     if (word.Length > 0)
     {
         char currentLetter = char.ToLower(word[level]);
         if (this.entries.ContainsKey(currentLetter))
         {
             //Debug.Log ("TestMW: "+ currentLetter +" level: "+level);
             //Debug.Log ("TestMW: " + word);
             return(this.entries[currentLetter].insert(word, rate, level + 1));
         }
         else
         {
             //Debug.Log ("TestSW"+ currentLetter +" level: "+level);
             //Debug.Log ("TestSW-Wort:" + word);
             DictEntrySingleWord single = new DictEntrySingleWord(word, rate, this);
             this.entries.Add(currentLetter, single);
             return(single);
         }
     }
     return(null);
 }
    /*
     * This method is used, if a new word is inserted and found a single word, e.g. first insert: "Anna", secondly insert: "Annies"
     * The internal has to change from one DictEntrySingleWord to One MultyLineWord and two SingleLineWords
     */
    public override DictEntrySingleWord insert(string newWord, int newRate, string oldWord, int oldRate, int level)
    {
        /* Example:
         * Inserted first: "anna" -> MultylineWord with entry a => anna
         * Inserted secondly: "annanas" -> found entry anna, deleted it, insert new MultylineWord-Entries until both words are equal + one,
         * then insert for both a SingleLineWord-entry
         * at the end return the new SinglelineWord-Entry
         */
        //Debug.Log ("Level: " + level);
        //Debug.Log ("Length oldWord:" + oldWord.Length);
        //Debug.Log ("Length newWOrld:" + newWord.Length);


        if (level < newWord.Length & level < oldWord.Length)
        {
            //Debug.Log ("InsertChange: " + "NewWorld - " + newWord + "=>" + newWord [level] + "........." + "OldWorld - " + oldWord + "=>" + oldWord [level]);
            //If both current letters (newWord and oldWord) are equal, add a new DictEntryMultiWord
            if (char.ToLower(newWord [level]).CompareTo(char.ToLower(oldWord [level])) == 0)
            {
                char currentLetter = char.ToLower(oldWord [level]);
                this.entries.Add(currentLetter, new DictEntryMultyWord());
                return(this.entries [currentLetter].insert(newWord, newRate, oldWord, oldRate, level + 1));
            }
            else
            {
                //if both current letters are not equal add for each a new DictEntrySingleWord and return the new one
                this.entries.Add(char.ToLower(oldWord [level]), new DictEntrySingleWord(oldWord, oldRate, this));
                DictEntrySingleWord newSingleEntry = new DictEntrySingleWord(newWord, newRate, this);
                this.entries.Add(char.ToLower(newWord [level]), newSingleEntry);
                return(newSingleEntry);
            }

            /*If the new word is bigger than the old world,
             * add DictEntrySingleWord for the oldWord one level back
             * and add a new DictEntrySingleWord for the new Word at the same level
             */
        }
        else if (level >= oldWord.Length)
        {
            this.entries.Add(char.ToLower(oldWord [level - 1]), new DictEntrySingleWord(oldWord, oldRate, this));
            //Debug.Log ("Test1");
            if (level < newWord.Length)
            {
                //Debug.Log ("Test2");
                DictEntrySingleWord newSingleEntry = new DictEntrySingleWord(newWord, newRate, this);
                this.entries.Add(char.ToLower(newWord [level]), newSingleEntry);
                return(newSingleEntry);
            }
        }
        else if (level >= newWord.Length)
        {
            /*If the old word is bigger than the new world,
             * add DictEntrySingleWord for the new Word one level back
             * and add a new DictEntrySingleWord for the old Word at the same level
             */
            //Debug.Log ("Test3");
            if (level < oldWord.Length)
            {
                //Debug.Log ("Test4");
                this.entries.Add(char.ToLower(oldWord [level]), new DictEntrySingleWord(oldWord, oldRate, this));
            }
            DictEntrySingleWord newSingleEntry = new DictEntrySingleWord(newWord, newRate, this);
            this.entries.Add(char.ToLower(newWord [level - 1]), newSingleEntry);
            return(newSingleEntry);
        }
        return(null);
    }