// GET: Quizs/Create
        public async Task <ActionResult> Create()
        {
            var user = await db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefaultAsync();

            Quiz q = new Quiz()
            {
                QuizNumber = NumberGenerator.GetQuizNumber(user), Title = "Новый тест", IsModified = true
            };

            return(View(q));
        }
        public async Task <ActionResult> Random(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var user = await db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefaultAsync();

            var currentQuiz = await db.Quizes.Where(q => q.Id == id).Include("Questions.Answers").FirstOrDefaultAsync();

            if (currentQuiz.ParentQuizId != 0)
            {
                int pId = currentQuiz.ParentQuizId;

                currentQuiz = await db.Quizes.Where(q => q.Id == pId).Include("Questions.Answers").FirstOrDefaultAsync();
            }

            Quiz newQuiz = Quiz.GetRandomizeQuiz(currentQuiz, NumberGenerator.GetQuizNumber(user));

            newQuiz.AnswerText = Quiz.SerializeAnswers(newQuiz.Questions.ToList());


            db.Entry(newQuiz).State = EntityState.Added;

            foreach (var question in newQuiz.Questions)
            {
                db.Entry(question).State = EntityState.Added;
                // db.Questions.Add(question);

                foreach (var answer in question.Answers)
                {
                    db.Entry(answer).State = EntityState.Added;
                    //db.QuestionAnswers.Add(answer);
                }
            }

            // db.Quizes.Add(newQuiz);


            user.Quizs.Add(newQuiz);
            db.Entry(user).State = EntityState.Modified;


            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Upload(HttpPostedFileBase upload, string title)
        {
            if (upload != null)
            {
                string fileName = Path.GetFileName(upload.FileName);
                byte[] binData  = new byte[upload.ContentLength];
                upload.InputStream.Read(binData, 0, upload.ContentLength);

                Stream stream = new MemoryStream(binData);

                string result = System.Text.Encoding.Default.GetString(binData);

                var user = await db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefaultAsync();

                Quiz quiz = new Quiz()
                {
                    Title = title, QuizNumber = NumberGenerator.GetQuizNumber(user), IsModified = true
                };

                using (ExcelPackage package = new ExcelPackage(stream))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                    for (int i = 1; i <= worksheet.Dimension.Rows; i++)
                    {
                        try
                        {
                            Question question = new Question()
                            {
                                QuestionText = worksheet.Cells[i, 1].Value.ToString(), Difficulty = int.Parse(worksheet.Cells[i, 2].Value.ToString())
                            };


                            question.Answers.Add(ConvertToQuestionAnswer(worksheet.Cells[i, 3].Value?.ToString()));
                            question.Answers.Add(ConvertToQuestionAnswer(worksheet.Cells[i, 4].Value?.ToString()));
                            question.Answers.Add(ConvertToQuestionAnswer(worksheet.Cells[i, 5].Value?.ToString()));
                            question.Answers.Add(ConvertToQuestionAnswer(worksheet.Cells[i, 6].Value?.ToString()));
                            question.Answers.Add(ConvertToQuestionAnswer(worksheet.Cells[i, 7].Value?.ToString()));

                            quiz.Questions.Add(question);

                            db.Entry(question).State = EntityState.Added;
                        }
                        catch
                        {
                            Debug.Write("Skip question №" + i);
                        }
                    }
                }

                quiz.AnswerText = Quiz.SerializeAnswers(quiz.Questions.ToList());

                db.Entry(quiz).State = EntityState.Added;

                user.Quizs.Add(quiz);

                db.Entry(user).State = EntityState.Modified;

                await db.SaveChangesAsync();
            }


            return(RedirectToAction("Index"));
        }