using Modules.Library.Domain.Entities.MediaContent.Items.Anime;

namespace Modules.Library.Domain.EntityBuilders;

public class AnimeSeasonBuilder : AnimeItemBuilder<AnimeSeason>
{
    private static readonly Func<AnimeSeason> CreateInstanceFunc = CreateInstanceFunction();
    private static readonly Action<AnimeSeasonBuilder, AnimeSeason> SetInstanceFieldsAction = SetInstanceFieldsAction<AnimeSeasonBuilder>();
    private static readonly Action<AnimeSeasonBuilder, AnimeSeason> AddTitleFieldAction = AddFields([nameof(_title)]);

    private List<AnimeEpisodeBuilder> _episodeBuilders = [];
    private ushort? _order = null;
    private ushort? _variant = null;
    private bool? _completed = null;
    private TimeSpan _expirationTime = TimeSpan.Zero;
    private List<AnimeEpisode> _episodes = [];
    private string? _director = null;
    private string? _originCountry = null;

    //public AnimeSeasonBuilder(AnimeTitleBuilder titleBuilder) : base(titleBuilder)
    //{
    //}

    /*
    public AnimeSeasonBuilder SetVariant(ushort value)
    {
        _variant = value;
        return this;
    }
    */

    public AnimeSeasonBuilder SetDirector(string? value)
    {
        _director = value;
        return this;
    }

    public AnimeSeasonBuilder SetOriginCountry(string? value)
    {
        _originCountry = value;
        return this;
    }

    public AnimeSeasonBuilder AddEpisode(AnimeEpisodeBuilder builder)
    {
        _episodeBuilders.Add(builder);
        return this;
    }

    /*
    internal static AnimeSeasonBuilder FromAnimeSeason(AnimeSeason season)
    {
        var builder = new AnimeSeasonBuilder();
        builder.SetCommonProperties(CommonPropertiesBuilder.FromCommonProperties(season.CommonProperties));
        builder.SetOrder(season.Order);
        builder.SetVariant(season.Variant);
        builder.SetNumber(season.Number);
        builder.SetDirector(season.Director);
        builder.SetOriginCountry(season.OriginCountry);
        builder.SetCompleted(season.Completed);
        builder.SetDeleted(season.Deleted);
        builder.SetExpirationTime(season.ExpirationTime);
        foreach (var episode in season.Episodes) { builder.AddEpisode(AnimeEpisodeBuilder.FromAnimeEpisode(episode)); }
        return builder;
    }
    */

    public override AnimeSeason Build() => throw new NotImplementedException();

    internal AnimeSeason BuildObject(AnimeTitle animeTitle)
    {
        base.BuildLoaclObjects();
        var season = CreateInstanceFunc();
        _title = animeTitle;
        _episodeBuilders.ForEach(q => _episodes.Add(q.BuildObject(_title, season)));
        SetInstanceFieldsAction(this, season);
        AddTitleFieldAction(this, season);
        return season;
    }
}