using Modules.Library.Database.Database.Models.Anime;
using Modules.Library.Database.Repositories;
namespace Modules.Library.Database.GatewaysImplementations.Converters;

internal class AnimeEpisodeConverter
{
    private readonly CommonPropertiesConverter _commonPropertiesConverter;

    internal AnimeEpisodeConverter(CommonPropertiesConverter commonPropertiesConverter)
    {
        _commonPropertiesConverter = commonPropertiesConverter;
    }

    internal AnimeEpisodeConverter(LanguageRepository languageRepository, GenreRepository genreRepository)
    {
        _commonPropertiesConverter = new CommonPropertiesConverter(languageRepository, genreRepository);
    }


    internal async Task<Application.Models.Anime.Episode> Convert(AnimeEpisode episode)
    {
        return new Application.Models.Anime.Episode
        {
            Id = episode.Id,
            Deleted = episode.Deleted,
            Completed = episode.Completed,

            Type = (Application.Models.AnimeEpisodeType)episode.Type,
            
            CommonProperties = await _commonPropertiesConverter.Convert(episode.CommonProperties),
            Number = episode.Number,
            Order = episode.Order,
            Variant = episode.Variant,
            ExpirationTime = episode.ExpirationTime,
            
            Duration = episode.Duration,
            DurationType = (Application.Models.AnimeItemSingleDurationType)episode.DurationType,
        };
    }

    internal AnimeEpisode Convert(Domain.Entities.MediaContent.Items.Anime.AnimeEpisode episode)
    {
        var dbEpisode = new AnimeEpisode
        {
            Deleted = episode.Deleted,
            Completed = episode.Completed,

            Type = (AnimeEpisodeType)episode.Type,
            
            CommonProperties = _commonPropertiesConverter.Convert(episode.CommonProperties),
            Order = episode.Order,
            Variant = episode.Variant,
            Number = episode.Number,
            ExpirationTime = episode.ExpirationTime,

            Duration = episode.Duration,
            DurationType = (AnimeItemSingleDurationType)episode.DurationType,
        };
        if (!episode.IsTransient()) dbEpisode.Id = episode.Id;
        return dbEpisode;

    }

    internal async Task<Domain.Models.AnimeEpisode> ConvertToDomain(AnimeEpisode episode)
    {
        return new Domain.Models.AnimeEpisode
        {
            Id = episode.Id,
            Deleted = episode.Deleted,
            Completed = episode.Completed,

            Type = (Domain.Entities.MediaContent.Items.Anime.AnimeEpisodeType)episode.Type,

            CommonProperties = await _commonPropertiesConverter.ConvertToDomain(episode.CommonProperties),
            Number = episode.Number,
            Order = episode.Order,
            Variant = episode.Variant,
            ExpirationTime = episode.ExpirationTime,

            Duration = episode.Duration,
        };
    }
}