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

namespace Modules.Library.Domain.EntityBuilders;

public class AnimeEpisodeBuilder : AnimeItemBuilder<AnimeEpisode>
{
    private static readonly Func<AnimeEpisode> CreateInstanceFunc = CreateInstanceFunction();
    private static readonly Action<AnimeEpisodeBuilder, AnimeEpisode> SetInstanceFieldsAction = SetInstanceFieldsAction<AnimeEpisodeBuilder>();
    private static readonly Action<AnimeEpisodeBuilder, AnimeEpisode> AddTitleAndSeasonFieldsAction = AddFields([nameof(_title), nameof(_season)]);

    private AnimeSeason? _season = null;
    private TimeSpan? _duration = null;

    private ushort? _variant = null;
    private bool? _completed = null;
    private TimeSpan? _expirationTime = null;

    protected AnimeEpisodeType _type;

    public AnimeEpisodeBuilder SetVariant(ushort value)
    {
        _variant = value;
        return this;
    }

    public AnimeEpisodeBuilder SetDuration(TimeSpan value)
    {
        _duration = value;
        return this;
    }

    public AnimeEpisodeBuilder SetType(AnimeEpisodeType value)
    {
        _type = value;
        return this;
    }

    //internal static AnimeEpisodeBuilder FromAnimeEpisode(AnimeEpisode episode)
    //{
    //    var builder = new AnimeEpisodeBuilder();
    //    builder.SetCommonProperties(CommonPropertiesBuilder.FromCommonProperties(episode.CommonProperties));
    //    builder.SetOrder(episode.Order);
    //    builder.SetVariant(episode.Variant);
    //    if (episode.Duration.HasValue) builder.SetDuration(episode.Duration.Value);
    //    builder.SetType(episode.Type);
    //    builder.SetNumber(episode.Number);
    //    builder.SetCompleted(episode.Completed);
    //    builder.SetDeleted(episode.Deleted);
    //    builder.SetExpirationTime(episode.ExpirationTime);
    //    return builder;
    //}

    /*
    internal AnimeEpisodeShadow Build()
    {
        return episode;
    }
    */

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

    internal AnimeEpisode BuildObject(AnimeTitle title, AnimeSeason? season)
    {
        base.BuildLoaclObjects();
        var episode = CreateInstanceFunc();
        SetInstanceFieldsAction(this, episode);
        _title = title;
        _season = season;
        AddTitleAndSeasonFieldsAction(this, episode);
        return episode;
    }
}