//  public ActionResult ShowVideo(int SubChapID, string name)
        public ActionResult ShowVideo(int SubChapID, int questionId, int exerciseId, string transactionid
                                      , string session_id = null, bool isdwn = false
                                      )
        {
            if (TempData["paymentTransactionId"] != null)
            {
                PayPalManager payPalManager = new PayPalManager();
                var           invoice       = payPalManager.GetPayPalInvoiceByGUID(Convert.ToString(TempData["paymentTransactionId"]));
                if (invoice != null)
                {
                    if (invoice.QuestionID != SubChapID)
                    {
                        ViewBag.paymentTransactionId = null;
                    }
                    else
                    {
                        ViewBag.paymentTransactionId = TempData["paymentTransactionId"];
                    }
                }
                //TempData.Keep("paymentTransactionId");
            }
            if (string.IsNullOrEmpty(transactionid))
            {
                if (isdwn == true)
                {
                    List <Questions> Question = new SubjectsController().getChaptersQuesionsByQuestionID(questionId);
                    if (Question.Select(x => x.isfree).FirstOrDefault() == true)
                    {
                        var showVideoViewModel1 = new SubjectsController().getShowVideoViewModelByID(SubChapID).FirstOrDefault();
                        var Exercises1          = new SubjectsController().getShowVideoViewModelByID(exerciseId);
                        var question1           = Exercises1.Where(x => x.ContentsHolder == questionId).FirstOrDefault();
                        ViewBag.Path = "/ContentStorage/ChaptersContents/" + System.IO.Path.GetFileName(question1.VideoFilePath);
                        showVideoViewModel1.ExerciseName = Exercises1.FirstOrDefault().ChapterName;
                        showVideoViewModel1.QuestionText = question1.QuestionText;

                        string downloadURL1 = GetBaseUrl() + "/Home/DownloadQuestion?p1=" + questionId;
                        ViewBag.DownloadURL   = downloadURL1;
                        ViewBag.transactionid = transactionid;


                        return(View("ShowVideo", "~/Views/Shared/_IndexPageLayout.cshtml", showVideoViewModel1));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    return
                        (RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                PayPalManager payPalManager = new PayPalManager();
                var           invoice       = payPalManager.GetPayPalInvoiceByGUID(transactionid);
                if (invoice != null && invoice.RequestXML.ToLower().Contains("stripe payment"))
                {
                    if (invoice.QuestionID != questionId && session_id != invoice.PaymentID)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    if (invoice == null || invoice.QuestionID != questionId || invoice.State != "approved")
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }

            var showVideoViewModel = new SubjectsController().getShowVideoViewModelByID(SubChapID).FirstOrDefault();
            var Exercises          = new SubjectsController().getShowVideoViewModelByID(exerciseId);
            var question           = Exercises.Where(x => x.ContentsHolder == questionId).FirstOrDefault();

            ViewBag.Path = "/ContentStorage/ChaptersContents/" + System.IO.Path.GetFileName(question.VideoFilePath);
            showVideoViewModel.ExerciseName = Exercises.FirstOrDefault().ChapterName;
            showVideoViewModel.QuestionText = question.QuestionText;

            string downloadURL = GetBaseUrl() + "/Home/DownloadQuestion?p1=" + questionId;

            ViewBag.DownloadURL   = downloadURL;
            ViewBag.transactionid = transactionid;

            try
            {
                string strURL = "/ContentStorage/ChaptersContents/" + System.IO.Path.GetFileName(question.VideoFilePath);
                //string strFileName = "/ContentStorage/ChaptersContents/" + System.IO.Path.GetFileName(videoFileName);
                WebClient    req      = new WebClient();
                HttpResponse response = System.Web.HttpContext.Current.Response;
                response.Clear();
                response.ClearContent();
                response.ClearHeaders();
                response.Buffer = true;
                response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
                //response.AppendHeader("Content-Disposition", "attachment; filename=" + subjectName +" "+ chapterName + " " + questionText + "");
                byte[] data = req.DownloadData(Server.MapPath(strURL));
                response.BinaryWrite(data);
                response.End();
            }
            catch (Exception ex)
            {
            }

            return(View("ShowVideo", "~/Views/Shared/_IndexPageLayout.cshtml", showVideoViewModel));
        }
        public ActionResult ChapterQuestions(int Sub, int questionYearId, string variant, bool scfp = false)
        {
            ChaptersViewModel vm            = new ChaptersViewModel();
            PayPalManager     payPalManager = new PayPalManager();
            int subjecttype;

            if (scfp == false)
            {
                if (TempData["paymentTransactionId"] != null)
                {
                    SubjectsController sc = new SubjectsController();
                    sc.DeleteInvoiceFromPaymentQuestion(Convert.ToString(TempData["paymentTransactionId"]));
                    payPalManager.DeletePayPalInvoice(Convert.ToString(TempData["paymentTransactionId"]));

                    ViewBag.paymentTransactionId     = null;
                    TempData["paymentTransactionId"] = null;
                }
                else if (TempData["paymentTransactionId"] == null)
                {
                    ViewBag.paymentTransactionId     = null;
                    TempData["paymentTransactionId"] = null;
                }
            }

            if (TempData["paymentTransactionId"] != null)
            {
                var invoice = payPalManager.GetPayPalInvoiceByGUID(Convert.ToString(TempData["paymentTransactionId"]));
                if (invoice != null)
                {
                    if (invoice.QuestionID != Sub)
                    {
                        ViewBag.paymentTransactionId = null;
                    }
                    else if (invoice.QuestionID == Sub && !string.IsNullOrEmpty(variant))
                    {
                        List <QuestionPayment> lstQuestion = new SubjectsController().getQuesionsFromPaymentQuestion_BySubjectsIDAndTransactionAndVarient(Sub, Convert.ToString(TempData["paymentTransactionId"]), variant);
                        if (lstQuestion.Count > 0)
                        {
                            if (lstQuestion.Select(x => x.create_date == DateTime.Now.Date).FirstOrDefault())
                            {
                                ViewBag.paymentTransactionId = TempData["paymentTransactionId"];
                                TempData.Keep("paymentTransactionId");

                                //try
                                //{
                                //    MailMessage msgs = new MailMessage();
                                //    msgs.To.Add("*****@*****.**");
                                //    MailAddress address = new MailAddress("*****@*****.**");
                                //    msgs.From = address;
                                //    msgs.Subject = "USer Redirect to Payment";
                                //    string htmlBody = "Sending This email When user click on Payment Button ";
                                //    msgs.Body = htmlBody;
                                //    msgs.IsBodyHtml = true;
                                //    SmtpClient client = new SmtpClient();
                                //    client.Host = "relay-hosting.secureserver.net";
                                //    client.Port = 25;
                                //    client.UseDefaultCredentials = false;
                                //    client.Credentials = new System.Net.NetworkCredential("*****@*****.**", "asjad001$");
                                //    //Send the msgs
                                //    client.Send(msgs);

                                //}
                                //catch (Exception ex) { }
                            }
                            else
                            {
                                ViewBag.paymentTransactionId = null;
                            }
                        }
                        else
                        {
                            ViewBag.paymentTransactionId = null;
                        }
                    }
                    else
                    {
                        List <QuestionPayment> lstQuestion = new SubjectsController().getQuesionsFromPaymentQuestion_BySubjectsIDAndTransaction(Sub, Convert.ToString(TempData["paymentTransactionId"]));
                        if (lstQuestion.Count > 0)
                        {
                            if (lstQuestion.Select(x => x.create_date == DateTime.Now.Date).FirstOrDefault())
                            {
                                ViewBag.paymentTransactionId = TempData["paymentTransactionId"];
                                TempData.Keep("paymentTransactionId");
                                //try
                                //{
                                //    MailMessage msgs = new MailMessage();
                                //    msgs.To.Add("*****@*****.**");
                                //    MailAddress address = new MailAddress("*****@*****.**");
                                //    msgs.From = address;
                                //    msgs.Subject = "USer Redirect to Payment";
                                //    string htmlBody = "Sending This email When user click on Payment Button ";
                                //    msgs.Body = htmlBody;
                                //    msgs.IsBodyHtml = true;
                                //    SmtpClient client = new SmtpClient();
                                //    client.Host = "relay-hosting.secureserver.net";
                                //    client.Port = 25;
                                //    client.UseDefaultCredentials = false;
                                //    client.Credentials = new System.Net.NetworkCredential("*****@*****.**", "asjad001$");
                                //    //Send the msgs
                                //    client.Send(msgs);

                                //}
                                //catch (Exception ex) { }
                            }
                            else
                            {
                                ViewBag.paymentTransactionId = null;
                            }
                        }
                        else
                        {
                            ViewBag.paymentTransactionId = null;
                        }
                    }
                }
                else
                {
                    ViewBag.paymentTransactionId = null;
                }
            }
            else
            {
                ViewBag.paymentTransactionId = null;
            }



            List <SubjectChapters> lstRet  = new SubjectsController().getChaptersExerciseBySubjectsID(Sub);
            List <Questions>       lstRet1 = new SubjectsController().getChaptersQuesionsBySubjectsID(Sub);
            var variants1 = lstRet1.Where(x => !string.IsNullOrWhiteSpace(x.ImageFileName)).Select(x => x.ImageFileName).Distinct().ToList();

            subjecttype             = lstRet.Select(x => x.SubjectType).FirstOrDefault();
            ViewBag.subjecttype     = subjecttype;
            ViewBag.selectedVariant = null;

            // var type = lstsubjects.Select(x=>x.SubjectID) lstRet1.Select(x => x.SubjectsChaptersID).FirstOrDefault();
            vm.Variants = GetSelectListItems(variants1);
            var questionYear = lstRet1.Where(x => x.QuestionID == questionYearId).FirstOrDefault()?.PaperYear;

            //var questionYear = lstRet1.FirstOrDefault()?.PaperYear;
            ViewBag.questionYearId = questionYearId;


            //var variatnEndwith2;
            //foreach
            if (subjecttype == 1 || subjecttype == 4 || subjecttype == 5 || subjecttype == 8)
            {
                if (!string.IsNullOrEmpty(variant))
                {
                    lstRet1 = lstRet1.Where(x => x.ImageFileName == variant).ToList();
                    ViewBag.selectedVariant = variant;
                }
                else
                {
                    var variants2 = variants1.Where(x => x.EndsWith("2")).FirstOrDefault();
                    lstRet1 = lstRet1.Where(x => x.ImageFileName == variants2).ToList();
                    ViewBag.selectedVariant = variants2;
                }
            }



            vm.SubjectChapters = lstRet;
            vm.Questions       = lstRet1.Where(x => x.PaperYear == questionYear).ToList();

            return(View(vm));
        }