using System;
using System.Data;

namespace KelimeEzber.General
{
	/// <summary>
	/// Summary description for Structures.
	/// </summary>
	public class Structures
	{
		public Structures()
		{
			//
			// TODO: Add constructor logic here
			//
		}
	}
	public class Sozluk
	{
		// Public Attributes
		public Int32	RecordId;
		public String	English = "";
		public String	Turkce = "";
		public String	Okunusu = "";
		public String	Definition = "";
		public String	Example = "";
		public Int32	KacDefaBilindi=0;
		public Int32	KacDefaBilinemedi=0;

		// Default constructor
		public Sozluk()
		{
			this.KacDefaBilindi = 0;
			this.KacDefaBilinemedi = 0;
		}

		// Constructor
		public Sozluk( DataRow dr )
		{
			if ( dr["RecordID"] != DBNull.Value )
			{
				this.RecordId = Int16.Parse(dr["RecordID"].ToString());
			}

			if ( dr["English"] != DBNull.Value )
			{
				this.English = dr["English"].ToString().Trim();
			}

			if ( dr["Turkce"] != DBNull.Value )
			{
				this.Turkce = dr["Turkce"].ToString().Trim();
			}

			if ( dr["Okunusu"] != DBNull.Value )
			{
				this.Okunusu = dr["Okunusu"].ToString().Trim();
			}

			if ( dr["Definition"] != DBNull.Value )
			{
				this.Definition = dr["Definition"].ToString().Trim();
			}

			if ( dr["Example"] != DBNull.Value )
			{
				this.Example = dr["Example"].ToString().Trim();
			}

			if ( dr["KacDefaBilindi"] != DBNull.Value )
			{
				this.KacDefaBilindi = Int32.Parse(dr["KacDefaBilindi"].ToString());
			}

			if ( dr["KacDefaBilinemedi"] != DBNull.Value )
			{
				this.KacDefaBilinemedi = Int32.Parse(dr["KacDefaBilinemedi"].ToString());
			}

		}
		
		private void yanlisliklariEngelle() 
		{
			if (this.English.Length > 0) 
			{
				this.English = this.English.Replace("'","''");
			}
			if (this.Turkce.Length > 0) 
			{
				this.Turkce = this.Turkce.Replace("'","''");
			}
			if (this.Okunusu.Length > 0) 
			{
				this.Okunusu = this.Okunusu.Replace("'","''");
			}
			if (this.Definition.Length > 0) 
			{
				this.Definition = this.Definition.Replace("'","''");
			}
			if (this.Example.Length > 0) 
			{
				this.Example = this.Example.Replace("'","''");
			}
			English = English.Trim();
			Turkce = Turkce.Trim();
			Okunusu = Okunusu.Trim();
			Definition = Definition.Trim();
			Example = Example.Trim();
		}
		
		public string getUpdateQuery()
		{
			if (this.RecordId == 0)
			{
				return null;
			}
			yanlisliklariEngelle();

			string sQuery = "Update Sozluk Set English = '" + this.English + "'," +
				"Turkce = '" + this.Turkce + "'," +
				"Okunusu = '" + this.Okunusu + "'," +
				"Definition = '" + this.Definition + "'," +
				"Example = '" + this.Example + "'";
			if (this.KacDefaBilindi > 0) 
			{
				sQuery += ", KacDefaBilindi = " + this.KacDefaBilindi;
			}
			if (this.KacDefaBilinemedi > 0) 
			{
				sQuery += ", KacDefaBilinemedi = " + this.KacDefaBilinemedi;
			}
			sQuery += " Where RecordID = " + this.RecordId; 

			return sQuery;
		} 

		public string getInsertQuery()
		{
			yanlisliklariEngelle();

			string sQuery = "Insert Into Sozluk(English, Turkce, Okunusu, Definition, Example, KacDefaBilindi, KacDefaBilinemedi) " + 
								"Values('" + this.English + "','" +
											 this.Turkce + "','" +
											 this.Okunusu + "','" +
											 this.Definition + "','" +
											 this.Example + "'," +
											 this.KacDefaBilindi.ToString() + "," +
											 this.KacDefaBilinemedi.ToString() + " )";
			
			return sQuery;
		} 

		public string getDeleteQuery()
		{
			if (this.RecordId == 0)
			{
				return null;
			}

			string sQuery = "Delete From Sozluk where RecordID = " + this.RecordId;
			
			return sQuery;
		} 
	}
}