using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Configuration;

namespace KelimeEzber.General
{
	/// <summary>
	/// Summary description for DataManager.
	/// </summary>
	public class DataManager
	{
		public DataSet myDataSet = null;
		public DataRow myDataRow = null;
		private OleDbConnection myConnection = null;
		public string sQuery = "";
		public string sTableName = "";
		private string ConnectionString = null;
		public string sErrorMessage="";

		public DataManager() 
		{
			myConnection = null;
			#if (DEBUG)
				ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\" + System.Configuration.ConfigurationSettings.AppSettings["dbpath"];
			#else
				ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Configuration.ConfigurationSettings.AppSettings["dbpath"];
			#endif
			sQuery = "";
			myDataSet = null;
		}

		~DataManager() { Dispose(); }
		
		public void Dispose()
		{
			try
			{
				if (myDataSet != null) myDataSet.Dispose();
				if (myConnection != null) myConnection.Close();
			}
			catch ( Exception ex )
			{
				string message = ex.Message;
				sErrorMessage = "Hata: DATAMANAGER ' da Bellekten silmede hata. \n" + ex.Message;
			}
		}

		public void ErrorMessage(string sErrorMsg) 
		{
			if (sErrorMsg.Length>0) 
			{
				DialogResult dr = MessageBox.Show(sErrorMsg,"Hata Oluştu",
					MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		public bool OpenConn() 
		{
			try
			{
				//#if USINGPROJECTSYSTEM
				//			string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\BugTypes.MDB";
				//#else
				//				string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB";
				//#endif
				myConnection = new OleDbConnection(ConnectionString);
				myConnection.Open();
				return false;
			}
			catch ( Exception ex )
			{
				string message = ex.Message;
				ErrorMessage("Hata: Database bağlantısı kurulamadı. \n" + ex.Message);
				return true;
			}
		}

		public bool CloseConn() 
		{
			try
			{
				myConnection.Close();
				return false;
			}
			catch ( Exception ex )
			{
				string message = ex.Message;
				ErrorMessage("Hata: Database bağlantısı kesilemedi. \n" + ex.Message);
				return true;
			}
		}

		public bool DataCommand() 
		{	
			bool ConnOpen = false;
			try
			{
				if (sQuery==null ||sQuery=="") return false;
				if (myConnection == null) 
				{
					if (OpenConn()) 
					{
						return true;
					}
					ConnOpen = true;
				}
				OleDbDataAdapter myAdapter = new OleDbDataAdapter(sQuery,myConnection);
				myDataSet = new DataSet();
				myAdapter.Fill(myDataSet,sTableName);
				if (ConnOpen == true) 
				{
					if (CloseConn()) 
					{
						return false;
					}
				}
				return false;
			}
			catch ( Exception ex )
			{
				string message = ex.Message;
				ErrorMessage("Hata: " + sTableName + " Kayıt üzerindeki işlem gerçekleştirilemedi. \n" + ex.Message);
				return true;
			}
		}

		public DataTable ExecuteQuery() 
		{	
			bool ConnOpen = false;
			try
			{
				if (myConnection == null) 
				{
					if (OpenConn()) 
					{
						return null;
					}
					ConnOpen = true;
				}
				OleDbDataAdapter myAdapter = new OleDbDataAdapter(sQuery,myConnection);
				DataSet myDataSet = new DataSet();
				myAdapter.Fill(myDataSet, sTableName);
				if (ConnOpen == true) 
				{
					if (CloseConn()) 
					{
						return null;
					}
				}
				myDataRow = myDataSet.Tables[0].Rows[0];
				myDataSet.Tables[0].TableName = sTableName;
				return myDataSet.Tables[0];
			}
			catch ( Exception ex )
			{
				sErrorMessage = "Hata: " + sTableName + " Kayıtları getirilemiyor. \n" + ex.Message;
				return null;
			}
		}

				public bool Kaydet(Sozluk gelenSozluk)
				{
					sTableName = "sozluk";
					if (gelenSozluk.RecordId > 0)
					{
						sQuery = gelenSozluk.getUpdateQuery();
						if (DataCommand()) 
						{
							return true;
						}
					}
					else
					{
						sQuery = gelenSozluk.getInsertQuery();
						if (DataCommand()) 
						{
							return true;
						}
					}
					return false;
				}

				public bool Sil(Sozluk gelenSozluk)
				{
					sTableName = "sozluk";
					if (gelenSozluk.RecordId == 0)
					{
						ErrorMessage("Hata: " + sTableName + " Kaydı temsil eden anahtar yok. Silinemez.");
						return true;
					}
					else
					{
						sQuery = gelenSozluk.getDeleteQuery();
						if (DataCommand()) 
						{
							return true;
						}
					}
					return false;
				}

				public Sozluk FirstRecord(Sozluk gelenSozluk) 
				{
					sQuery = "Select top 1 * From SOZLUK Order By English";
					sTableName = "SOZLUK";
					if (ExecuteQuery() == null) 
					{
						return null;
					}
					return new Sozluk(myDataRow);
				}
				
				public Sozluk PrevRecord(Sozluk gelenSozluk) 
				{
					if (gelenSozluk == null) return null;
					sQuery = "Select top 1 * From SOZLUK where English < '" + gelenSozluk.English + "' Order By English desc";
					sTableName = "SOZLUK";
					if (ExecuteQuery() == null) 
					{
						return null;
					}
					return new Sozluk(myDataRow);
				}

				public Sozluk NextRecord(Sozluk gelenSozluk) 
				{
					if (gelenSozluk == null) return null;
					sQuery = "Select top 1 * From SOZLUK where English > '" + gelenSozluk.English + "' Order By English asc";
					sTableName = "SOZLUK";
					if (ExecuteQuery() == null) 
					{
						return null;
					}
					return new Sozluk(myDataRow);
				}

				public Sozluk LastRecord(Sozluk gelenSozluk)
				{
					sQuery = "Select top 1 * From SOZLUK Order By English Desc";
					sTableName = "SOZLUK";
					if (ExecuteQuery() == null) 
					{
						return null;
					}
					return new Sozluk(myDataRow);
				}
	}
}
