using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Infrastructure.DataAccess;
using TestCasePlanner.Infrastructure;
using Infrastructure.Models;
using TestCasePlanner.Models.ViewModels;
using Infrastructure.Services;
using Infrastructure.Interfaces;

namespace TestCasePlanner.Controllers
{
    public class DashboardController : AppBaseController
    {
        public DashboardController() : this(new TestCaseService(), new TestProjectService())
        {
        }

        public DashboardController(ITestCaseService testCaseService, ITestProjectService testProjectService)
        {
            _testCaseService = testCaseService;
            _testProjectService = testProjectService;
        }

        public ITestCaseService _testCaseService { get; private set; }
        public ITestProjectService _testProjectService { get; private set; }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(ProjectDropdownViewModel model)
        {
            if (ModelState.IsValid)
            {
                _testProjectService.SetProject(model.SelectedProjectID);
            }

            return View();
        }

        [ChildActionOnly]
        public ActionResult PopulateProjectsDropdown()
        {
            var projects = Context.TestProjects;
            
            Mapper.CreateMap<TestProject, ProjectDropdownViewModel>().ForMember(x => x.SelectedProjectID, opt => opt.Ignore());
            Mapper.AssertConfigurationIsValid();

            var viewModel = Mapper.Map<IEnumerable<TestProject>, IEnumerable<ProjectDropdownViewModel>>(projects);
            
            return PartialView("_ProjectsDropdownPartial", viewModel);
        }

        [ChildActionOnly]
        public ActionResult GetSelectedProject()
        {
            return Content(SelProject.Project.Name);
        }

        [ChildActionOnly]
        public ActionResult ProjectTestCasesWidget()
        {
            var testCases = Context.TestCases.Where(i => i.ProjectID == SelProject.Project.ID);

            Mapper.CreateMap<TestCase, ProjectTestCasesViewModel>();
            Mapper.AssertConfigurationIsValid();
            
            var viewModel = Mapper.Map<IEnumerable<TestCase>, IEnumerable<ProjectTestCasesViewModel>>(testCases);
            return PartialView("_ProjectTestCasesPartial", viewModel);
        }

        [ChildActionOnly]
        public ActionResult ProjectTestPlansWidget()
        {
            var testPlans = Context.TestPlans.Where(i => i.ProjectID == SelProject.Project.ID);

            Mapper.CreateMap<TestPlan, ProjectTestPlansViewModel>().ForMember(dest => dest.CreatorName, opt => opt.MapFrom(src => src.Creator.UserName));
            Mapper.AssertConfigurationIsValid();

            var viewModel = Mapper.Map<IEnumerable<TestPlan>, IEnumerable<ProjectTestPlansViewModel>>(testPlans);
            return PartialView("_ProjectTestPlansPartial", viewModel);
        }    
    }
}