using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model;
using MediaBrowser.Model.MediaSegments;
namespace ConfusedPolarBear.Plugin.IntroSkipper.Providers
{
///
/// Introskipper media segment provider.
///
public class SegmentProvider : IMediaSegmentProvider
{
private readonly int _remainingSecondsOfIntro;
///
/// Initializes a new instance of the class.
///
public SegmentProvider()
{
_remainingSecondsOfIntro = Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2;
}
///
public string Name => Plugin.Instance!.Name;
///
public Task> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken)
{
var segments = new List();
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue))
{
segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(introValue.Start).Ticks,
EndTicks = TimeSpan.FromSeconds(introValue.End - _remainingSecondsOfIntro).Ticks,
ItemId = request.ItemId,
Type = MediaSegmentType.Intro
});
}
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue))
{
segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks,
EndTicks = TimeSpan.FromSeconds(creditValue.End - _remainingSecondsOfIntro).Ticks,
ItemId = request.ItemId,
Type = MediaSegmentType.Outro
});
}
return Task.FromResult>(segments);
}
///
public ValueTask Supports(BaseItem item) => ValueTask.FromResult(item is Episode);
}
}