using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MultipleChoiceTrainer.Data;
using MultipleChoiceTrainer.Models;
using MultipleChoiceTrainer.Models.DataModels;

namespace MultipleChoiceTrainer.Controllers
{
    public class QuizController : Controller
    {
        private readonly ApplicationDbContext _context;
        private Random _rnd = new Random();

        public QuizController(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult Section(int id)
        {
            var vm = new QuizViewModel()
            {
                QuizType = QuizType.Section,
                CurrentTypeId = id
            };

            GetQuestion(vm);

            return Quiz(vm);
        }

        public IActionResult Category(int id)
        {
            var vm = new QuizViewModel()
            {
                QuizType = QuizType.Category,
                CurrentTypeId = id
            };
            GetQuestion(vm);

            return Quiz(vm);
        } 

        private void GetQuestion(QuizViewModel vm)
        {
            var questions = _context.Questions.Include(e => e.Section).ThenInclude(e => e.Category).Include(e => e.Choices).Include(e => e.Answers).AsQueryable();

            switch(vm.QuizType)
            {
                case QuizType.Category:
                    questions = questions.Where(e => e.Section.CategoryId == vm.CurrentTypeId);
                    break;
                case QuizType.Section:
                    questions = questions.Where(e => e.SectionId == vm.CurrentTypeId);
                    break;
            }

            questions = questions.ToList().AsQueryable();
            var newestDate = questions.Select(e => e.CreationDate).Max();
            questions = questions.OrderByDescending(e => e.Answers.Where(a => a.Date > newestDate).Count());
            var minAnswerCount = questions.Select(e => e.Answers.Where(a => a.Date > newestDate).Count()).Min();
            questions = questions.Where(e => e.Answers.Where(a => a.Date > newestDate).Count() == minAnswerCount);
            //questions = questions.Take(10);

            vm.CurrentQuestion = questions.ElementAt(_rnd.Next(0, questions.Count()-1));
            vm.Choices = vm.CurrentQuestion.Choices.Select(oc => new Choice() { Id = oc.Id, Text = oc.Text }).OrderBy(a => Guid.NewGuid()).ToList();
        }

        public IActionResult Quiz(QuizViewModel viewModel)
        {
            var newViewModel = new QuizViewModel()
            {
                QuizType = viewModel.QuizType,
                CurrentTypeId = viewModel.CurrentTypeId
            }; 

            if (string.IsNullOrEmpty(viewModel.CurrentQuestion.Text))
            {
                var refQuestion = _context.Questions.Include(e => e.Choices).First(q => q.Id == viewModel.CurrentQuestion.Id);
                newViewModel.Evaluations = new List<Evaluation>();
                newViewModel.PreviousQuestion = refQuestion.Text;
                newViewModel.PreviousQuestionImage = refQuestion.Image;

                foreach(var answer in viewModel.Choices)
                {
                    var refChoice = refQuestion.Choices.First(rc => rc.Id == answer.Id);
                    newViewModel.Evaluations.Add(new Evaluation()
                    {
                        Text = refChoice.Text,
                        GivenAnswer = answer.IsTrue,
                        RightAnswer = refChoice.IsTrue,
                    }); 
                }

                var dbAnswer = new Answer()
                {
                    Date = DateTime.Now,
                    QuestionId = viewModel.CurrentQuestion.Id,
                    Successfull = newViewModel.PassedPreviousQuestion
                };
                refQuestion.Answers.Add(dbAnswer);
                _context.SaveChanges();
            }

            GetQuestion(newViewModel);
            ModelState.Clear();
            return View("quiz", newViewModel);
        }
    }
}