using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace AzmoonGir
{
    public partial class FrmAzmoon : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        int QN = 0;
        DataTable dt;
        ListBox answerlist;
        int gozineh;
        int sahih, nazade, qalat;



        public FrmAzmoon()
        {
            InitializeComponent();
        }




        private void ClearCheck()
        {

            rdbCase1.Checked = false;
            rdbCase2.Checked = false;
            rdbCase3.Checked = false;
            rdbCase4.Checked = false;


        }



        private void FrmAzmoon_Load(object sender, EventArgs e)
        {

            SqlDataAdapter da = new SqlDataAdapter("SelectAllGroup", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            CmbGroups.DataSource = dt;
            CmbGroups.DisplayMember = "GroupName";
            CmbGroups.ValueMember = "GroupID";



            //   cbxDesign_DrawItem(CmbGroups,CmbGroups_Dr );

        }












        private void TxtTedad_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) <= 57 & Convert.ToInt16(e.KeyChar) >= 48 || Convert.ToInt16(e.KeyChar) == 8)
            {
            }
            else
            {
                e.KeyChar = Convert.ToChar(Keys.None);
            }
        }



        private void btnNext_Click(object sender, EventArgs e)
        {
            btnPrev.Enabled = true;
            
           
            if (rdbCase1.Checked == true) 
            {
                gozineh = 1;
            }
            else if (rdbCase2.Checked == true) 
            {
                gozineh = 2;
            }
            else if (rdbCase3.Checked == true) 
            {
                gozineh = 3;
            }
            else if (rdbCase4.Checked == true) 
            {
                gozineh = 4;
            }
            else
            {
                gozineh = 0;
            }


            answerlist.Items[QN] = gozineh.ToString();

            if (QN < dt.Rows.Count - 1)
            {
                
                QN++;
                lblQtext.Text = dt.Rows[QN]["QuestionText"].ToString();
                rdbCase1.Text = dt.Rows[QN]["Case1"].ToString();
                rdbCase2.Text = dt.Rows[QN]["Case2"].ToString();
                rdbCase3.Text = dt.Rows[QN]["Case3"].ToString();
                rdbCase4.Text = dt.Rows[QN]["Case4"].ToString();

            }

            if (QN == dt.Rows.Count - 1)
            {
                btnNext.Enabled = false;
                btnEnd.Enabled = true;
                

            }


            
            if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 0)
            {
                ClearCheck();
            }

            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 1)
            {
                rdbCase1.Checked = true;
            }

            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 2)
            {
                rdbCase2.Checked = true;
            }


            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 3)
            {
                rdbCase3.Checked = true;
            }



            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 4)
            {
                rdbCase4.Checked = true;
            }



            lblQnumber.Text = Convert.ToString(Convert.ToInt32(lblQnumber.Text) + 1);


        }





        private void btnPrev_Click(object sender, EventArgs e)
        {
            btnNext.Enabled = true;
            btnEnd.Enabled = false;
           
            if (rdbCase1.Checked == true)
            {
                gozineh = 1;
            }
            else if (rdbCase2.Checked == true)
            {
                gozineh = 2;
            }
            else if (rdbCase3.Checked == true)
            {
                gozineh = 3;
            }
            else if (rdbCase4.Checked == true)
            {
                gozineh = 4;
            }
            else 
            {
                gozineh = 0;
            }


            answerlist.Items[QN] = gozineh.ToString();

            if (QN > 0)
            {
                
                QN--;
                lblQtext.Text = dt.Rows[QN]["QuestionText"].ToString();
                rdbCase1.Text = dt.Rows[QN]["Case1"].ToString();
                rdbCase2.Text = dt.Rows[QN]["Case2"].ToString();
                rdbCase3.Text = dt.Rows[QN]["Case3"].ToString();
                rdbCase4.Text = dt.Rows[QN]["Case4"].ToString();
            }


            if (QN == 0)
            {
                btnPrev.Enabled = false;
                
            }






            if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 0)
            {
                ClearCheck();
            }

            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 1)
            {
                rdbCase1.Checked = true;
            }

            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 2)
            {
                rdbCase2.Checked = true;
            }


            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 3)
            {
                rdbCase3.Checked = true;
            }



            else if (Convert.ToInt32(answerlist.Items[QN].ToString()) == 4)
            {
                rdbCase4.Checked = true;
            }







            lblQnumber.Text = Convert.ToString(Convert.ToInt32(lblQnumber.Text) - 1);







        }





        private void BtnAzmoon_Click(object sender, EventArgs e)
        {
             

            if (string.IsNullOrWhiteSpace(TxtTedad.Text))
            {

                errorProvider1.SetError(TxtTedad, "تعداد سوالات را وارد کنید");
                return;

            }

            if (TxtTedad.Text == "0")
            {

                errorProvider1.SetError(TxtTedad, "تعداد سوالات را وارد کنید");
                return;

            }


            errorProvider1.SetError(TxtTedad, "");

            //SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS;DataBase = DbAzmoon;Integrated Security = True");
            SqlCommand cmd = new SqlCommand("CheckQuestionNumber", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@groupid", CmbGroups.SelectedValue);
            cmd.Parameters.AddWithValue("@questionnumber", TxtTedad.Text);

            cmd.Parameters.Add("@result", SqlDbType.Int);
            cmd.Parameters["@result"].Direction = ParameterDirection.Output;

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            int res = Convert.ToInt32(cmd.Parameters["@result"].Value.ToString());



            Frm_Message Fm = new Frm_Message();

            if (res == 0)
            {
                Fm.lbltext.Text = "تعداد سوالات مد نظر بیشتر از تعداد سوالات موجود در بانک است";
                Fm.ImgClose.Visible = true;
                Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;
                Fm.ShowDialog();
            }
            else
            {



                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                dt = new DataTable();
                da.Fill(dt);


                QN = 0;
               

                lblQnumber.Text = Convert.ToString(QN + 1);


                lblQtext.Text = dt.Rows[QN]["QuestionText"].ToString();
                rdbCase1.Text = dt.Rows[QN]["Case1"].ToString();
                rdbCase2.Text = dt.Rows[QN]["Case2"].ToString();
                rdbCase3.Text = dt.Rows[QN]["Case3"].ToString();
                rdbCase4.Text = dt.Rows[QN]["Case4"].ToString();



               
                btnNext.Enabled = true;
                btnPrev.Enabled = false;
                BtnAzmoon.Enabled = false;
                ChTime.Enabled = false;
                TxtTedad.Enabled = false;
                CmbGroups.Enabled = false;



                ClearCheck();
                answerlist = new ListBox();


                for (int i = 0; i < dt.Rows.Count ; i++)
                {
                    answerlist.Items.Add("0");
                }






                label1.Visible = true;
                lblQnumber.Visible = true;
                lblQtext.Visible = true;
                lblTime.Visible = true;
                rdbCase1.Visible = true;
                rdbCase2.Visible = true;
                rdbCase3.Visible = true;
                rdbCase4.Visible = true;
                btnEnd.Visible = true;
                btnNext.Visible = true;
                btnPrev.Visible = true;


                if (ChTime.Checked == true)
                {
                    lblTime.Text = Convert.ToString((Properties.Settings.Default.Qtime) * (Convert.ToInt32(TxtTedad.Text)));
                    timer1.Enabled = true;
                }

                else
                {
                    lblTime.Text = "نامحدود";
                }





                //  Grd.DataSource = dt;

                //  ListBox lst = new ListBox();
                //  while (lst.Items.Count < Convert.ToInt32(TxtTedad.Text))
                //  {


                //Random objrnd = new Random();
                //int num = objrnd.Next(Convert.ToInt32( dt.Rows[0][0].ToString()), Convert.ToInt32( dt.Rows[dt.Rows.Count - 1][0].ToString()));



                //   }





            }


        }

        private void rdbCase1_CheckedChanged(object sender, EventArgs e)
        {
            

        }

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

            sahih = 0;
            nazade = 0;
            qalat = 0;



            if (rdbCase1.Checked == true)
            {
                gozineh = 1;
            }
            else if (rdbCase2.Checked == true)
            {
                gozineh = 2;
            }
            else if (rdbCase3.Checked == true)
            {
                gozineh = 3;
            }
            else if (rdbCase4.Checked == true)
            {
                gozineh = 4;
            }
            else
            {
                gozineh = 0;
            }


            answerlist.Items[QN] = gozineh.ToString();




            for (int i = 0; i < Convert.ToInt32(answerlist.Items.Count.ToString()); i++)
            {

                if (answerlist.Items[i].ToString() == dt.Rows[i]["Answer"].ToString())
                {
                    sahih = sahih + 1;
                }

                else if (answerlist.Items[i].ToString() == "0")
                {
                    nazade = nazade + 1;
                }
                else
                {
                    qalat = qalat + 1;
                }


            }




            FrmNatije fmn = new FrmNatije();
            fmn.lblsahih.Text = sahih.ToString();
            fmn.lblqalat.Text =  qalat.ToString();
            fmn.lblnazade.Text = nazade.ToString();

            fmn.ShowDialog();
            this.Close();














        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblTime.Text) > 1)
            {
                lblTime.Text = Convert.ToString(Convert.ToInt32(lblTime.Text) - 1);
            }
            else
            {
                btnEnd_Click(null, null);
            }
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void TxtTedad_TextChanged(object sender, EventArgs e)
        {

        }




    }

}

