/// <summary>
 /// check if user with same email already there.
 /// </summary>
 /// <param name="email"></param>
 /// <returns></returns>
 public static bool checkUserByEmail(string email)
 {
     using (SVDB_Entities db = new SVDB_Entities())
     {
         tblUser user = db.tblUsers.Where(x => x.email == email).FirstOrDefault();
         if (user != null) return true; else return false;
     }
 }
 public static tblUser getUserById(int id)
 {
     using(SVDB_Entities db = new SVDB_Entities())
     {
         tblUser user = db.tblUsers.Where(x => x.userId == id).FirstOrDefault();
         return user;
     }
 }
        public static tblUser _checkUser(tblUser user)
        {
            SVDB_Entities db = new SVDB_Entities();

            tblUser _user =   db.tblUsers.Where(x => x.email == user.email && x.password == user.password).FirstOrDefault();
            if (_user != null) return _user;
            else return _user;
            
        }
 /// <summary>
 /// add user to database coming from register.
 /// </summary>
 /// <param name="user"></param>
 public static string _addUser(tblUser user)
 {
     using(SVDB_Entities db = new SVDB_Entities())
     {
         
             user.createOn = DateTime.Now;
             user.ipaddress = "127.0.0.1";
             db.tblUsers.Add(user);
             db.SaveChanges();
             return "User Created!";                            
     }
 }
        public ActionResult Discussion(int page)
        {
            var _pagedData = new PagedData<tblQuestion>();

            using (var db = new SVDB_Entities())
            {
                _pagedData.Data = db.tblQuestions.OrderBy(x => x.questionId).Skip(PageSize * (page -1)).Take(PageSize).ToList();
                _pagedData.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)db.tblQuestions.Count() / PageSize));
                _pagedData.CurrentPage = page;
            }
            ViewBag._pagedData = _pagedData;
            return PartialView(_pagedData);
        }
        public JsonResult submitQuestion(tblQuestion question)
        {

            if(!question.questionImage.Contains("undefined") && question.questionImage != null)
            {
                var regex = new Regex(@"data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*),(?<height>[0-9]+)px,(?<width>[0-9]+)px", RegexOptions.Compiled);
                var match = regex.Match(question.questionImage);

                var mime = match.Groups["mime"].Value;
                var encoding = match.Groups["encoding"].Value;
                string data = match.Groups["data"].Value;
                var height = Convert.ToInt32(match.Groups["height"].Value);
                var width = Convert.ToInt32(match.Groups["width"].Value);


                Image img = SuccessVidya.BAL.Utility.Base64ToImage(data);
                Image resizedImage = img.GetThumbnailImage(width, height, null, IntPtr.Zero);

                question.questionImage = Guid.NewGuid().ToString();
                //resizedImage.Save("~/QuestionImages/asd1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
                resizedImage.Save(Server.MapPath("~/QuestionImages/" + question.questionImage + ".jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);               

                int index = question.questionText.IndexOf(data); 
                //without image will go to DB
                question.questionText = (index < 0)
                    ? question.questionText
                    : question.questionText.Remove(index, data.Length);
            }

            question.createOn = DateTime.Now;
            question.Years = 2013;
            question.userID = 2;
            question.secionID = 11;
            question.Examid = 1;
            question.correctAnswer = "A";



            using (SVDB_Entities db = new SVDB_Entities())
            {
                if (ModelState.IsValid)
                {
                    db.tblQuestions.Add(question);
                    db.SaveChanges();
                }
            }
            return Json(new { Answer = "question submitted", ErrorMessage = "" });
            //return RedirectToAction("Index","Home");
        }
        public ActionResult Index()
        {
                var _pagedData = new PagedData<tblQuestion>();

                List<mstSection> sectionList = SectionManagement.getSections();                 
                ViewBag.sectionList = sectionList;
                ViewBag.subSectionList = SectionManagement.getSubsections(1);
            
                using(SVDB_Entities db = new SVDB_Entities())
                {
                    _pagedData.Data = db.tblQuestions.OrderBy(x => x.questionId).Take(PageSize).ToList();
                    _pagedData.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)db.tblQuestions.Count() / PageSize));
                    ViewBag._pagedData = _pagedData;  
                }           
                return View();
        }