using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.OleDb;

namespace CSDic
{
    public partial class Form1 : Form
    {
        //CSDic - Free Persian Dictionary !

            //Programmer : Mehran Rasa - M8SPY .
            //Email : mehran8_rasa@yahoo.com
            //http://m8spy.blogfa.com


        public Form1()
        {
            InitializeComponent();
        }
        OleDbConnection CON;
        OleDbDataAdapter ADP;
        DataSet DataSET = new DataSet();
        string ConStr = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=Words.mdb";
        string SelStr = "SELECT * FROM CSDic";

        int FaSearch = -1;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Enabled = false;
        }

        private void RAD1_CheckedChanged(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("en-US"));
            BOX1.RightToLeft = RightToLeft.No;
            BOX2.RightToLeft = RightToLeft.Yes;

                BOX1.Text = null;
                BOX2.Text = null;
                LBFound.Text = null;
                BOX1.Focus();
        }

        private void RAD2_CheckedChanged(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("fa-IR"));
            BOX1.RightToLeft = RightToLeft.Yes;
            BOX2.RightToLeft = RightToLeft.No;

                BOX1.Text = null;
                BOX2.Text = null;
                LBFound.Text = null;
                BOX1.Focus();
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            CON.Close(); DataSET.Clear();
            Application.Exit();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            BOX1.Focus();
        }

        private void SEARCH()

        {
            try
            {

            int cn = DataSET.Tables["CSDic"].Rows.Count;
            int en, fa;

            if (BOX1.Text.Trim() != null && BOX2.Text =="" )
            {
                Pr.Maximum = cn;
                P.Visible =false ;
                
                for (int m = 0; m < cn; m++)
                {
                    Pr.Value = m;
                    Application.DoEvents();

                    //English 2 Farsi
                    if (RAD1.Checked)
                    {
                            string EngWord = DataSET.Tables["CSDic"].Rows[m].ItemArray[0].ToString()
                                    .ToLower();

                            if (EngWord == BOX1.Text.ToLower())
                            {
                                BOX2.Text = DataSET.Tables["CSDic"].Rows[m].ItemArray[1].ToString()
                                    .ToLower();
                                LBFound.Text = EngWord;
                                Pr.Value = cn; P.Visible = true; break;
                            }
                    }

                    //Farsi to English
                    if (RAD2.Checked)
                    {
                        string FaWord = DataSET.Tables["CSDic"].Rows[m].ItemArray[1].ToString()
                                    .ToLower();

                        if (FaWord ==BOX1.Text.ToLower())
                        {
                            BOX2.Text =DataSET.Tables["CSDic"].Rows[m].ItemArray[0].ToString()
                                    .ToLower();
                            LBFound.Text  = FaWord;
                            Pr.Value = cn; P.Visible = true; break;
                            }
                else 
                {

                FaSearch = FaWord.IndexOf(BOX1.Text.ToLower());
                        
                    if (FaSearch !=-1)
                    {
                        BOX2.Text = BOX2.Text = DataSET.Tables["CSDic"].Rows[m].ItemArray[0].ToString()
                            .ToLower();
                        LBFound.Text = DataSET.Tables["CSDic"].Rows[m].ItemArray[1].ToString()
                            .ToLower();
                        Pr.Value = cn; P.Visible = true; break;
                        

                     }

                        }
                    }
                }
                groupBox1.Enabled = true;
                BOX1.Enabled = true;
                checkBox1.Enabled = true;

        }
               }
            catch {}
    }

        private void BOX1_KeyUp(object sender, KeyEventArgs e)
        {
            BOX2.Text = "";

            if (checkBox1.Checked)
            {
                groupBox1.Enabled = false;
                SEARCH();
            }
            else if (e.KeyCode == Keys.Enter)
            {
                BOX1.Enabled = false;
                checkBox1.Enabled = false;
                groupBox1.Enabled = false;
                SEARCH();
            }
            
            
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;

            try
            {
                //
                CON = new OleDbConnection(ConStr);
                ADP = new OleDbDataAdapter(SelStr, CON);
                ADP.Fill(DataSET, "CSDic");
            }
            catch { MessageBox.Show("بانک اطلاعاتی برنامه پیدا نشد", "'Words.mdb' not found", MessageBoxButtons.OK, MessageBoxIcon.Error); Close(); }

            this.Enabled = true;
            BOX1.Focus();

        }






        }

}
