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

namespace KelimeEzber.General
{
	/// <summary>
	/// Summary description for GenelAyarlar.
	/// </summary>
	public class GenelAyarlar
	{
		private Int32	Hiz=0;
		private Int32	SesTekrarAdedi=0;
		private Int32	DefadaSil=0;
		private bool	Ses=true;
		private bool	SiraliKelimeGoster=true;
		private bool	SystemTraydaGoster=true;
		private int		SoruTuru=-1;
		private int		SecenekAdedi=10;
		private string	SonKelime=" ";


		private bool	ChangeBilgi=false;
		private System.IO.DirectoryInfo directoryInfo=null;

		private string XmlFile="";

		public Int32 iHiz {
			get { return this.Hiz;}
			set { 
				if (this.Hiz != Convert.ToInt32(value))
				{
					this.Hiz = Convert.ToInt32(value);
					ChangeBilgi=true;
				}
			}
		}

		public Int32 iSesTekrarAdedi {
			get { return this.SesTekrarAdedi;}
			set {
				if (this.SesTekrarAdedi != Convert.ToInt32(value))
				{
					this.SesTekrarAdedi = Convert.ToInt32(value);
					ChangeBilgi=true;
				}
			}
		}

		public Int32 iDefadaSil {
			get { return this.DefadaSil;}
			set 
			{
				if (this.DefadaSil != Convert.ToInt32(value))
				{
					this.DefadaSil = Convert.ToInt32(value);
					ChangeBilgi=true;
				}
			}
		}

		public Int32 iSoruTuru
		{
			get { return this.SoruTuru;}
			set 
			{
				if (this.SoruTuru != Convert.ToInt32(value))
				{
					this.SoruTuru = Convert.ToInt32(value);
					ChangeBilgi=true;
				}
			}
		}

		public Int32 iSecenekAdedi
		{
			get { return this.SecenekAdedi;}
			set 
			{
				if (this.SecenekAdedi != Convert.ToInt32(value))
				{
					this.SecenekAdedi = Convert.ToInt32(value);
					ChangeBilgi=true;
				}
			}
		}

		public bool bSes 
		{
			get { return this.Ses;}
			set {
				if (this.Ses != Convert.ToBoolean(value))
				{
					this.Ses = Convert.ToBoolean(value);
					ChangeBilgi=true;
				}
			}
		}

		public bool bSiraliKelimeGoster {
			get { return this.SiraliKelimeGoster;}
			set {
				if (this.SiraliKelimeGoster != Convert.ToBoolean(value))
				{
					this.SiraliKelimeGoster = Convert.ToBoolean(value);
					ChangeBilgi=true;
				}
			}
		}

		public bool bSystemTraydaGoster 
		{
			get { return this.SystemTraydaGoster;}
			set {
				if (this.SystemTraydaGoster != Convert.ToBoolean(value))
				{
					this.SystemTraydaGoster = Convert.ToBoolean(value);
					ChangeBilgi=true;
				}
			}
		}

		public string sSonKelime {
			get { return this.SonKelime;}
			set {
				if (this.SonKelime != value)
				{
					this.SonKelime = value;
					ChangeBilgi=true;
				}
			}
		}

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

			XmlFile = directoryInfo.FullName + "\\ayarlar.xml";

			//Set the output path
			if (!System.IO.File.Exists(XmlFile))
			{
				return;				
			}
			//load the xml file into the XmlTextReader object.

