using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using API.Comparators;
using API.DTOs;
using API.Entities;
using API.Entities.Enums;
using API.Services.Tasks.Scanner.Parser;
namespace API.Extensions;
#nullable enable
public static class VolumeListExtensions
{
///
/// Selects the first Volume to get the cover image from. For a book with only a special, the special will be returned.
/// If there are both specials and non-specials, then the first non-special will be returned.
///
///
///
///
public static Volume? GetCoverImage(this IList volumes, MangaFormat seriesFormat)
{
if (volumes == null) throw new ArgumentException("Volumes cannot be null");
if (seriesFormat is MangaFormat.Epub or MangaFormat.Pdf)
{
return volumes.MinBy(x => x.MinNumber);
}
if (volumes.HasAnyNonLooseLeafVolumes())
{
return volumes.FirstNonLooseLeafOrDefault();
}
// We only have 1 volume of chapters, we need to be cautious if there are specials, as we don't want to order them first
return volumes.MinBy(x => x.MinNumber);
}
///
/// If the collection of volumes has any non-loose leaf volumes
///
///
///
public static bool HasAnyNonLooseLeafVolumes(this IEnumerable volumes)
{
return volumes.Any(v => v.MinNumber.IsNot(Parser.DefaultChapterNumber));
}
///
/// Returns first non-loose leaf volume
///
///
///
public static Volume? FirstNonLooseLeafOrDefault(this IEnumerable volumes)
{
return volumes.OrderBy(x => x.MinNumber, ChapterSortComparerDefaultLast.Default)
.FirstOrDefault(v => v.MinNumber.IsNot(Parser.DefaultChapterNumber));
}
///
/// Returns the first (and only) loose leaf volume or null if none
///
///
///
public static Volume? GetLooseLeafVolumeOrDefault(this IEnumerable volumes)
{
return volumes.FirstOrDefault(v => v.MinNumber.Is(Parser.DefaultChapterNumber));
}
///
/// Returns the first (and only) special volume or null if none
///
///
///
public static Volume? GetSpecialVolumeOrDefault(this IEnumerable volumes)
{
return volumes.FirstOrDefault(v => v.MinNumber.Is(Parser.SpecialVolumeNumber));
}
public static IEnumerable WhereNotLooseLeaf(this IEnumerable volumes)
{
return volumes.Where(v => v.MinNumber.Is(Parser.DefaultChapterNumber));
}
public static IEnumerable WhereLooseLeaf(this IEnumerable volumes)
{
return volumes.Where(v => v.MinNumber.Is(Parser.DefaultChapterNumber));
}
}