using System;
using System.IO;
using System.Collections;

namespace MathSpeakWrite
{
	/// <summary>
	/// Summary description for RegTest.
	/// </summary>
	public class RegTest
	{
		public int totalSampsTested, totalSymsTested, totalWrong, totalRight;
		public String dirpath;
		public int[][] recMatrix; //X = what it really is, Y = what it's recognized as
		public int[] numSamples; //Indexed by 

		public RegTest(String dirpath)
		{
			this.dirpath=dirpath;
			this.totalSampsTested=0;
			this.totalSymsTested=0;
			this.totalWrong=0;
			this.totalRight = 0;
		}
		

		public void testFiles() 
		{
			int numfiles;
			String curdir = System.IO.Directory.GetCurrentDirectory();
			System.IO.Directory.SetCurrentDirectory(dirpath);
			String[] fileNames = System.IO.Directory.GetFiles(dirpath);
			numfiles = fileNames.Length;
			this.numSamples = new int[numfiles];
			
			Console.Out.WriteLine("Running Regression Test");

			for(int i=0; i < numfiles; i++)
			{
				String nameCode, numSamp;
				StreamReader sr;

				if(fileNames[i].EndsWith("FeatureMatrix"))
					continue;

				//Read in a file
				sr = new System.IO.StreamReader(fileNames[i]);
				sr.ReadLine(); //Read the line of junk
				numSamp = sr.ReadLine();
				this.numSamples[i] = System.Int32.Parse(numSamp);
				
//				Console.Out.WriteLine("DEBUG: " + "num Samples = " +this.numSamples[i]);

				nameCode = sr.ReadLine();
				sr.ReadLine();//Read the whitespace
				//Loop			
				for(int j=0; j < this.numSamples[i]; j++) 
				{
					char[] buf = new char[128];
					String bufVal;
					int[][] points = readMultiLine(sr);


					sr.ReadLine(); //Read White Space
					//sr.ReadLine(); //Read White Space

					CITHandwriting.LoadProfWithRec(points[0], points[1], points[0].Length);
					CITHandwriting.ProfNameOfChoice(0, buf);
					bufVal = new String(buf, 0, CITHandwriting.CStringLength(buf));

					if(!bufVal.Equals(nameCode))
					{
						Console.Out.WriteLine("WRONG RegTest: expected: " + nameCode + " got: " +bufVal);
						this.totalWrong++;
					}
					else
					{
						this.totalRight++;
					}
					this.totalSampsTested ++;
				}
				this.totalSymsTested ++;

				sr.Close();
			}
			Console.Out.WriteLine("Tested symbols: " + this.totalSymsTested + " samples: " + this.totalSampsTested
				+ " right : " + this.totalRight +" wrong: " + this.totalWrong);
			System.IO.Directory.SetCurrentDirectory(curdir);
		}		
		

		/// <summary>
		/// Reads in a multiline from the data files used by CIT. It returns an array with the 
		/// coordinates following form: [xs,ys][points]
		/// </summary>
		/// <param name="sr"></param>
		/// <returns></returns>
		public static int[][] readMultiLine(StreamReader sr)
		{
			int[][] returnme;
			ArrayList coords = new ArrayList();
			int numpoints, numcomponents, compsRead =0;
			String headerline = sr.ReadLine(); //Read the descriptor, get the numpoints
			
			//Parse the headerline, extract the points and components fields
			String[] headerComponents = headerline.Split(",=".ToCharArray());
			numpoints = System.Int32.Parse(headerComponents[3]);
			numcomponents = System.Int32.Parse(headerComponents[5]);			

			sr.ReadLine(); //REad the blank line

			for(int i=0; true; i++)
			{
				int[] presCoords = new int[2];
				int presx, presy;
				String presLine = sr.ReadLine();
				
				if(presLine.Length <3)
				{
					compsRead ++;
					presx=0;
					presy=0;

					if(compsRead == numcomponents)
						break;
				}
				else 
				{
					String[] coordComponents = presLine.Split(" (),".ToCharArray());
					String xComp = coordComponents[3];
					String yComp = coordComponents[5];
					presx = System.Int32.Parse(xComp);
					presy = System.Int32.Parse(yComp);
	//				Console.Out.WriteLine("DEBUG: Just read in x= " + presx + " y= " + presy);
				}
				presCoords[0] = presx;
				presCoords[1] = presy;
				coords.Add(presCoords);
			}
			
			returnme = new int[2][];
			returnme[1] = new int[coords.Count];
			returnme[0] = new int[coords.Count];
			for(int i=0; i < coords.Count; i++)
			{
				int [] presPoint = (int[]) coords[i];
				returnme[0][i] = presPoint[0];
				returnme[1][i] = presPoint[1];
			}

			return returnme;
		}
	}
}
