using System.Collections.Generic;
using System.Linq;
using API.Comparators;
using API.Entities;
namespace API.Extensions;
public static class SeriesExtensions
{
    /// 
    /// Calculates the Cover Image for the Series
    /// 
    /// 
    /// 
    /// This is under the assumption that the Volume already has a Cover Image calculated and set
    public static string? GetCoverImage(this Series series)
    {
        var volumes = series.Volumes ?? new List();
        var firstVolume = volumes.GetCoverImage(series.Format);
        if (firstVolume == null) return null;
        string? coverImage = null;
        var chapters = firstVolume.Chapters.OrderBy(c => double.Parse(c.Number), ChapterSortComparerZeroFirst.Default).ToList();
        if (chapters.Count > 1 && chapters.Any(c => c.IsSpecial))
        {
            coverImage = chapters.FirstOrDefault(c => !c.IsSpecial)?.CoverImage ?? chapters.First().CoverImage;
            firstVolume = null;
        }
        return firstVolume?.CoverImage ?? coverImage;
    }
}