using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

using glue.Debugging;

using agree;

namespace WpfClient
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public partial class MainWindow : Window
	{
		public Grammar g;
		public String grammar_file;

		public SysObj so;

		public MainWindow(String grammar_file)
		{
			this.grammar_file = grammar_file;
			this.FontSize = 13;
			this.Title = "aɢʀee :: another ɢʀammar enɢineerinɢ environment";
			this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///
		///
		///
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		void MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
			so = SysObj.Instance;

			String base_name = System.IO.Path.GetFileNameWithoutExtension(grammar_file);

			g = new SysCommands.CmdTokLoadGrammar(so, base_name, grammar_file).Task.Result;

			g.Parser.SetDebugOutput(new DebugTextWriter());

			g.tm.RegressionTest(new DebugTextWriter(),base_name);

			//Debug.WriteLine(g.tm.GarbageReport());
			//ConstraintPool.StartGarbageTracking();

			//Debug.WriteLine(g.tm.GarbageReport());

			//DumpMemoryStats();
			//ShowTestTfses();
			//ShowAllTfses();


			String[] sent = { "สนิท", "อยู่", "บ้าน", "ที่" };


			IEnumerable<TfsEdge> ie_edge;

#if DEBUG
			ParseChart pc = null;
#endif
			//pc = g.Parser.Parse(sent).Result;
			//ie_edge = pc.SpanningDerivedEdges().Select(ce => ce.Contents);

			TfsEdge e1 = g.tm.type_dict["head-adj-int"].Expanded;
			TfsEdge e2 = g.tm.type_dict["ที่_4484"].Expanded;
			TfsEdge e3 = default(TfsEdge);// new UnifyHelper(g.tm).UnifyRulePart(e1, e1.RuleDaughters.Skip(1).First(), e2);
			ie_edge = new TfsEdge[] { e1, e2, e3 };

			this.Content = new MainScroll(g, ie_edge, Orientation.Horizontal);

			Debug.WriteLine("memory used: {0:#,#}", GC.GetTotalMemory(true));
			Debug.WriteLine("expanding all types took: {0} ms", g.tm.ms_expand);

#if DEBUG
			if (pc != null)
			{
				HashSet<PoolMark> reachable = g.Reachable();
				reachable.UnionWith(pc.AllEdges().SelectMany(_e => _e.Self.PoolMarksBelow));
				Debug.WriteLine(g.GarbageReport(reachable));
			}
#endif
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///
		/// Display some typed feature structures
		///
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		void ShowTestTfses()
		{
			List<String> expanded_to_show = new List<String>
				{
	//				"omni-conj-first-left-coord-rule",

//					"expl-arg-only-lex-item",
"emb-clause-verb-lex",
//"non-canonical",

//					"head-mod-phrase",
//					"basic-head-mod-phrase-simple",
//					"head-adj-phrase",
//					"head-adj-scop-phrase",
//					"head-adj-scop",
	//				"base-number-lex",
		//			"extracted-adj-phrase",
//					"s1-top-coord-rule",
				//	"ไม่ใช่_25318",
				};
			List<String> defs_to_show = new List<String>
				{
					"locative-verb-lex",
					"ไม่ใช่_25318",
					"extracted-adj-phrase",
					"head-adj-phrase",
					"headed-phrase",
					"basic-head-spec-phrase",
					"basic-binary-phrase",
					"รถ_4785",
				};

			List<TfsEdge> q = new List<TfsEdge>();
			q.AddRange(expanded_to_show.Select(s => g.tm.type_dict[s].Expanded));
			q.AddRange(defs_to_show.Select(s => g.tm.type_dict[s].Definition));

			//g.ExpandTypeDefinitions();
			q.Insert(0, g.tm.type_dict["conj-first-bottom-coord-rule"].Expanded);

			this.Content = new MainScroll(g, q, Orientation.Horizontal);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///
		/// Display some typed feature structures
		///
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		void ShowAllTfses()
		{
			List<TfsEdge> q = new List<TfsEdge>();
			q.AddRange(g.tm.AllTypes.Select(t => t.Definition));
			this.Content = new MainScroll(g, q, Orientation.Vertical);
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///
		///
		///
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		void DumpMemoryStats()
		{
			Debug.WriteLine("memory used: {0:#,#}", GC.GetTotalMemory(true));
			Debug.WriteLine("constraint pool sizes:");
			Debug.WriteLine("\taverage: {0}", g.loadtray.Pools.Average(cp => cp.Count));
			Debug.WriteLine("\tmin: {0}", g.loadtray.Pools.Min(cp => cp.Count));
			Debug.WriteLine("\tmax: {0}", g.loadtray.Pools.Max(cp => cp.Count));
		}
	};
}