using System;
using System.Xml;
using System.Windows.Forms;

namespace KelimeEzber.General
{
	public class CrossWordXML
	{	 
		private string[] myCrossWordString;
		private string[] myColString;
		private string[] myRowString;
		private string xmlFileName="";
		private string[] xmlFileFullNames=null;
		private string[] xmlFiles=null;
		private string xmlFilePath="";

		public string sGetColBilgi="";
		public string sGetRowBilgi="";

		private System.IO.DirectoryInfo directoryInfo=null;

		public void getNewCrossWord()
		{
			for (int i=0;i<13;i++)
			{
				myCrossWordString[i] = "";
				myColString[i] = "";
				myRowString[i] = "";
			}
		}

		public string[] getFileNames()
		{
			return xmlFiles;
		}

		private void readXMLFile()
		{
			int iBilgi=0,iRow=0,iCol=0;
			XmlTextReader XmlRdr = new System.Xml.XmlTextReader(xmlFilePath + '\\' + xmlFileName);
			//while moving through the xml document.
			while(XmlRdr.Read())
			{
				if (XmlRdr.NodeType==XmlNodeType.Element)
				{
					switch(XmlRdr.Name)       
					{         
						case "Bilgi":
							XmlRdr.Read();
							myCrossWordString[iBilgi] = XmlRdr.Value.Replace("I","i");
							iBilgi++;
							break;
						case "Col":
							XmlRdr.Read();
							myColString[iCol] = XmlRdr.Value.Replace("I","i");
							iCol++;
							break;
						case "Row":
							XmlRdr.Read();
							myRowString[iRow] = XmlRdr.Value.Replace("I","i");
							iRow++;
							break;       
					}
				}
			}
			XmlRdr.Close();
			XmlRdr = null;
		}

		public string[] getNewAlistirma(string comingFileName)
		{
			xmlFileName = comingFileName;
			readXMLFile();
			return xmlFiles;
		}

		private void Dispose()
		{
		}

		public CrossWordXML()
		{
			int iDosyaSayisi=0;

			//Get the applications startup path
			#if (DEBUG)
				directoryInfo = System.IO.Directory.GetParent(Application.StartupPath);
				if (directoryInfo.Name.ToString() == "bin")
				{
					xmlFilePath = directoryInfo.FullName;
					directoryInfo = System.IO.Directory.GetParent(xmlFilePath);
				}
			#else
				directoryInfo = System.IO.Directory.GetParent(Application.ExecutablePath);
			#endif

			//Set the output path
			xmlFilePath = directoryInfo.FullName + 
						System.Configuration.ConfigurationSettings.AppSettings["crossWord"];
			myCrossWordString = new String[13];
			myColString = new String[13];
			myRowString = new String[13];
			xmlFileName="";
			getNewCrossWord();
			System.IO.DirectoryInfo objDir = new System.IO.DirectoryInfo(xmlFilePath);
			foreach (System.IO.FileInfo objFile in objDir.GetFiles("*.xml")) 
			{
				iDosyaSayisi++;
			}
			xmlFiles = new string[iDosyaSayisi];
			xmlFileFullNames = new string[iDosyaSayisi];
			iDosyaSayisi = 0;
			foreach (System.IO.FileInfo objFile in objDir.GetFiles("*.xml")) 
			{
				xmlFiles[iDosyaSayisi] = objFile.Name;
				xmlFileFullNames[iDosyaSayisi] = objFile.FullName;
				iDosyaSayisi++;
				if (xmlFileName=="") xmlFileName =xmlFileFullNames[0];
			}			
		}

		public string harfGetir(int iGelenRow, int iGelenCol)
		{
			sGetColBilgi="";
			sGetRowBilgi="";
			if (iGelenRow > 13 || iGelenCol > 13 || iGelenRow < 1 || 
				iGelenCol < 1)	return "";
			if (myCrossWordString[iGelenRow-1]!=null) 
			{
				if (myCrossWordString[iGelenRow - 1].Length < iGelenCol -1) return "";
				if (myCrossWordString[iGelenRow - 1].Substring(iGelenCol - 1) == "")
					return "";
				else 
				{
					sGetColBilgi=myColString[iGelenCol - 1];
					sGetRowBilgi=myRowString[iGelenRow - 1];
					if (myCrossWordString[iGelenRow - 1].Substring(iGelenCol - 1,1).ToLower() != " ")
						return myCrossWordString[iGelenRow - 1].Substring(iGelenCol - 1,1).ToLower();
					else return "";
				}
			}
			return "";
		}

		public string harfControl(int iGelenRow, int iGelenCol)
		{
			if (iGelenRow > 13 || iGelenCol > 13 || iGelenRow < 1 || 
				iGelenCol < 1)	return "";
			if (myCrossWordString[iGelenRow-1]!=null) 
			{
				if (myCrossWordString[iGelenRow - 1].Length < iGelenCol -1) return "";
				if (myCrossWordString[iGelenRow - 1].Substring(iGelenCol - 1) == "")
					return "";
				else 
				{
					if (myCrossWordString[iGelenRow - 1].Substring(iGelenCol - 1,1).ToLower() != " ")
						return myCrossWordString[iGelenRow - 1].Substring(iGelenCol - 1,1).ToLower();
					else return "";
				}
			}
			return "";
		}
	}
}
