using Microsoft.Extensions.Logging;
using YTExtractor.Services.YoutubeExplode;

namespace YTExtractor.Services.Handlers
{
    public class YoutubeExplodeHandler : YoutubeServiceHandler
    {
        private readonly YoutubeExplodeClient _youtubeExplodeClient;
        private readonly VttFixerService _vttFixerService;

        public YoutubeExplodeHandler(
            ILogger<YoutubeExplodeHandler> logger, 
            YoutubeExplodeClient youtubeExplodeClient,
            VttFixerService vttFixerService) : base(logger)
        {
            _youtubeExplodeClient = youtubeExplodeClient;
            _vttFixerService = vttFixerService;
        }

        public override async Task<YtDlpInfo> HandleVideoInfo(string url, string workingDir)
        {
            try
            {
                _logger.LogInformation("Getting video info using YoutubeExplode for {Url}", url);
                
                var videoInfo = await _youtubeExplodeClient.GetVideoInfoAsync(url);
                
                if (videoInfo != null && !string.IsNullOrEmpty(videoInfo.Title))
                {
                    _logger.LogInformation("Successfully retrieved video info using YoutubeExplode for {Url}", url);
                    return videoInfo;
                }
                
                _logger.LogInformation("No video info found with YoutubeExplode, passing to next handler for {Url}", url);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Error getting video info with YoutubeExplode, passing to next handler for {Url}", url);
            }
            
            // Pass to the next handler if YoutubeExplode fails or returns no data
            if (_nextHandler != null)
            {
                return await _nextHandler.HandleVideoInfo(url, workingDir);
            }
            
            throw new Exception("Failed to get video info. No more handlers available.");
        }
        
        public override async Task<string> HandleSubtitles(string url, string language, string workingDir)
        {
            try
            {
                _logger.LogInformation("Getting subtitles using YoutubeExplode for {Url} in language {Language}", url, language);
                
                var subtitles = await _youtubeExplodeClient.GetSubtitlesAsync(url, language);
                
                if (!string.IsNullOrEmpty(subtitles))
                {
                    _logger.LogInformation("Successfully retrieved subtitles using YoutubeExplode for {Url}", url);
                    
                    // Fix the subtitles with VttFixer
                    var fixedSubtitles = _vttFixerService.FixYoutubeVtt(subtitles);
                    return fixedSubtitles;
                }
                
                _logger.LogInformation("No subtitles found with YoutubeExplode, passing to next handler for {Url}", url);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Error getting subtitles with YoutubeExplode, passing to next handler for {Url}", url);
            }
            
            // Pass to the next handler if YoutubeExplode fails or returns no data
            if (_nextHandler != null)
            {
                return await _nextHandler.HandleSubtitles(url, language, workingDir);
            }
            
            throw new Exception("Failed to get subtitles. No more handlers available.");
        }
    }
}