using System;
using System.Linq;
using AutoMapper;
using Qusion.Cms.Modules.Events;
using Qusion.Cms.Modules.Events.Core;
using Qusion.Cms.Users.Core;

namespace Qusion.WebApp.Areas.Qusion.Models.Events
{
    public class EventProfile : Profile
    {
        private readonly EventService _eventService;
        private readonly UserService _userService;

        public EventProfile(UserService userService, EventService eventService)
        {
            _userService = userService;
            _eventService = eventService;
        }

        protected override void Configure()
        {
            Mapper.CreateMap<Event, EventListModel>();

            Mapper.CreateMap<Event, EventModel>();

            Mapper.CreateMap<EventCreateInput, Event>()
                .ForMember(x => x.Labels, o => o.Ignore());

            Mapper.CreateMap<Event, EventCreateInput>();

            Mapper.CreateMap<EventEditInput, Event>()
                .ForMember(x => x.EventUsers, o => o.Ignore())
                 .ForMember(x => x.EventTeams, o => o.Ignore())
                .ForMember(x => x.Labels, o => o.Ignore());

            Mapper.CreateMap<Event, EventEditInput>();

            Mapper.CreateMap<EventUserInput, EventUser>()
                .ForMember(x => x.User, o => o.MapFrom(x => _userService.GetById(x.UserID)))
                .ForMember(x => x.Event, o => o.MapFrom(x => _eventService.GetById(x.EventID)))
                .ForMember(x => x.Labels, o => o.Ignore())
                .AfterMap(UserStatuses);

            Mapper.CreateMap<EventUser, EventUserEntry>()
                .ForMember(x => x.UserID, o => o.MapFrom(x => x.User.GuidID));

            Mapper.CreateMap<EventUserEdit, EventUser>()
                .ForMember(x => x.User, o => o.MapFrom(x => _userService.GetById(x.UserID)))
                .ForMember(x => x.Labels, o => o.Ignore())
                .AfterMap(EditUserStatuses);

            //
            Mapper.CreateMap<EventTeamInput, EventTeam>()
               .ForMember(x => x.Event, o => o.MapFrom(x => _eventService.GetById(x.EventID)))
               .ForMember(x => x.Labels, o => o.Ignore())
               .AfterMap(TeamStatuses);

            Mapper.CreateMap<EventTeam, EventTeamEntry>();

            Mapper.CreateMap<EventTeamEdit, EventTeam>()
                .ForMember(x => x.Labels, o => o.Ignore())
                .AfterMap(EditTeamStatuses);

            CreateMap<EventQueryInput, EventQuery>()
                .ConstructUsing(x => EventQuery.Default);
        }

        protected void TeamStatuses(EventTeamInput model, EventTeam team)
        {
            if (team.ApprovedOn.HasValue && !team.SuspendedOn.HasValue && !(team.ExpiresOn <= DateTime.Today))
                team.EventSubscriptionStatus = EventSubscriptionStatus.Approved;
            if (team.SuspendedOn.HasValue && !(team.ExpiresOn <= DateTime.Today))
                team.EventSubscriptionStatus = EventSubscriptionStatus.Suspended;
            if (team.ExpiresOn <= DateTime.Today)
                team.EventSubscriptionStatus = EventSubscriptionStatus.Declined;
            if (team.StartsOn > DateTime.Today && !team.SuspendedOn.HasValue && !(team.ExpiresOn <= DateTime.Today))
                team.EventSubscriptionStatus = EventSubscriptionStatus.Pending;
        }

        protected void EditTeamStatuses(EventTeamEdit model, EventTeam team)
        {
            if (team.ApprovedOn.HasValue && !team.SuspendedOn.HasValue && !(team.ExpiresOn <= DateTime.Today))
                team.EventSubscriptionStatus = EventSubscriptionStatus.Approved;
            if (team.SuspendedOn.HasValue && !(team.ExpiresOn <= DateTime.Today))
                team.EventSubscriptionStatus = EventSubscriptionStatus.Suspended;
            if (team.ExpiresOn <= DateTime.Today)
                team.EventSubscriptionStatus = EventSubscriptionStatus.Declined;
            if (team.StartsOn > DateTime.Today && !team.SuspendedOn.HasValue && !(team.ExpiresOn <= DateTime.Today))
                team.EventSubscriptionStatus = EventSubscriptionStatus.Pending;
        }

        protected void UserStatuses(EventUserInput model, EventUser user)
        {
            if (user.ApprovedOn.HasValue && !user.SuspendedOn.HasValue && !(user.ExpiresOn <= DateTime.Today))
                user.EventSubscriptionStatus = EventSubscriptionStatus.Approved;
            if (user.SuspendedOn.HasValue && !(user.ExpiresOn <= DateTime.Today))
                user.EventSubscriptionStatus = EventSubscriptionStatus.Suspended;
            if (user.ExpiresOn <= DateTime.Today)
                user.EventSubscriptionStatus = EventSubscriptionStatus.Declined;
            if (user.StartsOn > DateTime.Today && !user.SuspendedOn.HasValue && !(user.ExpiresOn <= DateTime.Today))
                user.EventSubscriptionStatus = EventSubscriptionStatus.Pending;
        }

        protected void EditUserStatuses(EventUserEdit model, EventUser user)
        {
            if (user.ApprovedOn.HasValue && !user.SuspendedOn.HasValue && !(user.ExpiresOn <= DateTime.Today))
                user.EventSubscriptionStatus = EventSubscriptionStatus.Approved;
            if (user.SuspendedOn.HasValue && !(user.ExpiresOn <= DateTime.Today))
                user.EventSubscriptionStatus = EventSubscriptionStatus.Suspended;
            if (user.ExpiresOn <= DateTime.Today)
                user.EventSubscriptionStatus = EventSubscriptionStatus.Declined;
            if (user.StartsOn > DateTime.Today && !user.SuspendedOn.HasValue && !(user.ExpiresOn <= DateTime.Today))
                user.EventSubscriptionStatus = EventSubscriptionStatus.Pending;
        }
    }
}