using ServiceStack.Service;
using ServiceStack.ServiceHost;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Playback
{
    /// 
    /// Class StaticRemoteStreamWriter
    /// 
    public class StaticRemoteStreamWriter : IStreamWriter, IHasOptions
    {
        /// 
        /// The _input stream
        /// 
        private readonly HttpResponseMessage _msg;
        private readonly HttpClient _client;
        /// 
        /// The _options
        /// 
        private readonly IDictionary _options = new Dictionary();
        /// 
        /// Initializes a new instance of the  class.
        /// 
        public StaticRemoteStreamWriter(HttpResponseMessage msg, HttpClient client)
        {
            _msg = msg;
            _client = client;
        }
        /// 
        /// Gets the options.
        /// 
        /// The options.
        public IDictionary Options
        {
            get { return _options; }
        }
        /// 
        /// Writes to.
        /// 
        /// The response stream.
        public void WriteTo(Stream responseStream)
        {
            var task = WriteToAsync(responseStream);
            Task.WaitAll(task);
        }
        /// 
        /// Writes to async.
        /// 
        /// The response stream.
        /// Task.
        public async Task WriteToAsync(Stream responseStream)
        {
            using (_client)
            {
                using (_msg)
                {
                    using (var input = await _msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    {
                        await input.CopyToAsync(responseStream).ConfigureAwait(false);
                    }
                }
            }
        }
    }
}