using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;
using System.Timers;
using System.Web.SessionState;
using System.IO;
namespace student_management.studpro
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        int TotQus = 0;
        static int count = 0;
        static int viewradio = 0;
        float sepmarks = 0.0f;
        static int marks = 0, correct = 0;
        int totalSeconds = 0, seconds = 60, minutes = 60, selectedcount = 0, originalcount = 0, crt = 0, wr = 0;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aloproConnectionString"].ConnectionString);
        string selected=null,choose;
        string Result = null;
        static String CrtAns = null, ChoiceType = null;
        //string constr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Administrator\\Documents\\alopro.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        DataTable dt;
        DataSet ds;
        SqlDataAdapter ad;
        static int rowindex = -1;
        
        protected void Page_Load(object sender, EventArgs e)
        {
           
            DropDownList1.Visible = true;
            string noofqns = "SELECT [Questions] FROM [testsettings]";
            SqlCommand cmd = new SqlCommand(noofqns, con);
            con.Open();
            TotQus = Convert.ToInt16(cmd.ExecuteScalar().ToString());
            ds = new DataSet();
            ad = new SqlDataAdapter("SELECT * FROM addsubject", con);
            ad.Fill(ds,"addsubject");
            dt = ds.Tables["addsubject"];
            marks = (int)correct;
             if (marks >= 40 && marks<=45)
             {
                 Result = "Good";
             }
             else if(marks >=45)
             {
                 Result = "Best";
             }
             else if (marks >= 35)
             {
                 Result = "Better";
             }
             else if (marks <= 35)
             {
                 Result = "NotBad";
             }
            con.Close();
            if (viewradio == 0)
            {
                RadioInvisible();
            }
            else
            {
                RadioVisible();
            }
            viewradio += 1;
            CheckBoxInvisible();
        }
        protected void Timer1_tick(object sender, EventArgs e)
        {
            Session["time"] = Convert.ToInt16(Session["time"]) - 1;
            if (Convert.ToInt16(Session["time"]) <= 0)
            {
                ImageButton2.Visible = false;
              
                RadioInvisible();              
           }
            else
            {
                totalSeconds = Convert.ToInt16(Session["time"]);
                seconds = totalSeconds % 60;
                minutes = totalSeconds / 60;
                clock.Text = minutes + ":" + seconds;
            }
        }
        public string SelectedAnswer()
        {          
            if (RadioButton1.Checked==true)
            {              
                return "A";
            }
            else if (RadioButton2.Checked==true)
            {        
                return "B";
            }
            else if (RadioButton3.Checked==true)
            {              
                return "C";
            }
            else if (RadioButton4.Checked==true)
            {             
                return "D";
            }
            else
            {              
                return "nothing";
            }
        }
        protected string ChooseAnswer()
        {
            if (CheckBox1.Checked)
            {
                choose = "A,";
            }
            if (CheckBox2.Checked)
            {
                choose += "B,";
            }
            if (CheckBox3.Checked)
            {
                choose += "C,";
            }
            if (CheckBox4.Checked)
            {
                choose += "D,";
            }
            return choose;
        }
        protected void RadioUncheck()
        {
            RadioButton1.Checked = false;
            RadioButton2.Checked = false;
            RadioButton3.Checked = false;
            RadioButton4.Checked = false;
        }
        protected void CheckBoxUncheck()
        {
            CheckBox1.Checked = false;
            CheckBox2.Checked = false;
            CheckBox3.Checked = false;
            CheckBox4.Checked = false;
        }
        protected void RadioInvisible()
        {
            RadioButton1.Visible = false;
            RadioButton2.Visible = false;
            RadioButton3.Visible = false;
            RadioButton4.Visible = false;
        }
        protected void RadioVisible()
        {
            RadioButton1.Visible = true;
            RadioButton2.Visible = true;
            RadioButton3.Visible = true;
            RadioButton4.Visible = true;
        }
        protected void CheckBoxVisible()
        {
            CheckBox1.Visible = true;
            CheckBox2.Visible = true;
            CheckBox3.Visible = true;
            CheckBox4.Visible = true;
        }
        protected void CheckBoxInvisible()
        {
            CheckBox1.Visible = false;
            CheckBox2.Visible = false;
            CheckBox3.Visible = false;
            CheckBox4.Visible = false;
        }
        protected void Report()
        {
            try
            {
                //con.Open();
                string InsQuery = "INSERT INTO results(Dept,Testdate,Questions,Marks,Status,Username)values('" + DropDownList1.SelectedValue + "','" + DateTime.Today + "','" + TotQus + "','" + marks + "','" + Result + "','" + txtuser.Text + "')";              
                SqlCommand cmd2 = new SqlCommand(InsQuery, con);
                con.Open();
                Convert.ToInt16(cmd2.ExecuteNonQuery().ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex);
                Response.Write("Try Again");
            }
            finally
            {
                con.Close();
            }
        }
 
      protected void total()
      {
          try
          {
              con.Open();
              if (ChoiceType == "Single")
              {
                  selected = SelectedAnswer();
                  if (CrtAns == selected)
                  {
                      correct = correct + 1;
                  }
                  else
                  {
                      correct = correct - 1 / 100;
                  }
              }
              else
              {
                  Response.Write("<script language='javascript'>alert('no marks');</script>");
              }
          }
          catch (Exception)
          {
              correct += Convert.ToInt16(sepmarks * crt) + 1;
              scorestxtbox.Text = correct.ToString();
          }
          finally
          {
              //Response.Write("Wait");
              con.Close();
              RadioUncheck();
              CheckBoxUncheck();
          }
      }
      protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
      {
         
      }
      protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
      {
          con.Open();
          count += 1;
          try
          {
              if (rowindex < TotQus)
              {
                  rowindex = rowindex + 1;
                  ChoiceType = dt.Rows[rowindex]["ChooseAnswer"].ToString();
                  lblnoofquestions.Text = count.ToString() + "  of  " + TotQus;
                  subname.Text = dt.Rows[rowindex]["Subjectname"].ToString();
                  lblQuestion.Text = dt.Rows[rowindex]["Questions"].ToString();
                  Option1.Text = dt.Rows[rowindex]["Answera"].ToString();
                  Option2.Text = dt.Rows[rowindex]["Answerb"].ToString();
                  Option3.Text = dt.Rows[rowindex]["Answerc"].ToString();
                  Option4.Text = dt.Rows[rowindex]["Answerd"].ToString();
                  CrtAns = dt.Rows[rowindex]["Correctans"].ToString();
                  total();
                  Report();
                  if (ChoiceType == "Single")
                  {
                      RadioVisible();
                      CheckBoxInvisible();
                  }
                  else
                  {
                      RadioInvisible();
                      CheckBoxVisible();
                  }
              }
              else
              {
                  ImageButton2.Visible = false;                
                  RadioInvisible();
                  CheckBoxInvisible();
              }
          }
          catch (Exception)
          {
              Response.Write("<script language='javascript'>alert('Wait utill time over');</script>");
          }
          finally
          {
              //RadioUncheck();
              //CheckBoxUncheck();
              con.Close();
          }
      }
      protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
      {
          Panel1.Visible = true;
          DropDownList1.Visible = false;
          Labeldept.Visible = false;
          txtuser.Enabled = false;
          Response.Write("<script language='javascript'>alert('TimeStarts now');</script>");
          string query = "SELECT [Time] FROM [testsettings]";
          SqlCommand cmd = new SqlCommand(query, con);
          con.Open();
          int timeget = Convert.ToInt16(cmd.ExecuteScalar().ToString());
          totalSeconds = timeget * 60;
          Timer1.Enabled = true;
          Session["time"] = totalSeconds;
          rowindex = rowindex + 1;
          count = count + 1;
          try
          {
              if (rowindex < TotQus)
              {
                  ChoiceType = dt.Rows[rowindex]["ChooseAnswer"].ToString();
                  lblnoofquestions.Text = count.ToString() + "  of  " + TotQus;
                  subname.Text = dt.Rows[rowindex]["Subjectname"].ToString();
                  lblQuestion.Text = dt.Rows[rowindex]["Questions"].ToString();
                  Option1.Text = dt.Rows[rowindex]["Answera"].ToString();
                  Option2.Text = dt.Rows[rowindex]["Answerb"].ToString();
                  Option3.Text = dt.Rows[rowindex]["Answerc"].ToString();
                  Option4.Text = dt.Rows[rowindex]["Answerd"].ToString();
                  CrtAns = dt.Rows[rowindex]["Correctans"].ToString();
              }
              else
              {
                  RadioInvisible();
                  CheckBoxVisible();
              }
          }
          catch (IndexOutOfRangeException)
          {
              Response.Write("Questions Over");
          }
          finally
          {
              ImageButton3.Visible = true;
              ImageButton2.Visible = true;
              con.Close();
          }
      }
       
       }
    }