using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace KelimeEzber
{
	/// <summary>
	/// Summary description for Form_HizliDataGiris.
	/// </summary>
	public class Form_HizliDataGiris : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader_English;
		private System.Windows.Forms.ColumnHeader columnHeader_Turkce;
		private System.Windows.Forms.ColumnHeader columnHeader_Okunus;
		private System.Windows.Forms.ColumnHeader columnHeader_Definition;
		private System.Windows.Forms.ColumnHeader columnHeader_Example;
		private General.DataManager myDm = null;
		private DataTable tableSozluk=null;
		private System.Windows.Forms.Button button_sil;
		private System.Windows.Forms.Button button_kaydet;
		protected System.Windows.Forms.ColumnHeader columnHeader_RecordID;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form_HizliDataGiris()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/*private class tempTable 
		{
			public DataTable dTable=null;
			private DataRow currentRow;			
			DataColumn sRecordID = new DataColumn("RecorID");
			DataColumn sIngilizce = new DataColumn("English");
			DataColumn sTurkce = new DataColumn("Turkce");
			DataColumn sOkunusu = new DataColumn("Okunusu");
			DataColumn sDefinition = new DataColumn("Definition");
			DataColumn sExamples = new DataColumn("Example");
			DataColumn sBilindi = new DataColumn("KereBilindi");
			DataColumn sBilinemedi = new DataColumn("KereBilinemedi");
			

			public tempTable()
			{
				dTable = new DataTable("TempSozluk");
				sRecordID.ColumnName = "Kayıt No";
				sIngilizce.ColumnName = "İngilizce Kelime";
				sTurkce.ColumnName = "Türkçe Karşılık";
				sOkunusu.ColumnName = "Türkçe Okunuşu";
				sDefinition.ColumnName = "Definition";
				sExamples.ColumnName = "Examples";
				sBilindi.ColumnName = "Kere Bilindi";
				sBilinemedi.ColumnName = "Kere Bilinemedi";
				
				sRecordID.ReadOnly = true;

				dTable.Columns.Add(sRecordID);
				dTable.Columns.Add(sIngilizce);
				dTable.Columns.Add(sTurkce);
				dTable.Columns.Add(sOkunusu);
				dTable.Columns.Add(sDefinition);
				dTable.Columns.Add(sExamples);
				dTable.Columns.Add(sBilindi);
				dTable.Columns.Add(sBilinemedi);

			}

			public bool setAllRows(DataTable gelenTable) 
			{
				if (gelenTable == null) return true;
				try
				{
					sRecordID.ReadOnly = false;

					for  (int i=0;i!=gelenTable.Rows.Count;i++)
					{
						currentRow = dTable.NewRow();
						dTable.Rows.Add(currentRow);
						dTable.Rows[i][sRecordID.Caption] = gelenTable.Rows[i]["RecordID"];
						dTable.Rows[i][sIngilizce.Caption] = gelenTable.Rows[i]["English"];
						dTable.Rows[i][sTurkce.Caption] = gelenTable.Rows[i]["Turkce"];
						dTable.Rows[i][sOkunusu.Caption] = gelenTable.Rows[i]["Okunusu"];
						dTable.Rows[i][sDefinition.Caption] = gelenTable.Rows[i]["Definition"];
						dTable.Rows[i][sExamples.Caption] = gelenTable.Rows[i]["Example"];
						dTable.Rows[i][sBilindi.Caption] = gelenTable.Rows[i]["KacDefaBilindi"];
						dTable.Rows[i][sBilinemedi.Caption] = gelenTable.Rows[i]["KacDefaBilinemedi"];
					}

					sRecordID.ReadOnly = true;
				}
				catch (Exception emsj)
				{
					MessageBox.Show(emsj.Message);
					return true;
				}
				return false;
			}

		}*/

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form_HizliDataGiris));
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader_RecordID = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_English = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Turkce = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Okunus = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Definition = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Example = new System.Windows.Forms.ColumnHeader();
			this.button_sil = new System.Windows.Forms.Button();
			this.button_kaydet = new System.Windows.Forms.Button();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// listView1
			// 
			this.listView1.AllowColumnReorder = true;
			this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader_RecordID,
																						this.columnHeader_English,
																						this.columnHeader_Turkce,
																						this.columnHeader_Okunus,
																						this.columnHeader_Definition,
																						this.columnHeader_Example});
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.LabelEdit = true;
			this.listView1.Location = new System.Drawing.Point(0, 0);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(688, 80);
			this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listView1.TabIndex = 0;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader_RecordID
			// 
			this.columnHeader_RecordID.Text = "RecordID";
			// 
			// columnHeader_English
			// 
			this.columnHeader_English.Text = "İngilizce";
			this.columnHeader_English.Width = 81;
			// 
			// columnHeader_Turkce
			// 
			this.columnHeader_Turkce.Text = "Türkçe";
			this.columnHeader_Turkce.Width = 82;
			// 
			// columnHeader_Okunus
			// 
			this.columnHeader_Okunus.Text = "Türkçe Okunusu";
			this.columnHeader_Okunus.Width = 93;
			// 
			// columnHeader_Definition
			// 
			this.columnHeader_Definition.Text = "Definition";
			this.columnHeader_Definition.Width = 108;
			// 
			// columnHeader_Example
			// 
			this.columnHeader_Example.Text = "Example";
			this.columnHeader_Example.Width = 254;
			// 
			// button_sil
			// 
			this.button_sil.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.button_sil.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.button_sil.Location = new System.Drawing.Point(432, 178);
			this.button_sil.Name = "button_sil";
			this.button_sil.TabIndex = 8;
			this.button_sil.Text = "&Sil";
			this.button_sil.Click += new System.EventHandler(this.button_sil_Click);
			// 
			// button_kaydet
			// 
			this.button_kaydet.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.button_kaydet.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.button_kaydet.Location = new System.Drawing.Point(224, 178);
			this.button_kaydet.Name = "button_kaydet";
			this.button_kaydet.TabIndex = 7;
			this.button_kaydet.Text = "&Kaydet";
			this.button_kaydet.Click += new System.EventHandler(this.button_kaydet_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.dataGrid1.CaptionText = "Sözlük Kayıtları";
			this.dataGrid1.ContextMenu = this.contextMenu1;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(688, 168);
			this.dataGrid1.TabIndex = 9;
			this.dataGrid1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dataGrid1_KeyPress);
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItem3,
																						 this.menuItem1,
																						 this.menuItem2});
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "Kopyala";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.Text = "Yapıştır";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 2;
			this.menuItem2.Text = "Excel\' e Aktar";
			this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
			// 
			// Form_HizliDataGiris
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(688, 205);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.button_sil);
			this.Controls.Add(this.button_kaydet);
			this.Controls.Add(this.listView1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.KeyPreview = true;
			this.Name = "Form_HizliDataGiris";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.Text = "Hızlı Data Giriş";
			this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form_HizliDataGiris_KeyPress);
			this.Load += new System.EventHandler(this.Form_HizliDataGiris_Load);
			this.Closed += new System.EventHandler(this.Form_HizliDataGiris_Closed);
			this.Activated += new System.EventHandler(this.Form_HizliDataGiris_Activated);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Form_HizliDataGiris_Load(object sender, System.EventArgs e)
		{
			myDm = new General.DataManager();
		}

		private void button_kaydet_Click(object sender, System.EventArgs e)
		{
			string sHataBilgisi = "";
			DataRow currentRow;
			DataTable changeTbl = tableSozluk.GetChanges();
			if (changeTbl == null) 
			{
				MessageBox.Show("Kayıtlarda değişiklik yapılmamış.","Bilgi Mesajı");
				return;
			}
			try
			{
				myDm.OpenConn();
				sHataBilgisi = "";
				for  (int i=0;i!=changeTbl.Rows.Count;i++)
				{
					currentRow = changeTbl.Rows[i];
					General.Sozluk currentSozluk = new General.Sozluk(changeTbl.Rows[i]);
					if (currentSozluk.RecordId == 0) 
					{
						myDm.sQuery=currentSozluk.getInsertQuery();
						myDm.sTableName="SOZLUK";
						if (myDm.DataCommand() == true) 
						{
							sHataBilgisi += myDm.sErrorMessage + "\n";
						}
					} 
					else 
					{
						myDm.sQuery=currentSozluk.getUpdateQuery();
						myDm.sTableName="SOZLUK";
						if (myDm.DataCommand() == true) 
						{
							sHataBilgisi += myDm.sErrorMessage + "\n";
						}
					}
					currentSozluk = null;
				}
				myDm.CloseConn();
				if (sHataBilgisi.Length>0) MessageBox.Show(myDm.sErrorMessage,"Bilgi Mesajı");
				DisplayAllRecords();
			}
			catch (Exception emsj)
			{
				MessageBox.Show(emsj.Message,"Bilgi Mesajı");
				return;
			}
			MessageBox.Show("Kayıt işlemi yapıldı.","Bilgi Mesajı");
			return;
		}

		private void Form_HizliDataGiris_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (e.KeyChar == 27) 
			{
				this.Close();
			}
		}

		private bool DisplayAllRecords() 
		{
			if (myDm == null) return false;
			int iRow = this.dataGrid1.CurrentRowIndex;
			this.dataGrid1.DataSource = tableSozluk;
			myDm.OpenConn();
			myDm.sQuery = "Select * From SOZLUK Order By English";
			myDm.sTableName="SOZLUK";

			tableSozluk = myDm.ExecuteQuery();
			if (tableSozluk!=null) 
			{
				tableSozluk.Columns["RecordID"].ReadOnly = true;
				this.dataGrid1.DataSource = tableSozluk;
				myDm.CloseConn();
				if (iRow > 0) this.dataGrid1.CurrentRowIndex = iRow ;
			} else {
				MessageBox.Show("Hiç bir kayıt yokken bu FORM KULLANILAMAZ.","Bilgi Mesajı");
			}
			//MessageBox.Show(myDm.TableToString(tableSozluk));
			return false;
		}

		private void Form_HizliDataGiris_Activated(object sender, System.EventArgs e)
		{
			DisplayAllRecords();
		}

		private void Form_HizliDataGiris_Closed(object sender, System.EventArgs e)
		{
			myDm.Dispose();
			myDm = null;
		}

		private void button_sil_Click(object sender, System.EventArgs e)
		{
			try
			{
				int iRow = this.dataGrid1.CurrentRowIndex;
				if (iRow >= 0) 
				{
					General.Sozluk currentSozluk = new General.Sozluk(tableSozluk.Rows[iRow]);
					tableSozluk.Rows[iRow].Delete();
					myDm.sQuery = currentSozluk.getDeleteQuery();
					myDm.sTableName = "SOZLUK";
					if (myDm.DataCommand() == true) 
					{
						MessageBox.Show(myDm.sErrorMessage,"Hata Mesajı");
					}
				}
			}
			catch (Exception emsj)
			{
				MessageBox.Show(emsj.Message,"Bilgi Mesajı");
				return;
			}
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			General.DataGridFuncs myDtGridFuncs = new General.DataGridFuncs();
			myDtGridFuncs.Yapistir(	this.dataGrid1,
									this.dataGrid1.CurrentCell.RowNumber,
									this.dataGrid1.CurrentCell.ColumnNumber);
			myDtGridFuncs.Dispose();
			this.dataGrid1.Refresh();
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			General.DataGridFuncs myDtGridFuncs = new General.DataGridFuncs();
			myDtGridFuncs.DataGridExportToExcel(this.dataGrid1);
			myDtGridFuncs.Dispose();
			//MessageBox.Show(myDm.TableToString(tableSozluk));
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
this.dataGrid1.Focus();
SendKeys.Send ("^{C}");
		}

		private void dataGrid1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (e.KeyChar == 27) 
			{
				DisplayAllRecords();
				//dataGrid1[2][2]
			}
		}

	}
}
