// GET: Tests
        public async Task <IActionResult> Index(int Id)
        {
            var currentUser = await GetCurrentUserAsync();

            // Create a list of Responses that include UserResponses, UserTests, User and Questions that include their QuestionTypes
            // where the TestId of the Questions equal the Id passed in from the TestList view
            List <Response> responses = await _context.Response
                                        .Include(r => r.Rating)
                                        .Include(r => r.UserTest)
                                        .ThenInclude(ut => ut.User)
                                        .Include(r => r.Question)
                                        .ThenInclude(q => q.QuestionType)
                                        .Where(r => r.Question.TestId == Id && r.UserTest.User == currentUser)
                                        .ToListAsync();



            // Create a list of QuestionTypes
            // Select the QuestionTypes available through the Questions on the responses
            // Make the selected QuestionTypes distinct so they don't repeat
            // Order QuestionTypes by QuestionTypeId
            List <QuestionType> questionTypes = responses.Select(r => r.Question.QuestionType).Distinct().OrderBy(qt => qt.QuestionTypeId).ToList();

            // Create a list of UserTests that include the Test Data
            List <UserTest> userTests = await _context.UserTest
                                        .Include(ut => ut.Test)
                                        .Where(ut => ut.TestId == Id && ut.User == currentUser)
                                        .ToListAsync();

            // Create instance of ResponseDataViewModel
            List <ResponseDataViewModel> responseData = new List <ResponseDataViewModel>();

            // Loop over all question types in the QuestionTypes list
            foreach (QuestionType qt in questionTypes)
            {
                // Create a new instance of response data view model
                ResponseDataViewModel rd = new ResponseDataViewModel();
                // Give QuestionType in view model instance the value Type from the QuestionTypes list being looped over
                rd.QuestionType = qt.Type;
                // Create a variable containing a list of responses
                // where the QuestionTypeId associated with the question on the response
                // equals the QuestionTypeId on the list of QuestionTypes being looped over
                var totalResponses = responses.Where(r => r.Question.QuestionTypeId == qt.QuestionTypeId);
                // Create a new integer variable named number
                double number = new double();
                // Loop over list of responses held in totalResposnes variable
                foreach (var r in totalResponses)
                {
                    // Add a UserResponseId value to the number variable for each interation of the loop
                    number += r.Rating.RatingAmount;
                }
                // Divide the number variable by the amount of totalResponses and subtract 1
                number = (number / totalResponses.Count());

                number = Math.Round(number, 2);

                // Set the NumberOfResponses variable in the view model to the value of the number variable
                rd.NumberOfResponses = number;
                // Add the instance of the view model to the list of responseData view models
                responseData.Add(rd);
            }

            Test test = await _context.Test.FirstOrDefaultAsync(t => t.TestId == Id);

            // Create instance of IndexChartViewModel
            IndexChartViewModel viewModel = new IndexChartViewModel();

            // Add values to the view model based on variables above

            viewModel.Test          = test;
            viewModel.UserTest      = userTests;
            viewModel.Responses     = responses;
            viewModel.QuestionTypes = questionTypes;
            viewModel.ResponseData  = responseData;

            // Create a list of data points
            List <DataPoint> dataPoints = new List <DataPoint>();

            // Loop over each instance of a view model in the responseData view model list
            foreach (var rd in responseData)
            {
                // Add a new datapoint for each iteration
                // passing in the values of the QuestionType and NumberOfResponses from the view model
                dataPoints.Add(new DataPoint(rd.QuestionType, rd.NumberOfResponses));
            }

            // Magic
            ViewBag.DataPoints = JsonConvert.SerializeObject(dataPoints);

            // Return the viewModel as the view
            return(View(viewModel));
        }
        // GET: Tests/Details/5
        // Pass in an integer that can be null to represent an Id
        public async Task <IActionResult> Details(int?Id)
        {
            // If no Id integer is returned throw a 404 error message
            if (Id == null)
            {
                return(NotFound());
            }

            // Create variable for all UserTests in database
            var UserTestDisplay = await _context.UserTest
                                  .Include(ut => ut.Test)
                                  // Select only the UserTests whose Id equals the Id integer passed into the view
                                  .FirstOrDefaultAsync(ut => ut.UserTestId == Id);

            // If no UserTests are returned with a UserTestId that equals the Id integer return 404 error
            if (UserTestDisplay == null)
            {
                return(NotFound());
            }

            var currentUser = await GetCurrentUserAsync();

            // Create a list of Responses that include UserResponses and Questions that include their QuestionTypes
            // Where the UserTestId associated with the responses
            // Equals the UserTestId associated with the UserTestDisplay variable
            List <Response> responses = await _context.Response
                                        .Include(r => r.Rating)
                                        .Include(r => r.UserTest)
                                        .ThenInclude(ut => ut.User)
                                        .Include(r => r.Question)
                                        .ThenInclude(q => q.QuestionType)
                                        .Where(r => r.UserTestId == UserTestDisplay.UserTestId && r.UserTest.User == currentUser)
                                        .OrderBy(r => r.Question.QuestionId)
                                        .ToListAsync();


            // Create a list of QuestionTypes
            // Select the QuestionTypes available through the Questions on the responses
            // Make the selected QuestionTypes distinct so they don't repeat
            // Order QuestionTypes by QuestionTypeId
            List <QuestionType> QuestionTypes = responses.Select(r => r.Question.QuestionType).Distinct().ToList();

            // Create a new list of ResponseDataViewModels
            List <ResponseDataViewModel> responseData = new List <ResponseDataViewModel>();

            // Loop over all question types in the QuestionTypes list
            foreach (QuestionType qt in QuestionTypes)
            {
                // Create a new instance of response data view model
                ResponseDataViewModel rd = new ResponseDataViewModel();
                // Give QuestionType in view model instance the value Type from the QuestionTypes list being looped over
                rd.QuestionType = qt.Type;
                // Create a variable containing a list of responses
                // where the QuestionTypeId associated with the question on the response
                // equals the QuestionTypeId on the list of QuestionTypes being looped over
                var totalResponses = responses.Where(r => r.Question.QuestionTypeId == qt.QuestionTypeId);
                // Create a new integer variable named number
                double number = new double();
                // Loop over list of responses held in totalResposnes variable
                foreach (var r in totalResponses)
                {
                    // Add a UserResponseId value to the number variable for each interation of the loop
                    number += r.Rating.RatingAmount;
                }
                // Divide the number variable by the amount of totalResponses and subtract 1
                number = (number / totalResponses.Count());

                number = Math.Round(number, 2);

                // Set the NumberOfResponses variable in the view model to the value of the number variable
                rd.NumberOfResponses = number;
                // Add the instance of the view model to the list of responseData view models
                responseData.Add(rd);
            }

            // Create new instance of TestDetailsViewModel
            TestDetailsViewModel TestDetails = new TestDetailsViewModel();

            // Add values to the view model based on variables above
            TestDetails.UserTest      = UserTestDisplay;
            TestDetails.QuestionTypes = QuestionTypes;
            TestDetails.Responses     = responses;
            TestDetails.ResponseData  = responseData;

            // Create a new list of datapoints
            List <DataPoint> dataPoints = new List <DataPoint>();

            // Loop over each instance of a view model in the responseData view model list
            foreach (var rd in responseData)
            {
                // Add a new datapoint for each iteration
                // passing in the values of the QuestionType and NumberOfResponses from the view model
                dataPoints.Add(new DataPoint(rd.QuestionType, rd.NumberOfResponses));
            }

            // Magic
            ViewBag.DataPoints = JsonConvert.SerializeObject(dataPoints);

            // Return TestDetails to the View
            return(View(TestDetails));
        }