using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.Models.PlaybackDtos;
using Jellyfin.Api.Models.StreamingDtos;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Api.Controllers
{
    /// 
    /// The video hls controller.
    /// 
    [Route("")]
    [Authorize(Policy = Policies.DefaultAuthorization)]
    public class VideoHlsController : BaseJellyfinApiController
    {
        private const string DefaultEncoderPreset = "superfast";
        private const TranscodingJobType TranscodingJobType = MediaBrowser.Controller.MediaEncoding.TranscodingJobType.Hls;
        private readonly EncodingHelper _encodingHelper;
        private readonly IDlnaManager _dlnaManager;
        private readonly IAuthorizationContext _authContext;
        private readonly IUserManager _userManager;
        private readonly ILibraryManager _libraryManager;
        private readonly IMediaSourceManager _mediaSourceManager;
        private readonly IServerConfigurationManager _serverConfigurationManager;
        private readonly IMediaEncoder _mediaEncoder;
        private readonly IFileSystem _fileSystem;
        private readonly ISubtitleEncoder _subtitleEncoder;
        private readonly IConfiguration _configuration;
        private readonly IDeviceManager _deviceManager;
        private readonly TranscodingJobHelper _transcodingJobHelper;
        private readonly ILogger _logger;
        private readonly EncodingOptions _encodingOptions;
        /// 
        /// 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.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// The  singleton.
        /// Instance of the .
        public VideoHlsController(
            IMediaEncoder mediaEncoder,
            IFileSystem fileSystem,
            ISubtitleEncoder subtitleEncoder,
            IConfiguration configuration,
            IDlnaManager dlnaManager,
            IUserManager userManger,
            IAuthorizationContext authorizationContext,
            ILibraryManager libraryManager,
            IMediaSourceManager mediaSourceManager,
            IServerConfigurationManager serverConfigurationManager,
            IDeviceManager deviceManager,
            TranscodingJobHelper transcodingJobHelper,
            ILogger logger)
        {
            _encodingHelper = new EncodingHelper(mediaEncoder, fileSystem, subtitleEncoder, configuration);
            _dlnaManager = dlnaManager;
            _authContext = authorizationContext;
            _userManager = userManger;
            _libraryManager = libraryManager;
            _mediaSourceManager = mediaSourceManager;
            _serverConfigurationManager = serverConfigurationManager;
            _mediaEncoder = mediaEncoder;
            _fileSystem = fileSystem;
            _subtitleEncoder = subtitleEncoder;
            _configuration = configuration;
            _deviceManager = deviceManager;
            _transcodingJobHelper = transcodingJobHelper;
            _logger = logger;
            _encodingOptions = serverConfigurationManager.GetEncodingOptions();
        }
        /// 
        /// Gets a hls live stream.
        /// 
        /// The item id.
        /// The audio container.
        /// Optional. If true, the original file will be streamed statically without any encoding. Use either no url extension or the original file extension. true/false.
        /// The streaming parameters.
        /// The tag.
        /// Optional. The dlna device profile id to utilize.
        /// The play session id.
        /// The segment container.
        /// The segment lenght.
        /// The minimum number of segments.
        /// The media version id, if playing an alternate version.
        /// The device id of the client requesting. Used to stop encoding processes when needed.
        /// Optional. Specify a audio codec to encode to, e.g. mp3. If omitted the server will auto-select using the url's extension. Options: aac, mp3, vorbis, wma.
        /// Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.
        /// Whether or not to allow copying of the video stream url.
        /// Whether or not to allow copying of the audio stream url.
        /// Optional. Whether to break on non key frames.
        /// Optional. Specify a specific audio sample rate, e.g. 44100.
        /// Optional. The maximum audio bit depth.
        /// Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.
        /// Optional. Specify a specific number of audio channels to encode to, e.g. 2.
        /// Optional. Specify a maximum number of audio channels to encode to, e.g. 2.
        /// Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, high.
        /// Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.
        /// Optional. A specific video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.
        /// Optional. A specific maximum video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.
        /// Whether or not to copy timestamps when transcoding with an offset. Defaults to false.
        /// Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.
        /// Optional. The fixed horizontal resolution of the encoded video.
        /// Optional. The fixed vertical resolution of the encoded video.
        /// Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.
        /// Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.
        /// Optional. Specify the subtitle delivery method.
        /// Optional.
        /// Optional. The maximum video bit depth.
        /// Optional. Whether to require avc.
        /// Optional. Whether to deinterlace the video.
        /// Optional. Whether to require a non anamporphic stream.
        /// Optional. The maximum number of audio channels to transcode.
        /// Optional. The limit of how many cpu cores to use.
        /// The live stream id.
        /// Optional. Whether to enable the MpegtsM2Ts mode.
        /// Optional. Specify a video codec to encode to, e.g. h264. If omitted the server will auto-select using the url's extension. Options: h265, h264, mpeg4, theora, vpx, wmv.
        /// Optional. Specify a subtitle codec to encode to.
        /// Optional. The transcoding reason.
        /// Optional. The index of the audio stream to use. If omitted the first audio stream will be used.
        /// Optional. The index of the video stream to use. If omitted the first video stream will be used.
        /// Optional. The .
        /// Optional. The streaming options.
        /// Optional. The max width.
        /// Optional. The max height.
        /// Optional. Whether to enable subtitles in the manifest.
        /// Hls live stream retrieved.
        /// A  containing the hls file.
        [HttpGet("Videos/{itemId}/live.m3u8")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task GetLiveHlsStream(
            [FromRoute] Guid itemId,
            [FromQuery] string? container,
            [FromQuery] bool? @static,
            [FromQuery] string? @params,
            [FromQuery] string? tag,
            [FromQuery] string? deviceProfileId,
            [FromQuery] string? playSessionId,
            [FromQuery] string? segmentContainer,
            [FromQuery] int? segmentLength,
            [FromQuery] int? minSegments,
            [FromQuery] string? mediaSourceId,
            [FromQuery] string? deviceId,
            [FromQuery] string? audioCodec,
            [FromQuery] bool? enableAutoStreamCopy,
            [FromQuery] bool? allowVideoStreamCopy,
            [FromQuery] bool? allowAudioStreamCopy,
            [FromQuery] bool? breakOnNonKeyFrames,
            [FromQuery] int? audioSampleRate,
            [FromQuery] int? maxAudioBitDepth,
            [FromQuery] int? audioBitRate,
            [FromQuery] int? audioChannels,
            [FromQuery] int? maxAudioChannels,
            [FromQuery] string? profile,
            [FromQuery] string? level,
            [FromQuery] float? framerate,
            [FromQuery] float? maxFramerate,
            [FromQuery] bool? copyTimestamps,
            [FromQuery] long? startTimeTicks,
            [FromQuery] int? width,
            [FromQuery] int? height,
            [FromQuery] int? videoBitRate,
            [FromQuery] int? subtitleStreamIndex,
            [FromQuery] SubtitleDeliveryMethod subtitleMethod,
            [FromQuery] int? maxRefFrames,
            [FromQuery] int? maxVideoBitDepth,
            [FromQuery] bool? requireAvc,
            [FromQuery] bool? deInterlace,
            [FromQuery] bool? requireNonAnamorphic,
            [FromQuery] int? transcodingMaxAudioChannels,
            [FromQuery] int? cpuCoreLimit,
            [FromQuery] string? liveStreamId,
            [FromQuery] bool? enableMpegtsM2TsMode,
            [FromQuery] string? videoCodec,
            [FromQuery] string? subtitleCodec,
            [FromQuery] string? transcodingReasons,
            [FromQuery] int? audioStreamIndex,
            [FromQuery] int? videoStreamIndex,
            [FromQuery] EncodingContext context,
            [FromQuery] Dictionary streamOptions,
            [FromQuery] int? maxWidth,
            [FromQuery] int? maxHeight,
            [FromQuery] bool? enableSubtitlesInManifest)
        {
            VideoRequestDto streamingRequest = new VideoRequestDto
            {
                Id = itemId,
                Container = container,
                Static = @static ?? true,
                Params = @params,
                Tag = tag,
                DeviceProfileId = deviceProfileId,
                PlaySessionId = playSessionId,
                SegmentContainer = segmentContainer,
                SegmentLength = segmentLength,
                MinSegments = minSegments,
                MediaSourceId = mediaSourceId,
                DeviceId = deviceId,
                AudioCodec = audioCodec,
                EnableAutoStreamCopy = enableAutoStreamCopy ?? true,
                AllowAudioStreamCopy = allowAudioStreamCopy ?? true,
                AllowVideoStreamCopy = allowVideoStreamCopy ?? true,
                BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false,
                AudioSampleRate = audioSampleRate,
                MaxAudioChannels = maxAudioChannels,
                AudioBitRate = audioBitRate,
                MaxAudioBitDepth = maxAudioBitDepth,
                AudioChannels = audioChannels,
                Profile = profile,
                Level = level,
                Framerate = framerate,
                MaxFramerate = maxFramerate,
                CopyTimestamps = copyTimestamps ?? true,
                StartTimeTicks = startTimeTicks,
                Width = width,
                Height = height,
                VideoBitRate = videoBitRate,
                SubtitleStreamIndex = subtitleStreamIndex,
                SubtitleMethod = subtitleMethod,
                MaxRefFrames = maxRefFrames,
                MaxVideoBitDepth = maxVideoBitDepth,
                RequireAvc = requireAvc ?? true,
                DeInterlace = deInterlace ?? true,
                RequireNonAnamorphic = requireNonAnamorphic ?? true,
                TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
                CpuCoreLimit = cpuCoreLimit,
                LiveStreamId = liveStreamId,
                EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? true,
                VideoCodec = videoCodec,
                SubtitleCodec = subtitleCodec,
                TranscodeReasons = transcodingReasons,
                AudioStreamIndex = audioStreamIndex,
                VideoStreamIndex = videoStreamIndex,
                Context = context,
                StreamOptions = streamOptions,
                MaxHeight = maxHeight,
                MaxWidth = maxWidth,
                EnableSubtitlesInManifest = enableSubtitlesInManifest ?? true
            };
            var cancellationTokenSource = new CancellationTokenSource();
            using var state = await StreamingHelpers.GetStreamingState(
                    streamingRequest,
                    Request,
                    _authContext,
                    _mediaSourceManager,
                    _userManager,
                    _libraryManager,
                    _serverConfigurationManager,
                    _mediaEncoder,
                    _fileSystem,
                    _subtitleEncoder,
                    _configuration,
                    _dlnaManager,
                    _deviceManager,
                    _transcodingJobHelper,
                    TranscodingJobType,
                    cancellationTokenSource.Token)
                .ConfigureAwait(false);
            TranscodingJobDto? job = null;
            var playlist = state.OutputFilePath;
            if (!System.IO.File.Exists(playlist))
            {
                var transcodingLock = _transcodingJobHelper.GetTranscodingLock(playlist);
                await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
                try
                {
                    if (!System.IO.File.Exists(playlist))
                    {
                        // If the playlist doesn't already exist, startup ffmpeg
                        try
                        {
                            job = await _transcodingJobHelper.StartFfMpeg(
                                    state,
                                    playlist,
                                    GetCommandLineArguments(playlist, state),
                                    Request,
                                    TranscodingJobType,
                                    cancellationTokenSource)
                                .ConfigureAwait(false);
                            job.IsLiveOutput = true;
                        }
                        catch
                        {
                            state.Dispose();
                            throw;
                        }
                        minSegments = state.MinSegments;
                        if (minSegments > 0)
                        {
                            await HlsHelpers.WaitForMinimumSegmentCount(playlist, minSegments, _logger, cancellationTokenSource.Token).ConfigureAwait(false);
                        }
                    }
                }
                finally
                {
                    transcodingLock.Release();
                }
            }
            job ??= _transcodingJobHelper.OnTranscodeBeginRequest(playlist, TranscodingJobType);
            if (job != null)
            {
                _transcodingJobHelper.OnTranscodeEndRequest(job);
            }
            var playlistText = HlsHelpers.GetLivePlaylistText(playlist, state.SegmentLength);
            return Content(playlistText, MimeTypes.GetMimeType("playlist.m3u8"));
        }
        /// 
        /// Gets the command line arguments for ffmpeg.
        /// 
        /// The output path of the file.
        /// The .
        /// The command line arguments as a string.
        private string GetCommandLineArguments(string outputPath, StreamState state)
        {
            var videoCodec = _encodingHelper.GetVideoEncoder(state, _encodingOptions);
            var threads = _encodingHelper.GetNumberOfThreads(state, _encodingOptions, videoCodec);
            var inputModifier = _encodingHelper.GetInputModifier(state, _encodingOptions);
            var format = !string.IsNullOrWhiteSpace(state.Request.SegmentContainer) ? "." + state.Request.SegmentContainer : ".ts";
            var outputTsArg = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + format;
            var segmentFormat = format.TrimStart('.');
            if (string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase))
            {
                segmentFormat = "mpegts";
            }
            var baseUrlParam = string.Format(
                CultureInfo.InvariantCulture,
                "\"hls{0}\"",
                Path.GetFileNameWithoutExtension(outputPath));
            return string.Format(
                    CultureInfo.InvariantCulture,
                    "{0} {1} -map_metadata -1 -map_chapters -1 -threads {2} {3} {4} {5} -f segment -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero -segment_time {6} {7} -individual_header_trailer 0 -segment_format {8} -segment_list_entry_prefix {9} -segment_list_type m3u8 -segment_start_number 0 -segment_list \"{10}\" -y \"{11}\"",
                    inputModifier,
                    _encodingHelper.GetInputArgument(state, _encodingOptions),
                    threads,
                    _encodingHelper.GetMapArgs(state),
                    GetVideoArguments(state),
                    GetAudioArguments(state),
                    state.SegmentLength.ToString(CultureInfo.InvariantCulture),
                    string.Empty,
                    segmentFormat,
                    baseUrlParam,
                    outputPath,
                    outputTsArg)
                .Trim();
        }
        /// 
        /// Gets the audio arguments for transcoding.
        /// 
        /// The .
        /// The command line arguments for audio transcoding.
        private string GetAudioArguments(StreamState state)
        {
            var codec = _encodingHelper.GetAudioEncoder(state);
            if (EncodingHelper.IsCopyCodec(codec))
            {
                return "-codec:a:0 copy";
            }
            var args = "-codec:a:0 " + codec;
            var channels = state.OutputAudioChannels;
            if (channels.HasValue)
            {
                args += " -ac " + channels.Value;
            }
            var bitrate = state.OutputAudioBitrate;
            if (bitrate.HasValue)
            {
                args += " -ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture);
            }
            if (state.OutputAudioSampleRate.HasValue)
            {
                args += " -ar " + state.OutputAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture);
            }
            args += " " + _encodingHelper.GetAudioFilterParam(state, _encodingOptions, true);
            return args;
        }
        /// 
        /// Gets the video arguments for transcoding.
        /// 
        /// The .
        /// The command line arguments for video transcoding.
        private string GetVideoArguments(StreamState state)
        {
            if (!state.IsOutputVideo)
            {
                return string.Empty;
            }
            var codec = _encodingHelper.GetVideoEncoder(state, _encodingOptions);
            var args = "-codec:v:0 " + codec;
            // if (state.EnableMpegtsM2TsMode)
            // {
            //     args += " -mpegts_m2ts_mode 1";
            // }
            // See if we can save come cpu cycles by avoiding encoding
            if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
            {
                // if h264_mp4toannexb is ever added, do not use it for live tv
                if (state.VideoStream != null &&
                    !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
                {
                    string bitStreamArgs = _encodingHelper.GetBitStreamArgs(state.VideoStream);
                    if (!string.IsNullOrEmpty(bitStreamArgs))
                    {
                        args += " " + bitStreamArgs;
                    }
                }
            }
            else
            {
                var keyFrameArg = string.Format(
                    CultureInfo.InvariantCulture,
                    " -force_key_frames \"expr:gte(t,n_forced*{0})\"",
                    state.SegmentLength.ToString(CultureInfo.InvariantCulture));
                var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;
                args += " " + _encodingHelper.GetVideoQualityParam(state, codec, _encodingOptions, DefaultEncoderPreset) + keyFrameArg;
                // Add resolution params, if specified
                if (!hasGraphicalSubs)
                {
                    args += _encodingHelper.GetOutputSizeParam(state, _encodingOptions, codec);
                }
                // This is for internal graphical subs
                if (hasGraphicalSubs)
                {
                    args += _encodingHelper.GetGraphicalSubtitleParam(state, _encodingOptions, codec);
                }
            }
            args += " -flags -global_header";
            if (!string.IsNullOrEmpty(state.OutputVideoSync))
            {
                args += " -vsync " + state.OutputVideoSync;
            }
            args += _encodingHelper.GetOutputFFlags(state);
            return args;
        }
    }
}