using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using IntroSkipper.Data;
using MediaBrowser.Controller;
using MediaBrowser.Model;
using Microsoft.Extensions.Logging;
namespace IntroSkipper.Manager
{
///
/// Initializes a new instance of the class.
///
/// MediaSegmentManager.
/// logger.
/// segmentProvider.
public class MediaSegmentUpdateManager(IMediaSegmentManager mediaSegmentManager, ILogger logger, IMediaSegmentProvider segmentProvider)
{
private readonly IMediaSegmentManager _mediaSegmentManager = mediaSegmentManager;
private readonly ILogger _logger = logger;
private readonly IMediaSegmentProvider _segmentProvider = segmentProvider;
private readonly string _name = Plugin.Instance!.Name;
///
/// Updates all media items in a List.
///
/// Queued media items.
/// CancellationToken.
/// A representing the asynchronous operation.
public async Task UpdateMediaSegmentsAsync(IReadOnlyList episodes, CancellationToken cancellationToken)
{
foreach (var episode in episodes)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var existingSegments = await _mediaSegmentManager.GetSegmentsAsync(episode.EpisodeId, null).ConfigureAwait(false);
await Task.WhenAll(existingSegments.Select(s => _mediaSegmentManager.DeleteSegmentAsync(s.Id))).ConfigureAwait(false);
var newSegments = await _segmentProvider.GetMediaSegments(new MediaSegmentGenerationRequest { ItemId = episode.EpisodeId }, cancellationToken).ConfigureAwait(false);
if (newSegments.Count == 0)
{
_logger.LogDebug("No segments found for episode {EpisodeId}", episode.EpisodeId);
continue;
}
await Task.WhenAll(newSegments.Select(s => _mediaSegmentManager.CreateSegmentAsync(s, _name))).ConfigureAwait(false);
_logger.LogDebug("Updated {SegmentCount} segments for episode {EpisodeId}", newSegments.Count, episode.EpisodeId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing episode {EpisodeId}", episode.EpisodeId);
}
}
}
}
}