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_KelimeBulma.
	/// </summary>
	public class Form_KelimeBulma : System.Windows.Forms.Form
	{
		private System.Windows.Forms.CheckedListBox checkedListBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.PictureBox pictureBox_Yanlis;
		private System.Windows.Forms.PictureBox pictureBox_Dogru;
		private System.Windows.Forms.Label label_Sonuc;
		private System.Windows.Forms.Label label_Kelime;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;

		private General.Sozluk [] mySozlukRecord = null;
		private General.Sozluk mySorulanSozlukRecord = null;
		private General.DataManager myDm = new KelimeEzber.General.DataManager();
		private System.Windows.Forms.Label label_SecenekAdet;
		private System.Windows.Forms.NumericUpDown numericUpDown1;
		private int iKelimeTur = 0;
		private bool bSoruCevaplandi = false;
		private System.Windows.Forms.Label label_Cevap;
		private General.Mp3Play myMp3Play=null;
		private System.Windows.Forms.CheckBox checkBox_Sesli;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

		/// <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_KelimeBulma));
			this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.label_Kelime = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox_Yanlis = new System.Windows.Forms.PictureBox();
			this.pictureBox_Dogru = new System.Windows.Forms.PictureBox();
			this.label_Sonuc = new System.Windows.Forms.Label();
			this.label_SecenekAdet = new System.Windows.Forms.Label();
			this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
			this.label_Cevap = new System.Windows.Forms.Label();
			this.checkBox_Sesli = new System.Windows.Forms.CheckBox();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
			this.SuspendLayout();
			// 
			// checkedListBox1
			// 
			this.checkedListBox1.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.checkedListBox1.CheckOnClick = true;
			this.checkedListBox1.ContextMenu = this.contextMenu1;
			this.checkedListBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.checkedListBox1.Location = new System.Drawing.Point(0, 80);
			this.checkedListBox1.Name = "checkedListBox1";
			this.checkedListBox1.Size = new System.Drawing.Size(408, 276);
			this.checkedListBox1.TabIndex = 0;
			this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItem1,
																						 this.menuItem2});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.Text = "Sadece &Türkçe Kelime Sor";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.Text = "Sadece &Ingilizce Kelime Sor";
			this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
			// 
			// label_Kelime
			// 
			this.label_Kelime.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.label_Kelime.BackColor = System.Drawing.Color.Black;
			this.label_Kelime.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.label_Kelime.ForeColor = System.Drawing.Color.Lime;
			this.label_Kelime.Location = new System.Drawing.Point(0, 0);
			this.label_Kelime.Name = "label_Kelime";
			this.label_Kelime.Size = new System.Drawing.Size(408, 80);
			this.label_Kelime.TabIndex = 1;
			this.label_Kelime.Text = "label1";
			this.label_Kelime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// button1
			// 
			this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
			this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.button1.Location = new System.Drawing.Point(319, 403);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 32);
			this.button1.TabIndex = 2;
			this.button1.Text = "&Sonraki";
			this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox_Yanlis
			// 
			this.pictureBox_Yanlis.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.pictureBox_Yanlis.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox_Yanlis.Image")));
			this.pictureBox_Yanlis.Location = new System.Drawing.Point(1, 357);
			this.pictureBox_Yanlis.Name = "pictureBox_Yanlis";
			this.pictureBox_Yanlis.Size = new System.Drawing.Size(96, 88);
			this.pictureBox_Yanlis.TabIndex = 3;
			this.pictureBox_Yanlis.TabStop = false;
			// 
			// pictureBox_Dogru
			// 
			this.pictureBox_Dogru.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.pictureBox_Dogru.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox_Dogru.Image")));
			this.pictureBox_Dogru.Location = new System.Drawing.Point(3, 358);
			this.pictureBox_Dogru.Name = "pictureBox_Dogru";
			this.pictureBox_Dogru.Size = new System.Drawing.Size(104, 88);
			this.pictureBox_Dogru.TabIndex = 4;
			this.pictureBox_Dogru.TabStop = false;
			// 
			// label_Sonuc
			// 
			this.label_Sonuc.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			this.label_Sonuc.Location = new System.Drawing.Point(118, 368);
			this.label_Sonuc.Name = "label_Sonuc";
			this.label_Sonuc.Size = new System.Drawing.Size(120, 48);
			this.label_Sonuc.TabIndex = 5;
			this.label_Sonuc.Text = "Sonuç";
			// 
			// label_SecenekAdet
			// 
			this.label_SecenekAdet.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.label_SecenekAdet.Location = new System.Drawing.Point(248, 360);
			this.label_SecenekAdet.Name = "label_SecenekAdet";
			this.label_SecenekAdet.Size = new System.Drawing.Size(100, 16);
			this.label_SecenekAdet.TabIndex = 7;
			this.label_SecenekAdet.Text = "Seçenek Adedi :";
			this.label_SecenekAdet.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// numericUpDown1
			// 
			this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.numericUpDown1.Location = new System.Drawing.Point(352, 360);
			this.numericUpDown1.Minimum = new System.Decimal(new int[] {
																		   10,
																		   0,
																		   0,
																		   0});
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.Size = new System.Drawing.Size(56, 20);
			this.numericUpDown1.TabIndex = 8;
			this.numericUpDown1.Value = new System.Decimal(new int[] {
																		 10,
																		 0,
																		 0,
																		 0});
			// 
			// label_Cevap
			// 
			this.label_Cevap.Location = new System.Drawing.Point(240, 104);
			this.label_Cevap.Name = "label_Cevap";
			this.label_Cevap.TabIndex = 9;
			this.label_Cevap.Text = "label_Cevap";
			// 
			// checkBox_Sesli
			// 
			this.checkBox_Sesli.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			this.checkBox_Sesli.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.checkBox_Sesli.Location = new System.Drawing.Point(184, 416);
			this.checkBox_Sesli.Name = "checkBox_Sesli";
			this.checkBox_Sesli.Size = new System.Drawing.Size(66, 24);
			this.checkBox_Sesli.TabIndex = 10;
			this.checkBox_Sesli.Text = "Sesli";
			// 
			// Form_KelimeBulma
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 445);
			this.ContextMenu = this.contextMenu1;
			this.Controls.Add(this.checkBox_Sesli);
			this.Controls.Add(this.label_Cevap);
			this.Controls.Add(this.numericUpDown1);
			this.Controls.Add(this.label_SecenekAdet);
			this.Controls.Add(this.label_Sonuc);
			this.Controls.Add(this.pictureBox_Dogru);
			this.Controls.Add(this.pictureBox_Yanlis);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label_Kelime);
			this.Controls.Add(this.checkedListBox1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.KeyPreview = true;
			this.Name = "Form_KelimeBulma";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.Text = "Kelime Bulma Oyunu";
			this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form_KelimeBulma_KeyPress);
			this.Load += new System.EventHandler(this.Form_KelimeBulma_Load);
			this.Closed += new System.EventHandler(this.Form_KelimeBulma_Closed);
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void SoruSor()
		{
			myDm.OpenConn();
			if (bSoruCevaplandi == true) 
			{
				//Sonuçları Kaydetme Bölümü
				myDm.Kaydet(mySorulanSozlukRecord);
			}
			this.label_Sonuc.Text = "";
			bSoruCevaplandi = false;
			bool bBulundu = false;
			Random random = new Random();
			myDm.sQuery = "Select * from SOZLUK Order By English";
			myDm.sTableName = "SOZLUK";
			DataTable myTable = new DataTable();
			myTable = myDm.ExecuteQuery();
			if (myTable!=null) 
			{
				mySozlukRecord = new General.Sozluk[Convert.ToInt32(this.numericUpDown1.Value)];
				int iSor = 0;
				int iRastgeleBirSayi = 0;
				this.checkedListBox1.Items.Clear();
				iSor = 0;
				if (this.menuItem1.Checked == true)			iKelimeTur = 0;
				else if (this.menuItem2.Checked == true)	iKelimeTur = 1;
				else 
				{
					if (iKelimeTur == 0) iKelimeTur = 1;
					else iKelimeTur = 0;
				}
				for (int i=0;i<this.numericUpDown1.Value;i++) 
				{
					iRastgeleBirSayi = random.Next(0, myTable.Rows.Count); 
					DataRow myTableRow = myTable.Rows[iRastgeleBirSayi];
					bBulundu = false;
					for (int j=0;i<i;j++) 
					{
						if (mySozlukRecord[i].RecordId.ToString() != 
							myTableRow["RecordID"].ToString()) 
								bBulundu = true;
					}
					if (bBulundu == false) 
					{
						mySozlukRecord[i] = new General.Sozluk(myTableRow);
						if (iKelimeTur == 0 ) //Sorulan Kelime Türkçe
							this.checkedListBox1.Items.Add(mySozlukRecord[i].English);
						else	this.checkedListBox1.Items.Add(mySozlukRecord[i].Turkce);
					}
					else i--;
				}
				iSor = random.Next(0,Convert.ToInt32(this.numericUpDown1.Value) -1);
				mySorulanSozlukRecord = mySozlukRecord[iSor];
				if (iKelimeTur==0) 
				{
					this.label_Kelime.Text = mySorulanSozlukRecord.Turkce.ToString();
					label_Cevap.Text = mySorulanSozlukRecord.English.ToString();
				} 
				else 
				{
					this.label_Kelime.Text = mySorulanSozlukRecord.English.ToString();
					label_Cevap.Text = mySorulanSozlukRecord.Turkce.ToString();
					if (this.checkBox_Sesli.Checked)
						myMp3Play.sPlayWord = this.label_Kelime.Text;
				}
			}
			myDm.OpenConn();
			myTable.Dispose();
			mySozlukRecord = null;
			this.pictureBox_Yanlis.Visible = false;
			this.pictureBox_Dogru.Visible = false;
		}

		private void Form_KelimeBulma_Load(object sender, System.EventArgs e)
		{
			General.GenelAyarlar myAyarlar = new KelimeEzber.General.GenelAyarlar();
			myMp3Play = new KelimeEzber.General.Mp3Play();
			this.numericUpDown1.Value = myAyarlar.iSecenekAdedi;
			this.menuItem1.Checked = false;
			this.menuItem2.Checked = false;
			if (myAyarlar.iSoruTuru == 0)
				this.menuItem1.Checked = true;
			else if (myAyarlar.iSoruTuru == 1)
				this.menuItem2.Checked = true;
			this.checkBox_Sesli.Checked = myAyarlar.bSes;
			myAyarlar.Dispose();
			myAyarlar = null;

			label_Cevap.Text = "";
			label_Cevap.Visible =
			this.pictureBox_Dogru.Visible=
			this.pictureBox_Yanlis.Visible=
			this.label_Sonuc.Visible=false;
			SoruSor();
		}

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			SoruSor();
		}

		private void checkedListBox1_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
		{
			int iSec = e.Index; //this.checkedListBox1.SelectedIndex;
			this.pictureBox_Yanlis.Visible = false;
			if (this.checkBox_Sesli.Checked) 
			{
				if (this.iKelimeTur == 0)
				{
					myMp3Play.sPlayWord = "";
					myMp3Play.sPlayWord = this.checkedListBox1.Items[iSec].ToString();
				}
			}
			if (e.NewValue == CheckState.Checked)
			{	if (this.checkedListBox1.Items[iSec].ToString() == this.label_Cevap.Text) 
				{
					this.pictureBox_Yanlis.Visible = false;
					this.pictureBox_Dogru.Visible = true;
					if (bSoruCevaplandi == false)
						mySorulanSozlukRecord.KacDefaBilindi ++;
					this.label_Sonuc.Visible = true;
					this.label_Sonuc.Text = "Sonuçlar :\n" +
											"     Bilindi    :" + mySorulanSozlukRecord.KacDefaBilindi.ToString() + "\n" +
											"     Bilinemedi :" + mySorulanSozlukRecord.KacDefaBilinemedi.ToString();
					bSoruCevaplandi = true;
					return;
				}
				else 
				{
					//((Control)sender).Hide();
					if (bSoruCevaplandi == false)
						mySorulanSozlukRecord.KacDefaBilinemedi ++;
					if (this.pictureBox_Dogru.Visible != true)
						this.pictureBox_Yanlis.Visible = true;
					e.NewValue = CheckState.Unchecked;
					this.checkedListBox1.Items.RemoveAt(iSec);
					bSoruCevaplandi = true;
				}
			}
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			if (menuItem1.Checked == true)
					menuItem1.Checked = false;
			else	menuItem1.Checked = true;
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			if (menuItem2.Checked == true)
				menuItem2.Checked = false;
			else	menuItem2.Checked = true;
		}

		private void Form_KelimeBulma_Closed(object sender, System.EventArgs e)
		{
			if (bSoruCevaplandi == true) 
			{
				//Sonuçları Kaydetme Bölümü
				myDm.Kaydet(mySorulanSozlukRecord);
			}
			General.GenelAyarlar myAyarlar = new KelimeEzber.General.GenelAyarlar();
			myAyarlar.iSecenekAdedi = Convert.ToInt32(this.numericUpDown1.Value);
			myAyarlar.bSes = this.checkBox_Sesli.Checked;
			if (this.menuItem1.Checked == true)
					myAyarlar.iSoruTuru = 0;
			else if (this.menuItem2.Checked == true)
					myAyarlar.iSoruTuru = 1;
			else	myAyarlar.iSoruTuru = -1;
			myAyarlar.Dispose();
			myAyarlar = null;
		}

	}
}
