#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.MediaInfo;
namespace MediaBrowser.Model.Entities
{
    /// 
    /// Class MediaStream.
    /// 
    public class MediaStream
    {
        /// 
        /// Gets or sets the codec.
        /// 
        /// The codec.
        public string Codec { get; set; }
        /// 
        /// Gets or sets the codec tag.
        /// 
        /// The codec tag.
        public string CodecTag { get; set; }
        /// 
        /// Gets or sets the language.
        /// 
        /// The language.
        public string Language { get; set; }
        /// 
        /// Gets or sets the color range.
        /// 
        /// The color range.
        public string ColorRange { get; set; }
        /// 
        /// Gets or sets the color space.
        /// 
        /// The color space.
        public string ColorSpace { get; set; }
        /// 
        /// Gets or sets the color transfer.
        /// 
        /// The color transfer.
        public string ColorTransfer { get; set; }
        /// 
        /// Gets or sets the color primaries.
        /// 
        /// The color primaries.
        public string ColorPrimaries { get; set; }
        /// 
        /// Gets or sets the comment.
        /// 
        /// The comment.
        public string Comment { get; set; }
        /// 
        /// Gets or sets the time base.
        /// 
        /// The time base.
        public string TimeBase { get; set; }
        /// 
        /// Gets or sets the codec time base.
        /// 
        /// The codec time base.
        public string CodecTimeBase { get; set; }
        /// 
        /// Gets or sets the title.
        /// 
        /// The title.
        public string Title { get; set; }
        /// 
        /// Gets or sets the video range.
        /// 
        /// The video range.
        public string VideoRange
        {
            get
            {
                if (Type != MediaStreamType.Video)
                {
                    return null;
                }
                var colorTransfer = ColorTransfer;
                if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
                    || string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
                {
                    return "HDR";
                }
                return "SDR";
            }
        }
        public string localizedUndefined { get; set; }
        public string localizedDefault { get; set; }
        public string localizedForced { get; set; }
        public string DisplayTitle
        {
            get
            {
                switch (Type)
                {
                    case MediaStreamType.Audio:
                    {
                        var attributes = new List();
                        if (!string.IsNullOrEmpty(Language))
                        {
                            // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
                            string fullLanguage = CultureInfo
                                .GetCultures(CultureTypes.NeutralCultures)
                                .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
                                ?.DisplayName;
                            attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
                        }
                        if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase))
                        {
                            attributes.Add(AudioCodec.GetFriendlyName(Codec));
                        }
                        else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
                        {
                            attributes.Add(Profile);
                        }
                        if (!string.IsNullOrEmpty(ChannelLayout))
                        {
                            attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
                        }
                        else if (Channels.HasValue)
                        {
                            attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
                        }
                        if (IsDefault)
                        {
                            attributes.Add(string.IsNullOrEmpty(localizedDefault) ? "Default" : localizedDefault);
                        }
                        if (!string.IsNullOrEmpty(Title))
                        {
                            var result = new StringBuilder(Title);
                            foreach (var tag in attributes)
                            {
                                // Keep Tags that are not already in Title.
                                if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    result.Append(" - ").Append(tag);
                                }
                            }
                            return result.ToString();
                        }
                        return string.Join(" - ", attributes);
                    }
                    case MediaStreamType.Video:
                    {
                        var attributes = new List();
                        var resolutionText = GetResolutionText();
                        if (!string.IsNullOrEmpty(resolutionText))
                        {
                            attributes.Add(resolutionText);
                        }
                        if (!string.IsNullOrEmpty(Codec))
                        {
                            attributes.Add(Codec.ToUpperInvariant());
                        }
                        if (!string.IsNullOrEmpty(Title))
                        {
                            var result = new StringBuilder(Title);
                            foreach (var tag in attributes)
                            {
                                // Keep Tags that are not already in Title.
                                if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    result.Append(" - ").Append(tag);
                                }
                            }
                            return result.ToString();
                        }
                        return string.Join(" ", attributes);
                    }
                    case MediaStreamType.Subtitle:
                    {
                        var attributes = new List();
                        if (!string.IsNullOrEmpty(Language))
                        {
                            // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
                            string fullLanguage = CultureInfo
                                .GetCultures(CultureTypes.NeutralCultures)
                                .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
                                ?.DisplayName;
                            attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
                        }
                        else
                        {
                            attributes.Add(string.IsNullOrEmpty(localizedUndefined) ? "Und" : localizedUndefined);
                        }
                        if (IsDefault)
                        {
                            attributes.Add(string.IsNullOrEmpty(localizedDefault) ? "Default" : localizedDefault);
                        }
                        if (IsForced)
                        {
                            attributes.Add(string.IsNullOrEmpty(localizedForced) ? "Forced" : localizedForced);
                        }
                        if (!string.IsNullOrEmpty(Title))
                        {
                            var result = new StringBuilder(Title);
                            foreach (var tag in attributes)
                            {
                                // Keep Tags that are not already in Title.
                                if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    result.Append(" - ").Append(tag);
                                }
                            }
                            return result.ToString();
                        }
                        return string.Join(" - ", attributes);
                    }
                    default:
                        return null;
                }
            }
        }
        private string GetResolutionText()
        {
            var i = this;
            if (i.Width.HasValue && i.Height.HasValue)
            {
                var width = i.Width.Value;
                var height = i.Height.Value;
                if (width >= 3800 || height >= 2000)
                {
                    return "4K";
                }
                if (width >= 2500)
                {
                    if (i.IsInterlaced)
                    {
                        return "1440i";
                    }
                    return "1440p";
                }
                if (width >= 1900 || height >= 1000)
                {
                    if (i.IsInterlaced)
                    {
                        return "1080i";
                    }
                    return "1080p";
                }
                if (width >= 1260 || height >= 700)
                {
                    if (i.IsInterlaced)
                    {
                        return "720i";
                    }
                    return "720p";
                }
                if (width >= 700 || height >= 440)
                {
                    if (i.IsInterlaced)
                    {
                        return "480i";
                    }
                    return "480p";
                }
                return "SD";
            }
            return null;
        }
        public string NalLengthSize { get; set; }
        /// 
        /// Gets or sets a value indicating whether this instance is interlaced.
        /// 
        /// true if this instance is interlaced; otherwise, false.
        public bool IsInterlaced { get; set; }
        public bool? IsAVC { get; set; }
        /// 
        /// Gets or sets the channel layout.
        /// 
        /// The channel layout.
        public string ChannelLayout { get; set; }
        /// 
        /// Gets or sets the bit rate.
        /// 
        /// The bit rate.
        public int? BitRate { get; set; }
        /// 
        /// Gets or sets the bit depth.
        /// 
        /// The bit depth.
        public int? BitDepth { get; set; }
        /// 
        /// Gets or sets the reference frames.
        /// 
        /// The reference frames.
        public int? RefFrames { get; set; }
        /// 
        /// Gets or sets the length of the packet.
        /// 
        /// The length of the packet.
        public int? PacketLength { get; set; }
        /// 
        /// Gets or sets the channels.
        /// 
        /// The channels.
        public int? Channels { get; set; }
        /// 
        /// Gets or sets the sample rate.
        /// 
        /// The sample rate.
        public int? SampleRate { get; set; }
        /// 
        /// Gets or sets a value indicating whether this instance is default.
        /// 
        /// true if this instance is default; otherwise, false.
        public bool IsDefault { get; set; }
        /// 
        /// Gets or sets a value indicating whether this instance is forced.
        /// 
        /// true if this instance is forced; otherwise, false.
        public bool IsForced { get; set; }
        /// 
        /// Gets or sets the height.
        /// 
        /// The height.
        public int? Height { get; set; }
        /// 
        /// Gets or sets the width.
        /// 
        /// The width.
        public int? Width { get; set; }
        /// 
        /// Gets or sets the average frame rate.
        /// 
        /// The average frame rate.
        public float? AverageFrameRate { get; set; }
        /// 
        /// Gets or sets the real frame rate.
        /// 
        /// The real frame rate.
        public float? RealFrameRate { get; set; }
        /// 
        /// Gets or sets the profile.
        /// 
        /// The profile.
        public string Profile { get; set; }
        /// 
        /// Gets or sets the type.
        /// 
        /// The type.
        public MediaStreamType Type { get; set; }
        /// 
        /// Gets or sets the aspect ratio.
        /// 
        /// The aspect ratio.
        public string AspectRatio { get; set; }
        /// 
        /// Gets or sets the index.
        /// 
        /// The index.
        public int Index { get; set; }
        /// 
        /// Gets or sets the score.
        /// 
        /// The score.
        public int? Score { get; set; }
        /// 
        /// Gets or sets a value indicating whether this instance is external.
        /// 
        /// true if this instance is external; otherwise, false.
        public bool IsExternal { get; set; }
        /// 
        /// Gets or sets the method.
        /// 
        /// The method.
        public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
        /// 
        /// Gets or sets the delivery URL.
        /// 
        /// The delivery URL.
        public string DeliveryUrl { get; set; }
        /// 
        /// Gets or sets a value indicating whether this instance is external URL.
        /// 
        /// null if [is external URL] contains no value, true if [is external URL]; otherwise, false.
        public bool? IsExternalUrl { get; set; }
        public bool IsTextSubtitleStream
        {
            get
            {
                if (Type != MediaStreamType.Subtitle)
                {
                    return false;
                }
                if (string.IsNullOrEmpty(Codec) && !IsExternal)
                {
                    return false;
                }
                return IsTextFormat(Codec);
            }
        }
        public static bool IsTextFormat(string format)
        {
            string codec = format ?? string.Empty;
            // sub = external .sub file
            return codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) == -1 &&
                   codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) == -1 &&
                   codec.IndexOf("dvbsub", StringComparison.OrdinalIgnoreCase) == -1 &&
                   !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
                   !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
        }
        public bool SupportsSubtitleConversionTo(string toCodec)
        {
            if (!IsTextSubtitleStream)
            {
                return false;
            }
            var fromCodec = Codec;
            // Can't convert from this
            if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            // Can't convert to this
            if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            return true;
        }
        /// 
        /// Gets or sets a value indicating whether [supports external stream].
        /// 
        /// true if [supports external stream]; otherwise, false.
        public bool SupportsExternalStream { get; set; }
        /// 
        /// Gets or sets the filename.
        /// 
        /// The filename.
        public string Path { get; set; }
        /// 
        /// Gets or sets the pixel format.
        /// 
        /// The pixel format.
        public string PixelFormat { get; set; }
        /// 
        /// Gets or sets the level.
        /// 
        /// The level.
        public double? Level { get; set; }
        /// 
        /// Gets a value indicating whether this instance is anamorphic.
        /// 
        /// true if this instance is anamorphic; otherwise, false.
        public bool? IsAnamorphic { get; set; }
    }
}