using System;
using System.Collections.Generic;
using System.Text;

namespace PinyinModel
{
    internal class LexiconGraph : Graph
    {
        internal class EdgeData
        {
            public string phrase;
            public int id;
			public PinyinSequence pinyin_sequence;
        }

        public LexiconGraph(int vertex_count, PinyinSequence[] pinyin_sequences, Lexicon dictionary)
        {
            Console.Write("Making lexicon graph... ");
            this.vertex_count = vertex_count;
            InitializeVertex();

            foreach (PinyinSequence pinyin_sequence in pinyin_sequences)
            {
                List<string> phrases = dictionary.GetPhrases(pinyin_sequence.Pinyins);
                if (phrases != null)
                {
                    foreach (string phrase in phrases)
                    {
                        Edge e = AddEdge(vertex[pinyin_sequence.From], vertex[pinyin_sequence.To]);
                        e.data = new EdgeData();
						(e.data as EdgeData).pinyin_sequence = pinyin_sequence;
                        (e.data as EdgeData).phrase = phrase;
                        (e.data as EdgeData).id = this.edge_count;
                    }
                }
            }
            Console.WriteLine("Done.");
        }

        public override string ToString()
        {
            StringBuilder buffer = new StringBuilder();
            foreach (Edge e in this.edges)
            {
                Vertex vs = e.from;
                Vertex vt = e.to;

                buffer.AppendFormat("{0} {1} \"{2}\" \"{3}\"", 
                    vs.id, 
                    vt.id, 
                    (e.data as EdgeData).pinyin_sequence.ToString(), 
                    (e.data as EdgeData).phrase);
                buffer.AppendLine();

            }
            return buffer.ToString();
        }
    }
}
