using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using YTExtractor.Services;

namespace YTExtractor.Services
{
    public class YouExposeService : IYoutubeService
    {
        private readonly ILogger<YouExposeService> _logger;
        private readonly YoutubeService _fallbackService;

        public YouExposeService(ILogger<YouExposeService> logger, YoutubeService fallbackService)
        {
            _logger = logger;
            _fallbackService = fallbackService;
        }

        public bool IsValidYouTubeUrl(string url)
        {
            return Regex.IsMatch(url, @"^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$");
        }

        public async Task<YtDlpInfo> GetVideoInfo(string url, string workingDir)
        {
            try
            {
                _logger.LogInformation("Attempting to get video info using YouExpose for {Url}", url);
                
                // TODO: Implement YouExpose video info retrieval
                // This is where you would use the YouExpose library to get video information
                // Example code (to be replaced with actual implementation):
                // var youExposeClient = new YouExposeClient();
                // var videoInfo = await youExposeClient.GetVideoInfoAsync(url);
                // return new YtDlpInfo(videoInfo.Title, videoInfo.ThumbnailUrl);
                
                // For now, fall back to yt-dlp
                _logger.LogInformation("YouExpose implementation not available, falling back to yt-dlp for {Url}", url);
                return await _fallbackService.GetVideoInfo(url, workingDir);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Error getting video info with YouExpose, falling back to yt-dlp for {Url}", url);
                return await _fallbackService.GetVideoInfo(url, workingDir);
            }
        }

        public async Task<string> GetSubtitles(string url, string language, string workingDir)
        {
            try
            {
                _logger.LogInformation("Attempting to get subtitles using YouExpose for {Url} in language {Language}", url, language);
                
                // TODO: Implement YouExpose subtitles retrieval
                // This is where you would use the YouExpose library to get subtitles
                // Example code (to be replaced with actual implementation):
                // var youExposeClient = new YouExposeClient();
                // var subtitles = await youExposeClient.GetSubtitlesAsync(url, language);
                // if (!string.IsNullOrEmpty(subtitles))
                //     return subtitles;
                
                // For now, fall back to yt-dlp
                _logger.LogInformation("YouExpose implementation not available, falling back to yt-dlp for {Url}", url);
                return await _fallbackService.GetSubtitles(url, language, workingDir);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Error getting subtitles with YouExpose, falling back to yt-dlp for {Url}", url);
                return await _fallbackService.GetSubtitles(url, language, workingDir);
            }
        }
    }
}