using AutoMapper;
using Modules.Library.WebApi.Models;
using Modules.Library.WebApi.Models.Views;
using Modules.Library.WebApi.Models.Views.Anime;
using System.Linq;

namespace Modules.Library.WebApi.Automapper;

public class AnimeTitleMapprigProfile : Profile
{
    public AnimeTitleMapprigProfile()
    {
        CreateMap<Application.Models.NameItem, NameItem>();
        CreateMap<Application.Models.MediaInfo, MediaInfo>();
        CreateMap<Application.Models.Description, Description>();
        CreateMap<Application.Models.GenreProportion, GenreProportion>();
        CreateMap<Application.Models.CommonProperties, CommonProperties>();

        

        CreateMap<Application.Models.Anime.Episode, Episode>()
            .ForMember(q => q.ExpirationTimeTicks, opt => opt.MapFrom(q => q.ExpirationTime.Ticks))
            .ForMember(q => q.DurationTicks, opt => opt.MapFrom(q => q.Duration.HasValue ? q.Duration.Value.Ticks : default));
        
        CreateMap<Application.Models.Anime.Season, Season>()
            .ForMember(q => q.ExpirationTimeTicks, opt => opt.MapFrom(q => q.ExpirationTime.Ticks));
        
        CreateMap<Application.Models.Anime.AnimeItem, AnimeItem>().ConstructUsing((s, c) =>
            {
                if (s is Application.Models.Anime.Episode episode) return c.Mapper.Map<Episode>(episode);
                else if (s is Application.Models.Anime.Season season) return c.Mapper.Map<Season>(season);
                else throw new NotImplementedException();
            });

        CreateMap<Application.Models.Anime.Title, Title>()
            .ForMember(q => q.ExpirationTimeTicks, opt => opt.MapFrom(q => q.ExpirationTime.Ticks))
            ;
            //.ForMember(q => q.EpisodesInsideSeasonsCount, opt => opt.MapFrom(q => q.Seasons
            //    .SelectMany(q => q.Episodes).Count()));
                //.SelectMany(q => q.Episodes.DefaultIfEmpty()).Count()));
    }
}