using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarketData.MarketDataModel
{
  public class WordDictionary : Dictionary<String,Dictionary<String,DictionaryElement>>
  {
    public WordDictionary()
    {
    }
    public DictionaryCollection ToList()
    {
      DictionaryCollection dictionaryCollection=new DictionaryCollection();
      List<String> outerKeys=new List<String>(this.Keys);
      foreach(String outerKey in outerKeys)
      {
        Dictionary<String,DictionaryElement> innerDictionary=this[outerKey];
        DictionaryCollection items=new DictionaryCollection(innerDictionary.Values.ToList());
        dictionaryCollection.AddRange(items);
      }
      return dictionaryCollection;
    }
    public bool ContainsKeyAs(String word,String partOfSpeech)
    {
      if(!ContainsKey(word))return false;
      Dictionary<String,DictionaryElement> items=this[word];
      if(!items.ContainsKey(partOfSpeech))return false;
      return true;
    }
  }
  public class DictionaryElement
  {
    public DictionaryElement()
    {
    }
    public DictionaryElement(String word,String partOfSpeech)
    {
      Word=word;
      PartOfSpeech=partOfSpeech;
    }
    public String Word{get;set;}
    public String PartOfSpeech{get;set;}
  }
  public class DictionaryCollection : List<DictionaryElement>
  {
    public DictionaryCollection()
    {
    }
    public DictionaryCollection(List<DictionaryElement> dictionaryCollection)
    {
      foreach(DictionaryElement dictionaryElement in dictionaryCollection)Add(dictionaryElement);
    }
    public WordDictionary ToDictionary()
    {
      WordDictionary wordDictionary=new WordDictionary();
      foreach(DictionaryElement dictionaryElement in this)
      {
        if(!wordDictionary.ContainsKey(dictionaryElement.Word))
        {
          wordDictionary.Add(dictionaryElement.Word,new Dictionary<String,DictionaryElement>());
        }
        Dictionary<String,DictionaryElement> items=wordDictionary[dictionaryElement.Word];
        if(items.ContainsKey(dictionaryElement.PartOfSpeech))continue;
        items.Add(dictionaryElement.PartOfSpeech,dictionaryElement);
      }
      return wordDictionary;
    }
  }
}
