using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Session;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
    /// 
    /// Class GetSessions
    /// 
    [Route("/Sessions", "GET")]
    [Api(("Gets a list of sessions"))]
    public class GetSessions : IReturn>
    {
        /// 
        /// Gets or sets a value indicating whether [supports remote control].
        /// 
        /// null if [supports remote control] contains no value, true if [supports remote control]; otherwise, false.
        [ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
        public bool? SupportsRemoteControl { get; set; }
    }
    /// 
    /// Class BrowseTo
    /// 
    [Route("/Sessions/{Id}/Viewing", "POST")]
    [Api(("Instructs a session to browse to an item or view"))]
    public class BrowseTo : IReturnVoid
    {
        /// 
        /// Gets or sets the id.
        /// 
        /// The id.
        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public Guid Id { get; set; }
        /// 
        /// Artist, Genre, Studio, Person, or any kind of BaseItem
        /// 
        /// The type of the item.
        [ApiMember(Name = "ItemType", Description = "The type of item to browse to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string ItemType { get; set; }
        /// 
        /// Artist name, genre name, item Id, etc
        /// 
        /// The item identifier.
        [ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string ItemId { get; set; }
        /// 
        /// Gets or sets the name of the item.
        /// 
        /// The name of the item.
        [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string ItemName { get; set; }
        /// 
        /// Gets or sets the context (Movies, Music, TvShows, etc)
        /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
        /// This is optional to supply and clients are free to ignore it.
        /// 
        /// The context.
        [ApiMember(Name = "Context", Description = "The ui context for the client (movies, music, tv, games etc). This is optional to supply and clients are free to ignore it.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string Context { get; set; }
    }
    [Route("/Sessions/{Id}/Playing", "POST")]
    [Api(("Instructs a session to play an item"))]
    public class Play : IReturnVoid
    {
        /// 
        /// Gets or sets the id.
        /// 
        /// The id.
        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public Guid Id { get; set; }
        /// 
        /// Artist, Genre, Studio, Person, or any kind of BaseItem
        /// 
        /// The type of the item.
        [ApiMember(Name = "ItemIds", Description = "The ids of the items to play, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
        public string ItemIds { get; set; }
        /// 
        /// Gets or sets the start position ticks that the first item should be played at
        /// 
        /// The start position ticks.
        [ApiMember(Name = "StartPositionTicks", Description = "The starting position of the first item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
        public long? StartPositionTicks { get; set; }
        /// 
        /// Gets or sets the play command.
        /// 
        /// The play command.
        [ApiMember(Name = "PlayCommand", Description = "The type of play command to issue (PlayNow, PlayNext, PlayLast). Clients who have not yet implemented play next and play last may play now.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public PlayCommand PlayCommand { get; set; }
    }
    [Route("/Sessions/{Id}/Playing/{Command}", "POST")]
    [Api(("Issues a playstate command to a client"))]
    public class SendPlaystateCommand : IReturnVoid
    {
        /// 
        /// Gets or sets the id.
        /// 
        /// The id.
        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public Guid Id { get; set; }
        /// 
        /// Gets or sets the position to seek to
        /// 
        [ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
        public long? SeekPositionTicks { get; set; }
        /// 
        /// Gets or sets the play command.
        /// 
        /// The play command.
        [ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public PlaystateCommand Command { get; set; }
    }
    [Route("/Sessions/{Id}/System/{Command}", "POST")]
    [Api(("Issues a system command to a client"))]
    public class SendSystemCommand : IReturnVoid
    {
        /// 
        /// Gets or sets the id.
        /// 
        /// The id.
        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public Guid Id { get; set; }
        /// 
        /// Gets or sets the command.
        /// 
        /// The play command.
        [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public SystemCommand Command { get; set; }
    }
    [Route("/Sessions/{Id}/Message", "POST")]
    [Api(("Issues a command to a client to display a message to the user"))]
    public class SendMessageCommand : IReturnVoid
    {
        /// 
        /// Gets or sets the id.
        /// 
        /// The id.
        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
        public Guid Id { get; set; }
        [ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string Text { get; set; }
        [ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string Header { get; set; }
        [ApiMember(Name = "TimeoutMs", Description = "The message timeout. If omitted the user will have to confirm viewing the message.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
        public long? TimeoutMs { get; set; }
    }
    /// 
    /// Class SessionsService
    /// 
    public class SessionsService : BaseApiService
    {
        /// 
        /// The _session manager
        /// 
        private readonly ISessionManager _sessionManager;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The session manager.
        public SessionsService(ISessionManager sessionManager)
        {
            _sessionManager = sessionManager;
        }
        /// 
        /// Gets the specified request.
        /// 
        /// The request.
        /// System.Object.
        public object Get(GetSessions request)
        {
            var result = _sessionManager.Sessions.Where(i => i.IsActive);
            if (request.SupportsRemoteControl.HasValue)
            {
                result = result.Where(i => i.SupportsRemoteControl == request.SupportsRemoteControl.Value);
            }
            return ToOptimizedResult(result.Select(SessionInfoDtoBuilder.GetSessionInfoDto).ToList());
        }
        public void Post(SendPlaystateCommand request)
        {
            var task = SendPlaystateCommand(request);
            Task.WaitAll(task);
        }
        private async Task SendPlaystateCommand(SendPlaystateCommand request)
        {
            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
            if (session == null)
            {
                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
            }
            if (!session.SupportsRemoteControl)
            {
                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
            }
            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
            if (socket != null)
            {
                try
                {
                    await socket.SendAsync(new WebSocketMessage
                    {
                        MessageType = "Playstate",
                        Data = new PlaystateRequest
                        {
                            Command = request.Command,
                            SeekPositionTicks = request.SeekPositionTicks
                        }
                    }, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error sending web socket message", ex);
                }
            }
            else
            {
                throw new InvalidOperationException("The requested session does not have an open web socket.");
            }
        }
        /// 
        /// Posts the specified request.
        /// 
        /// The request.
        public void Post(BrowseTo request)
        {
            var task = BrowseTo(request);
            Task.WaitAll(task);
        }
        /// 
        /// Browses to.
        /// 
        /// The request.
        /// Task.
        /// 
        /// 
        /// The requested session does not have an open web socket.
        private async Task BrowseTo(BrowseTo request)
        {
            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
            if (session == null)
            {
                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
            }
            if (!session.SupportsRemoteControl)
            {
                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
            }
            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
            if (socket != null)
            {
                try
                {
                    await socket.SendAsync(new WebSocketMessage
                    {
                        MessageType = "Browse",
                        Data = request
                    }, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error sending web socket message", ex);
                }
            }
            else
            {
                throw new InvalidOperationException("The requested session does not have an open web socket.");
            }
        }
        /// 
        /// Posts the specified request.
        /// 
        /// The request.
        public void Post(SendSystemCommand request)
        {
            var task = SendSystemCommand(request);
            Task.WaitAll(task);
        }
        private async Task SendSystemCommand(SendSystemCommand request)
        {
            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
            if (session == null)
            {
                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
            }
            if (!session.SupportsRemoteControl)
            {
                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
            }
            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
            if (socket != null)
            {
                try
                {
                    await socket.SendAsync(new WebSocketMessage
                    {
                        MessageType = "SystemCommand",
                        Data = request.Command.ToString()
                    }, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error sending web socket message", ex);
                }
            }
            else
            {
                throw new InvalidOperationException("The requested session does not have an open web socket.");
            }
        }
        /// 
        /// Posts the specified request.
        /// 
        /// The request.
        public void Post(SendMessageCommand request)
        {
            var task = SendMessageCommand(request);
            Task.WaitAll(task);
        }
        private async Task SendMessageCommand(SendMessageCommand request)
        {
            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
            if (session == null)
            {
                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
            }
            if (!session.SupportsRemoteControl)
            {
                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
            }
            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
            if (socket != null)
            {
                try
                {
                    await socket.SendAsync(new WebSocketMessage
                    {
                        MessageType = "MessageCommand",
                        Data = new MessageCommand
                        {
                            Header = string.IsNullOrEmpty(request.Header) ? "Message from Server" : request.Header,
                            TimeoutMs = request.TimeoutMs,
                            Text = request.Text
                        }
                    }, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error sending web socket message", ex);
                }
            }
            else
            {
                throw new InvalidOperationException("The requested session does not have an open web socket.");
            }
        }
        /// 
        /// Posts the specified request.
        /// 
        /// The request.
        public void Post(Play request)
        {
            var task = Play(request);
            Task.WaitAll(task);
        }
        /// 
        /// Plays the specified request.
        /// 
        /// The request.
        /// Task.
        /// 
        /// 
        /// The requested session does not have an open web socket.
        private async Task Play(Play request)
        {
            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
            if (session == null)
            {
                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
            }
            if (!session.SupportsRemoteControl)
            {
                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
            }
            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
            if (socket != null)
            {
                try
                {
                    await socket.SendAsync(new WebSocketMessage
                    {
                        MessageType = "Play",
                        Data = new PlayRequest
                        {
                            ItemIds = request.ItemIds.Split(',').ToArray(),
                            PlayCommand = request.PlayCommand,
                            StartPositionTicks = request.StartPositionTicks
                        }
                    }, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error sending web socket message", ex);
                }
            }
            else
            {
                throw new InvalidOperationException("The requested session does not have an open web socket.");
            }
        }
    }
}