using VideoStudy.Shared;

namespace VideoStudy.Desktop.Services;

public class ScreenshotService
{
    private readonly FFmpegService _ffmpegService;

    public ScreenshotService(FFmpegService ffmpegService)
    {
        _ffmpegService = ffmpegService;
    }

    public async Task<string> CaptureScreenshotAsync(string videoPath, TimeSpan timestamp, string outputDirectory)
    {
        var fileName = $"screenshot_{timestamp.TotalSeconds:F0}.jpg";
        var outputPath = Path.Combine(outputDirectory, fileName);
        
        await _ffmpegService.ExtractFrameAsync(videoPath, timestamp, outputPath);
        
        return outputPath;
    }

    public async Task<List<string>> CaptureScreenshotsAsync(string videoPath, List<KeyMoment> moments, string outputDirectory)
    {
        var paths = new List<string>();
        foreach (var moment in moments)
        {
            try
            {
                // Parse timestamp string if needed, or use StartSeconds
                var time = TimeSpan.FromSeconds(moment.StartSeconds);
                var path = await CaptureScreenshotAsync(videoPath, time, outputDirectory);
                paths.Add(path);
            }
            catch
            {
                // Ignore failure for individual screenshots
            }
        }
        return paths;
    }
}
