using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace agree
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public partial class TypeMgr
	{
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///
		/// Regression test
		/// 
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		public void RegressionTest(TextWriter tw, String file)
		{
			if (!g.f_expanded_all)
				throw new Exception();
#if false
			// list of edges with out-mark == 0
			foreach (var zzz in tm.feat_dict.Where(kvp => kvp.Value.ContainsKey(0)).Select(kvp => new { kvp.Key, x = kvp.Value[0] }))
			{
				Debug.WriteLine("{0} {1}", zzz.Key, zzz.x);
			}
#endif

			StringBuilder sb = new StringBuilder();
			foreach (agree.Type t in AllTypes)
			{
				sb.Append(t.Definition.ToPathList("definition: " + t.Name));
			}
			foreach (agree.Type t in AllTypes)
			{
				if (t.m_flags.HasFlag(agree.Type.Flags.Expanded) && !t.Expanded.Equals(t.Definition))
					sb.Append(t.Expanded.ToPathList("expanded: " + t.Name));
			}

			String f_gold = String.Format("regression-trees-{0}.txt", file);
			String f_this = String.Format("regression-trees-latest-{0}.txt", file);

			String s = sb.ToString();
			if (!File.Exists(f_gold))
			{
				File.WriteAllText(f_gold, s);
				tw.WriteLine("Wrote new regression trees");
			}
			else
			{
				File.WriteAllText(f_this, s);
				if (File.ReadAllText(f_gold) != s)
				{
					tw.WriteLine("Regression test failed.");
					throw new RegressionTestFailedException("Regression test failed");
				}
				else
					tw.WriteLine("Regression test succeeded.");
			}
		}

		public class RegressionTestFailedException : Exception
		{
			public RegressionTestFailedException(String msg)
				: base(msg)
			{
			}
		};
	}
}