
using Modules.Library.Domain.Entities.MediaContent.CommonProperties;

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

public sealed class AnimeEpisode : AnimeItemSingle
{
    public AnimeEpisodeType Type { get; private set; }
    public int? Number { get; private set; }

    private AnimeEpisode(AnimeTitle title, AnimeSeason? season) : base(title, season) {}
    
    private AnimeEpisode(AnimeTitle title, AnimeSeason? season, Models.AnimeEpisode episode) : base(title, season, episode.Duration)
    {
        Id = episode.Id;
        CommonProperties = MediaContent.CommonProperties.CommonProperties.New(episode.CommonProperties);
        Deleted = episode.Deleted;
        Type = episode.Type;
        Number = episode.Number;
        Order = episode.Order;
        Variant = episode.Variant;
        Completed = episode.Completed;
        ExpirationTime = episode.ExpirationTime;
    }

    internal AnimeEpisode(AnimeTitle title, AnimeSeason? season, AnimeEpisodeType type, ushort order, ushort variant, int? number = null) : base(title, season)
    {
        Type = type;
        Order = order;
        Variant = variant;
        Number = number;
    }

    internal AnimeEpisode(AnimeTitle title, AnimeSeason? season, AnimeEpisodeType type,
        IEnumerable<NameItem> names, ushort order, ushort variant, int? number = null, TimeSpan? duration = null) :
        base(title, season, names, duration)
    {
        Type = type;
        Order = order;
        Variant = variant;
        Number = number;
    }

    internal static AnimeEpisode New(AnimeTitle title, Models.AnimeEpisode episode) => new(title, null, episode);

    internal static AnimeEpisode New(AnimeTitle title, AnimeSeason? season, Models.AnimeEpisode episode) => new(title, season, episode);

    public void SetNumber(int? number) => Number = number;

    public override void SetOrder(ushort order)
    {
        //structure action or not
        if (_season == null) SetItemOrder(_title.Items, Order, order);
        else SetItemOrder(_season.Episodes, Order, order);
    }

    public override void SetVariant(ushort variant)
    {
        if (_season == null) SetItemVariant(_title.Items, Order, Variant, variant);
        else SetItemVariant(_season.Episodes, Order, Variant, variant);
    }

    /*
    public override void Delete()
    {

    }
    */
    /*
    protected override IEnumerable<object?> GetEqualityComponents()
    {
        yield return Type;
        yield return Number;
        yield return base.GetEqualityComponents();
    }
    */

}

public enum AnimeEpisodeType
{
    Regular,
    Ova,
    Ona,
}