			XmlTextReader XmlRdr = new System.Xml.XmlTextReader(XmlFile);
			this.ChangeBilgi=true;
			//while moving through the xml document.
			while(XmlRdr.Read())
			{
				if (XmlRdr.NodeType==XmlNodeType.Element)
				{
					switch(XmlRdr.Name)       
					{         
						case "Hýz":
							XmlRdr.Read();
							this.Hiz = Convert.ToInt32(XmlRdr.Value);
							break;
						case "SesTekrarAdedi":   
							XmlRdr.Read();
							this.SesTekrarAdedi = Convert.ToInt32(XmlRdr.Value);
							break;
						case "DefadaSil":   
							XmlRdr.Read();
							this.DefadaSil = Convert.ToInt32(XmlRdr.Value);
							break;       
						case "SoruTuru":   
							XmlRdr.Read();
							this.SoruTuru = Convert.ToInt32(XmlRdr.Value);
							break;       
						case "SecenekAdedi":   
							XmlRdr.Read();
							this.SecenekAdedi = Convert.ToInt32(XmlRdr.Value);
							break;       
						case "SonKelime":   
							XmlRdr.Read();
							this.SonKelime = XmlRdr.Value;
							break;
						case "Ses":
							XmlRdr.Read();
							if (XmlRdr.Value == "1") this.Ses = true;
							else this.Ses = false;
							break;
						case "SystemTraydaGoster":
							XmlRdr.Read();
							if (XmlRdr.Value == "1") this.SystemTraydaGoster = true;
							else this.SystemTraydaGoster = false;
							break;
						case "SiraliKelimeGoster":
							XmlRdr.Read();
							if (XmlRdr.Value == "1") this.SiraliKelimeGoster = true;
							else this.SiraliKelimeGoster = false;
							break;
					}
				}
				this.ChangeBilgi=false;
			}
			XmlRdr.Close();
			XmlRdr = null;
		}

		public void Dispose()
		{
			if (this.ChangeBilgi==true) 
			{
				//Get the applications startup path
				#if (DEBUG)
					directoryInfo = System.IO.Directory.GetParent(Application.StartupPath);
					if (directoryInfo.Name.ToString() == "bin")
					{
						XmlFile = directoryInfo.FullName;
						directoryInfo = System.IO.Directory.GetParent(XmlFile);
					}
				#else
					directoryInfo = System.IO.Directory.GetParent(Application.ExecutablePath);
				#endif

				XmlFile = directoryInfo.FullName + "\\ayarlar.xml";

				//create the xml text writer object by providing the filename to write to
				//and the desired encoding.  If the encoding is left null, then the writer
				//assumes UTF-8.
				XmlTextWriter XmlWtr = new System.Xml.XmlTextWriter(XmlFile,null);
			
				//set the formatting option of the xml file. The default indentation is 2 character spaces.
				//To change the default, use the Indentation property to set the number of IndentChars to use
				//and use the IndentChar property to set the character to use for indentation, such as the 
				//tab character. Here the default is used.
				XmlWtr.Formatting=Formatting.Indented;

				//begin to write the xml document. This creates the xml declaration with the version attribute
				//set to "1.0".
				XmlWtr.WriteStartDocument();

				//start the first element.
				XmlWtr.WriteStartElement("Ayarlar");
			
				XmlWtr.WriteElementString("Hýz", this.Hiz.ToString());
				XmlWtr.WriteElementString("SesTekrarAdedi", this.SesTekrarAdedi.ToString());
				XmlWtr.WriteElementString("SoruTuru", this.SoruTuru.ToString());
				XmlWtr.WriteElementString("SecenekAdedi", this.SecenekAdedi.ToString());
				if (this.Ses==true)		
						XmlWtr.WriteElementString("Ses", "1");
				else	XmlWtr.WriteElementString("Ses", "0");
				if (this.SystemTraydaGoster==true)
						XmlWtr.WriteElementString("SystemTraydaGoster", "1");
				else	XmlWtr.WriteElementString("SystemTraydaGoster", "0");
				if (this.SiraliKelimeGoster==true)
					XmlWtr.WriteElementString("SiraliKelimeGoster", "1");
				else	XmlWtr.WriteElementString("SiraliKelimeGoster", "0");
				
				XmlWtr.WriteElementString("SonKelime", this.SonKelime.ToString());

				//start the first element.
				XmlWtr.WriteEndElement();

				//now end the document.
				XmlWtr.WriteEndDocument();
			
				//now flush the contents of the stream.
				XmlWtr.Flush();
			
				//close the text writerj and write the xml file.
				XmlWtr.Close();
				XmlWtr = null;
			}
		}

		~GenelAyarlar() { Dispose(); }
	}
}