using System;
using System.Collections.Generic;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Extensions;
using System.Diagnostics;
using MediaBrowser.Model.MediaInfo;
namespace MediaBrowser.Model.Entities
{
    /// 
    /// Class MediaStream
    /// 
    [DebuggerDisplay("StreamType = {Type}")]
    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 comment.
        /// 
        /// The comment.
        public string Comment { get; set; }
        public string TimeBase { get; set; }
        public string CodecTimeBase { get; set; }
        public string Title { get; set; }
        public string DisplayTitle
        {
            get
            {
                if (!string.IsNullOrEmpty(Title))
                {
                    return AddLanguageIfNeeded(Title);
                }
                if (Type == MediaStreamType.Audio)
                {
                    List attributes = new List();
                    if (!string.IsNullOrEmpty(Language))
                    {
                        attributes.Add(StringHelper.FirstToUpper(Language));
                    }
                    if (!string.IsNullOrEmpty(Codec) && !StringHelper.EqualsIgnoreCase(Codec, "dca"))
                    {
                        attributes.Add(AudioCodec.GetFriendlyName(Codec));
                    } 
                    else if (!string.IsNullOrEmpty(Profile) && !StringHelper.EqualsIgnoreCase(Profile, "lc"))
                    {
                        attributes.Add(Profile);
                    }
                    if (!string.IsNullOrEmpty(ChannelLayout))
                    {
                        attributes.Add(ChannelLayout);
                    }
                    else if (Channels.HasValue)
                    {
                        attributes.Add(StringHelper.ToStringCultureInvariant(Channels.Value) + " ch");
                    }
                    if (IsDefault)
                    {
                        attributes.Add("Default");
                    }
                    return string.Join(" ", attributes.ToArray());
                }
                if (Type == MediaStreamType.Subtitle)
                {
                    List attributes = new List();
                    if (!string.IsNullOrEmpty(Language))
                    {
                        attributes.Add(StringHelper.FirstToUpper(Language));
                    }
                    if (IsDefault)
                    {
                        attributes.Add("Default");
                    }
                    if (IsForced)
                    {
                        attributes.Add("Forced");
                    }
                    string name = string.Join(" ", attributes.ToArray());
                    return name;
                }
                if (Type == MediaStreamType.Video)
                {
                }
                return null;
            }
        }
        private string AddLanguageIfNeeded(string title)
        {
            if (!string.IsNullOrEmpty(Language) && !string.Equals(Language, "und", StringComparison.OrdinalIgnoreCase) && title.IndexOf(Language, StringComparison.OrdinalIgnoreCase) == -1)
            {
                title = StringHelper.FirstToUpper(Language) + " " + title;
            }
            return title;
        }
        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 StringHelper.IndexOfIgnoreCase(codec, "pgs") == -1 &&
                   StringHelper.IndexOfIgnoreCase(codec, "dvd") == -1 &&
                   StringHelper.IndexOfIgnoreCase(codec, "dvbsub") == -1 &&
                   !StringHelper.EqualsIgnoreCase(codec, "sub");
        }
        public bool SupportsSubtitleConversionTo(string codec)
        {
            if (!IsTextSubtitleStream)
            {
                return false;
            }
            // Can't convert from this 
            if (StringHelper.EqualsIgnoreCase(Codec, "ass"))
            {
                return false;
            }
            if (StringHelper.EqualsIgnoreCase(Codec, "ssa"))
            {
                return false;
            }
            // Can't convert to this 
            if (StringHelper.EqualsIgnoreCase(codec, "ass"))
            {
                return false;
            }
            if (StringHelper.EqualsIgnoreCase(codec, "ssa"))
            {
                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 external identifier.
        /// 
        /// The external identifier.
        public string ExternalId { 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; }
    }
}