using Microsoft.EntityFrameworkCore;
using Modules.Library.Application.Gateways;
using Modules.Library.Application.Models.Anime;
using Modules.Library.Database.GatewaysImplementations.Converters;
using Modules.Library.Database.Repositories;
using Modules.Library.Domain.Entities.MediaContent.Items.Anime;

namespace Modules.Library.Database.GatewaysImplementations;

public class AnimeSeasonGateway(AnimeTitleRepository titleRepository, AnimeSeasonRepository seasonRepository,
    LanguageRepository languageRepository, GenreRepository genreRepository) : IAnimeSeasonGateway
{
    private readonly AnimeSeasonConverter _converter = new(languageRepository, genreRepository);

    //public async Task<List<Title>> GetList()
    //{
    //    var list = await titleRepository.GetAllAsync();
    //    var result = new List<Title>();
    //    foreach (var title in list)
    //    {
    //        result.Add(await _converter.Convert(title));
    //    }

    //    return result;
    //}

    //public async Task<Title> GetDetail(Guid id)
    //{
    //    var dbTitle = await animeTitleRepository.GetFirstWhere(q => q.Id == id && !q.Deleted);
    //    return await _converter.Convert(dbTitle);
    //}

    public Task<Guid> Create(Guid titleId, AnimeSeason season)
    {
        return seasonRepository.AddAsync(titleId, _converter.Convert(season));
    }

    //public async Task<AnimeTitle> GetById(Guid id)
    //{
    //    var dbTitle = await animeTitleRepository.GetFirstWhere(q => q.Id == id && !q.Deleted);
    //    /*
    //    var commonPropertiesBuilder = new CommonPropertiesBuilder()
    //        .SetAnouncementDate(dbTitle.CommonProperties.AnnouncementDate)
    //        .SetEstimatedReleaseDate(dbTitle.CommonProperties.EstimatedReleaseDate)
    //        .SetReleaseDate(dbTitle.CommonProperties.ReleaseDate);
    //    foreach (var q in dbTitle.CommonProperties.Names)
    //    {
    //        var dbLanguage = await languageRepository.GetLanguage(q.LanguageId);
    //        var languageBuilder = new LanguageBuilder(dbLanguage.CodeIso2, dbLanguage.Name);
    //        if (dbLanguage.IconId.HasValue) languageBuilder.WithIcon(dbLanguage.IconId.Value);
    //        commonPropertiesBuilder.AddName(languageBuilder.Build(), (Domain.Entities.MediaContent.CommonProperties.NameType)q.Type, q.Value);
    //    }
    //    /*
    //    foreach (var q in dbTitle.CommonProperties.Genres)
    //    {
    //        var dbLanguage = await languageRepository.GetLanguage(q.LanguageId);
    //        var languageBuilder = new LanguageBuilder(dbLanguage.CodeIso2, dbLanguage.Name);
    //        if (dbLanguage.IconId.HasValue) languageBuilder.WithIcon(dbLanguage.IconId.Value);
    //        commonPropertiesBuilder.AddName(languageBuilder.Build(), (Domain.Entities.MediaContent.CommonProperties.NameType)q.Type, q.Value);
    //    }
    //    */
    //    /*
    //    foreach (var q in dbTitle.CommonProperties.Descriptions)
    //    {
    //        var dbLanguage = await languageRepository.GetLanguage(q.LanguageId);
    //        var languageBuilder = new LanguageBuilder(dbLanguage.CodeIso2, dbLanguage.Name);
    //        if (dbLanguage.IconId.HasValue) languageBuilder.WithIcon(dbLanguage.IconId.Value);
    //        commonPropertiesBuilder.AddName(languageBuilder.Build(), (Domain.Entities.MediaContent.CommonProperties.NameType)q.Type, q.Value);
    //    }
    //    */
    //    /*
    //    foreach (var q in dbTitle.CommonProperties.RelatedContent)
    //    {
    //        var dbLanguage = await languageRepository.GetLanguage(q.LanguageId);
    //        var languageBuilder = new LanguageBuilder(dbLanguage.CodeIso2, dbLanguage.Name);
    //        if (dbLanguage.IconId.HasValue) languageBuilder.WithIcon(dbLanguage.IconId.Value);
    //        commonPropertiesBuilder.AddName(languageBuilder.Build(), (Domain.Entities.MediaContent.CommonProperties.NameType)q.Type, q.Value);
    //    }
    //    */
    //    /*
    //    if (dbTitle.CommonProperties.Preview != null)
    //        commonPropertiesBuilder.SetPreview(dbTitle.CommonProperties.Preview.Url, (Domain.Entities.MediaInfoType)dbTitle.CommonProperties.Preview.Type);
    //    var builder = new AnimeTitleBuilder()
    //        .SetCommonProperties(commonPropertiesBuilder)
    //        .SetExpirationTime(dbTitle.ExpirationTime)
    //        .SetCompleted(dbTitle.Completed)
    //        .SetDeleted(dbTitle.Deleted);
    //    return builder.SetId(dbTitle.Id).Build();
    //    */
    //    return await _converter.ConvertToDomain(dbTitle);
    //}

    public async Task Update(Guid titleId, AnimeSeason season) => await seasonRepository.UpdateAsync(titleId, _converter.Convert(season));

    public async Task Delete(Guid titleId, AnimeSeason season)
    {
        await seasonRepository.DeleteAsync(titleId, season.Id);
    }

    public Task<Guid> AddSeason(Guid titleId, AnimeSeason season)
    {
        throw new NotImplementedException();
    }


    /*
    public async Task AddAsync(Domain.Entities.MediaContent.Items.Anime.AnimeTitle entity, IUser user) =>
        await AddAsync(ToDbConverter.Title(entity));

    public Task<bool> AnyWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        var p = predicate.
    }

    public Task<bool> DeleteAsync(Domain.Entities.MediaContent.Items.Anime.AnimeTitle entity, IUser user)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle> GetByIdAsync(string id)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle?> GetByIdOrDefaultAsync(string id)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle?> GetFirstOrDefaultWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle> GetFirstWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        throw new NotImplementedException();
    }

    public Task<List<Domain.Entities.MediaContent.Items.Anime.AnimeTitle>> GetRangeByIdsAsync(List<string> ids)
    {
        throw new NotImplementedException();
    }

    public Task<List<Domain.Entities.MediaContent.Items.Anime.AnimeTitle>> GetWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        throw new NotImplementedException();
    }

    public Task<bool> UpdateAsync(Domain.Entities.MediaContent.Items.Anime.AnimeTitle entity, IUser user)
    {
        throw new NotImplementedException();
    }

    Task<List<Domain.Entities.MediaContent.Items.Anime.AnimeTitle>> Application.Gateways.IRepository<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, string>.GetAllAsync()
    {
        throw new NotImplementedException();
    }
    */
}