using AutoMapper;
using Infrastructure.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestCasePlanner.Models.ViewModels;
using TestCasePlanner.ViewModels;

namespace TestCasePlanner.App_Start
{
    public class MappingInitializer
    {
        public MappingInitializer()
		{
		}

        public void Start()
        {
            var mapEditProject = Mapper.CreateMap<EditTestProjectViewModel, TestProject>();
            mapEditProject.ForAllMembers(opt => opt.Ignore());
            mapEditProject.ForMember(m => m.Name, opt => opt.MapFrom(src => src.Name));
            mapEditProject.ForMember(m => m.ShortName, opt => opt.MapFrom(src => src.ShortName));
            mapEditProject.ForMember(m => m.Description, opt => opt.MapFrom(src => src.Description));
            mapEditProject.ForMember(m => m.ID, opt => opt.MapFrom(src => src.ID));
            Mapper.AssertConfigurationIsValid();
            
            var mapNewProject = Mapper.CreateMap<NewTestProjectViewModel, TestProject>();
            mapNewProject.ForAllMembers(opt => opt.Ignore());
            mapNewProject.ForMember(m => m.Name, opt => opt.MapFrom(src => src.Name));
            mapNewProject.ForMember(m => m.ShortName, opt => opt.MapFrom(src => src.ShortName));
            mapNewProject.ForMember(m => m.Description, opt => opt.MapFrom(src => src.Description));
            Mapper.AssertConfigurationIsValid();

            var mapProjectToView = Mapper.CreateMap<TestProject, EditTestProjectViewModel>();

            var mapNewTestCase = Mapper.CreateMap<TestCasePlanner.ViewModels.NewTestCaseViewModel, TestCase>();
            mapNewTestCase.ForAllMembers(opt => opt.Ignore());
            mapNewTestCase.ForMember(m => m.Title, opt => opt.MapFrom(src => src.Title));
            mapNewTestCase.ForMember(m => m.Description, opt => opt.MapFrom(src => src.Description));
            mapNewTestCase.ForMember(m => m.Estimate, opt => opt.MapFrom(src => src.Estimate));
            mapNewTestCase.ForMember(m => m.DateCreated, opt => opt.UseValue(DateTime.Now));
            mapNewTestCase.ForMember(m => m.TestSuiteID, opt => opt.MapFrom(src => src.SelectedTestSuiteID));
            Mapper.AssertConfigurationIsValid();

            var mapListOfTestCases = Mapper.CreateMap<TestCase, ListTestCasesViewModel>();
            mapListOfTestCases.ForAllMembers(opt => opt.Ignore());
            mapListOfTestCases.ForMember(m => m.ExternalID, opt => opt.MapFrom(src => src.ExternalID));
            mapListOfTestCases.ForMember(m => m.Title, opt => opt.MapFrom(src => src.Title));
            mapListOfTestCases.ForMember(m => m.ID, opt => opt.MapFrom(src => src.ID));
            Mapper.AssertConfigurationIsValid();

            var mapTestCaseDetails = Mapper.CreateMap<TestCase, DisplayTestCaseViewModel>();
            mapTestCaseDetails.ForMember(ts => ts.CreatedBy, opt => opt.MapFrom(src => src.Creator.UserName));
            mapTestCaseDetails.ForMember(ts => ts.EditedBy, opt => opt.MapFrom(src => src.Editor.UserName));
            Mapper.AssertConfigurationIsValid();

            var mapListOfTestSuites = Mapper.CreateMap<TestSuite, SelectTestSuiteViewModel>();
            mapListOfTestSuites.ForAllMembers(opt => opt.Ignore());
            mapListOfTestSuites.ForMember(ts => ts.ID, opt => opt.MapFrom(src => src.ID));
            mapListOfTestSuites.ForMember(ts => ts.Name, opt => opt.MapFrom(src => src.Name));
            Mapper.AssertConfigurationIsValid();

            var mapSteps = Mapper.CreateMap<AddStepsToTestCaseViewModel, StepResultDefinition>();
            mapSteps.ForAllMembers(opt => opt.Ignore());
            mapSteps.ForMember(s => s.Result, opt => opt.MapFrom(src => src.Result));
            mapSteps.ForMember(s => s.DateCreated, opt => opt.MapFrom(src => DateTime.Now));
            mapSteps.ForMember(s => s.Step, opt => opt.MapFrom(src => src.Step));
            Mapper.AssertConfigurationIsValid();
        }
    }
}