using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
using TagLib;
namespace MediaBrowser.Providers.MediaInfo
{
    /// 
    /// Probes audio files for metadata.
    /// 
    public partial class AudioFileProber
    {
        // Default LUFS value for use with the web interface, at -18db gain will be 1(no db gain).
        private const float DefaultLUFSValue = -18;
        private readonly ILogger _logger;
        private readonly IMediaEncoder _mediaEncoder;
        private readonly IItemRepository _itemRepo;
        private readonly ILibraryManager _libraryManager;
        private readonly IMediaSourceManager _mediaSourceManager;
        private readonly LyricResolver _lyricResolver;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        public AudioFileProber(
            ILogger logger,
            IMediaSourceManager mediaSourceManager,
            IMediaEncoder mediaEncoder,
            IItemRepository itemRepo,
            ILibraryManager libraryManager,
            LyricResolver lyricResolver)
        {
            _logger = logger;
            _mediaEncoder = mediaEncoder;
            _itemRepo = itemRepo;
            _libraryManager = libraryManager;
            _mediaSourceManager = mediaSourceManager;
            _lyricResolver = lyricResolver;
        }
        [GeneratedRegex(@"I:\s+(.*?)\s+LUFS")]
        private static partial Regex LUFSRegex();
        [GeneratedRegex(@"REPLAYGAIN_TRACK_GAIN:\s+-?([0-9.]+)\s+dB")]
        private static partial Regex ReplayGainTagRegex();
        /// 
        /// Probes the specified item for metadata.
        /// 
        /// The item to probe.
        /// The .
        /// The .
        /// The type of item to resolve.
        /// A  probing the item for metadata.
        public async Task Probe(
            T item,
            MetadataRefreshOptions options,
            CancellationToken cancellationToken)
            where T : Audio
        {
            var path = item.Path;
            var protocol = item.PathProtocol ?? MediaProtocol.File;
            if (!item.IsShortcut || options.EnableRemoteContentProbe)
            {
                if (item.IsShortcut)
                {
                    path = item.ShortcutPath;
                    protocol = _mediaSourceManager.GetPathProtocol(path);
                }
                var result = await _mediaEncoder.GetMediaInfo(
                    new MediaInfoRequest
                    {
                        MediaType = DlnaProfileType.Audio,
                        MediaSource = new MediaSourceInfo
                        {
                            Path = path,
                            Protocol = protocol
                        }
                    },
                    cancellationToken).ConfigureAwait(false);
                cancellationToken.ThrowIfCancellationRequested();
                Fetch(item, result, options, cancellationToken);
            }
            var libraryOptions = _libraryManager.GetLibraryOptions(item);
            bool foundLUFSValue = false;
            if (libraryOptions.UseReplayGainTags)
            {
                using (var process = new Process()
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = _mediaEncoder.ProbePath,
                        Arguments = $"-hide_banner -i \"{path}\"",
                        RedirectStandardOutput = false,
                        RedirectStandardError = true
                    },
                })
                {
                    try
                    {
                        process.Start();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error starting ffmpeg");
                        throw;
                    }
                    using var reader = process.StandardError;
                    var output = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
                    cancellationToken.ThrowIfCancellationRequested();
                    Match split = ReplayGainTagRegex().Match(output);
                    if (split.Success)
                    {
                        item.LUFS = DefaultLUFSValue - float.Parse(split.Groups[1].ValueSpan, CultureInfo.InvariantCulture.NumberFormat);
                        foundLUFSValue = true;
                    }
                    else
                    {
                        item.LUFS = DefaultLUFSValue;
                    }
                }
            }
            if (libraryOptions.EnableLUFSScan && !foundLUFSValue)
            {
                using (var process = new Process()
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = _mediaEncoder.EncoderPath,
                        Arguments = $"-hide_banner -i \"{path}\" -af ebur128=framelog=verbose -f null -",
                        RedirectStandardOutput = false,
                        RedirectStandardError = true
                    },
                })
                {
                    try
                    {
                        process.Start();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error starting ffmpeg");
                        throw;
                    }
                    using var reader = process.StandardError;
                    var output = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
                    cancellationToken.ThrowIfCancellationRequested();
                    MatchCollection split = LUFSRegex().Matches(output);
                    if (split.Count != 0)
                    {
                        item.LUFS = float.Parse(split[0].Groups[1].ValueSpan, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else
                    {
                        item.LUFS = DefaultLUFSValue;
                    }
                }
            }
            if (!libraryOptions.EnableLUFSScan && !libraryOptions.UseReplayGainTags)
            {
                item.LUFS = DefaultLUFSValue;
            }
            _logger.LogDebug("LUFS for {ItemName} is {LUFS}.", item.Name, item.LUFS);
            return ItemUpdateType.MetadataImport;
        }
        /// 
        /// Fetches the specified audio.
        /// 
        /// The .
        /// The .
        /// The .
        /// The .
        protected void Fetch(
            Audio audio,
            Model.MediaInfo.MediaInfo mediaInfo,
            MetadataRefreshOptions options,
            CancellationToken cancellationToken)
        {
            audio.Container = mediaInfo.Container;
            audio.TotalBitrate = mediaInfo.Bitrate;
            audio.RunTimeTicks = mediaInfo.RunTimeTicks;
            audio.Size = mediaInfo.Size;
            if (!audio.IsLocked)
            {
                FetchDataFromTags(audio, options);
            }
            var mediaStreams = new List(mediaInfo.MediaStreams);
            AddExternalLyrics(audio, mediaStreams, options);
            audio.HasLyrics = mediaStreams.Any(s => s.Type == MediaStreamType.Lyric);
            _itemRepo.SaveMediaStreams(audio.Id, mediaStreams, cancellationToken);
        }
        /// 
        /// Fetches data from the tags.
        /// 
        /// The .
        /// The .
        private void FetchDataFromTags(Audio audio, MetadataRefreshOptions options)
        {
            var file = TagLib.File.Create(audio.Path);
            var tagTypes = file.TagTypesOnDisk;
            Tag? tags = null;
            if (tagTypes.HasFlag(TagTypes.Id3v2))
            {
                tags = file.GetTag(TagTypes.Id3v2);
            }
            else if (tagTypes.HasFlag(TagTypes.Ape))
            {
                tags = file.GetTag(TagTypes.Ape);
            }
            else if (tagTypes.HasFlag(TagTypes.FlacMetadata))
            {
                tags = file.GetTag(TagTypes.FlacMetadata);
            }
            else if (tagTypes.HasFlag(TagTypes.Apple))
            {
                tags = file.GetTag(TagTypes.Apple);
            }
            else if (tagTypes.HasFlag(TagTypes.Xiph))
            {
                tags = file.GetTag(TagTypes.Xiph);
            }
            else if (tagTypes.HasFlag(TagTypes.AudibleMetadata))
            {
                tags = file.GetTag(TagTypes.AudibleMetadata);
            }
            else if (tagTypes.HasFlag(TagTypes.Id3v1))
            {
                tags = file.GetTag(TagTypes.Id3v1);
            }
            if (tags is not null)
            {
                if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast))
                {
                    var people = new List();
                    var albumArtists = tags.AlbumArtists;
                    foreach (var albumArtist in albumArtists)
                    {
                        if (!string.IsNullOrEmpty(albumArtist))
                        {
                            PeopleHelper.AddPerson(people, new PersonInfo
                            {
                                Name = albumArtist,
                                Type = PersonKind.AlbumArtist
                            });
                        }
                    }
                    var performers = tags.Performers;
                    foreach (var performer in performers)
                    {
                        if (!string.IsNullOrEmpty(performer))
                        {
                            PeopleHelper.AddPerson(people, new PersonInfo
                            {
                                Name = performer,
                                Type = PersonKind.Artist
                            });
                        }
                    }
                    foreach (var composer in tags.Composers)
                    {
                        if (!string.IsNullOrEmpty(composer))
                        {
                            PeopleHelper.AddPerson(people, new PersonInfo
                            {
                                Name = composer,
                                Type = PersonKind.Composer
                            });
                        }
                    }
                    _libraryManager.UpdatePeople(audio, people);
                    if (options.ReplaceAllMetadata && performers.Length != 0)
                    {
                        audio.Artists = performers;
                    }
                    else if (!options.ReplaceAllMetadata
                             && (audio.Artists is null || audio.Artists.Count == 0))
                    {
                        audio.Artists = performers;
                    }
                    if (options.ReplaceAllMetadata && albumArtists.Length != 0)
                    {
                        audio.AlbumArtists = albumArtists;
                    }
                    else if (!options.ReplaceAllMetadata
                             && (audio.AlbumArtists is null || audio.AlbumArtists.Count == 0))
                    {
                        audio.AlbumArtists = albumArtists;
                    }
                }
                if (!audio.LockedFields.Contains(MetadataField.Name))
                {
                    audio.Name = options.ReplaceAllMetadata || string.IsNullOrEmpty(audio.Name) ? tags.Title : audio.Name;
                }
                if (options.ReplaceAllMetadata)
                {
                    audio.Album = tags.Album;
                    audio.IndexNumber = Convert.ToInt32(tags.Track);
                    audio.ParentIndexNumber = Convert.ToInt32(tags.Disc);
                }
                else
                {
                    audio.Album ??= tags.Album;
                    audio.IndexNumber ??= Convert.ToInt32(tags.Track);
                    audio.ParentIndexNumber ??= Convert.ToInt32(tags.Disc);
                }
                if (tags.Year != 0)
                {
                    var year = Convert.ToInt32(tags.Year);
                    audio.ProductionYear = year;
                    audio.PremiereDate = new DateTime(year, 01, 01);
                }
                if (!audio.LockedFields.Contains(MetadataField.Genres))
                {
                    audio.Genres = options.ReplaceAllMetadata || audio.Genres == null || audio.Genres.Length == 0
                        ? tags.Genres.Distinct(StringComparer.OrdinalIgnoreCase).ToArray()
                        : audio.Genres;
                }
                if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out _))
                {
                    audio.SetProviderId(MetadataProvider.MusicBrainzArtist, tags.MusicBrainzArtistId);
                }
                if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbumArtist, out _))
                {
                    audio.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, tags.MusicBrainzReleaseArtistId);
                }
                if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbum, out _))
                {
                    audio.SetProviderId(MetadataProvider.MusicBrainzAlbum, tags.MusicBrainzReleaseId);
                }
                if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzReleaseGroup, out _))
                {
                    audio.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, tags.MusicBrainzReleaseGroupId);
                }
                if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzTrack, out _))
                {
                    audio.SetProviderId(MetadataProvider.MusicBrainzTrack, tags.MusicBrainzTrackId);
                }
            }
        }
        private void AddExternalLyrics(
            Audio audio,
            List currentStreams,
            MetadataRefreshOptions options)
        {
            var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1);
            var externalLyricFiles = _lyricResolver.GetExternalStreams(audio, startIndex, options.DirectoryService, false);
            audio.LyricFiles = externalLyricFiles.Select(i => i.Path).Distinct().ToArray();
            currentStreams.AddRange(externalLyricFiles);
        }
    }
}