using MediaBrowser.Common.Events;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Session;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Session
{
    /// 
    /// Class SessionManager
    /// 
    public class SessionManager : ISessionManager
    {
        /// 
        /// The _user data repository
        /// 
        private readonly IUserDataManager _userDataRepository;
        /// 
        /// The _user repository
        /// 
        private readonly IUserRepository _userRepository;
        /// 
        /// The _logger
        /// 
        private readonly ILogger _logger;
        private readonly ILibraryManager _libraryManager;
        private readonly IUserManager _userManager;
        /// 
        /// Gets or sets the configuration manager.
        /// 
        /// The configuration manager.
        private readonly IServerConfigurationManager _configurationManager;
        /// 
        /// The _active connections
        /// 
        private readonly ConcurrentDictionary _activeConnections =
            new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
        /// 
        /// Occurs when [playback start].
        /// 
        public event EventHandler PlaybackStart;
        /// 
        /// Occurs when [playback progress].
        /// 
        public event EventHandler PlaybackProgress;
        /// 
        /// Occurs when [playback stopped].
        /// 
        public event EventHandler PlaybackStopped;
        private IEnumerable _sessionFactories = new List();
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The user data repository.
        /// The configuration manager.
        /// The logger.
        /// The user repository.
        /// The library manager.
        public SessionManager(IUserDataManager userDataRepository, IServerConfigurationManager configurationManager, ILogger logger, IUserRepository userRepository, ILibraryManager libraryManager, IUserManager userManager)
        {
            _userDataRepository = userDataRepository;
            _configurationManager = configurationManager;
            _logger = logger;
            _userRepository = userRepository;
            _libraryManager = libraryManager;
            _userManager = userManager;
        }
        /// 
        /// Adds the parts.
        /// 
        /// The session factories.
        public void AddParts(IEnumerable sessionFactories)
        {
            _sessionFactories = sessionFactories.ToList();
        }
        /// 
        /// Gets all connections.
        /// 
        /// All connections.
        public IEnumerable Sessions
        {
            get { return _activeConnections.Values.OrderByDescending(c => c.LastActivityDate).ToList(); }
        }
        /// 
        /// Logs the user activity.
        /// 
        /// Type of the client.
        /// The app version.
        /// The device id.
        /// Name of the device.
        /// The remote end point.
        /// The user.
        /// Task.
        /// user
        /// 
        public async Task LogSessionActivity(string clientType, string appVersion, string deviceId, string deviceName, string remoteEndPoint, User user)
        {
            if (string.IsNullOrEmpty(clientType))
            {
                throw new ArgumentNullException("clientType");
            }
            if (string.IsNullOrEmpty(appVersion))
            {
                throw new ArgumentNullException("appVersion");
            }
            if (string.IsNullOrEmpty(deviceId))
            {
                throw new ArgumentNullException("deviceId");
            }
            if (string.IsNullOrEmpty(deviceName))
            {
                throw new ArgumentNullException("deviceName");
            }
            if (user != null && user.Configuration.IsDisabled)
            {
                throw new UnauthorizedAccessException(string.Format("The {0} account is currently disabled. Please consult with your administrator.", user.Name));
            }
            var activityDate = DateTime.UtcNow;
            var userId = user == null ? (Guid?)null : user.Id;
            var username = user == null ? null : user.Name;
            var session = GetSessionInfo(clientType, appVersion, deviceId, deviceName, remoteEndPoint, userId, username);
            session.LastActivityDate = activityDate;
            if (user == null)
            {
                return session;
            }
            var lastActivityDate = user.LastActivityDate;
            user.LastActivityDate = activityDate;
            // Don't log in the db anymore frequently than 10 seconds
            if (lastActivityDate.HasValue && (activityDate - lastActivityDate.Value).TotalSeconds < 10)
            {
                return session;
            }
            // Save this directly. No need to fire off all the events for this.
            await _userRepository.SaveUser(user, CancellationToken.None).ConfigureAwait(false);
            return session;
        }
        /// 
        /// Updates the now playing item id.
        /// 
        /// The session.
        /// The item.
        /// if set to true [is paused].
        /// The current position ticks.
        private void UpdateNowPlayingItem(SessionInfo session, BaseItem item, bool isPaused, bool isMuted, long? currentPositionTicks = null)
        {
            session.IsMuted = isMuted;
            session.IsPaused = isPaused;
            session.NowPlayingPositionTicks = currentPositionTicks;
            session.NowPlayingItem = item;
            session.LastActivityDate = DateTime.UtcNow;
        }
        /// 
        /// Removes the now playing item id.
        /// 
        /// The session.
        /// The item.
        private void RemoveNowPlayingItem(SessionInfo session, BaseItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (session.NowPlayingItem != null && session.NowPlayingItem.Id == item.Id)
            {
                session.NowPlayingItem = null;
                session.NowPlayingPositionTicks = null;
                session.IsPaused = false;
            }
        }
        /// 
        /// Gets the connection.
        /// 
        /// Type of the client.
        /// The app version.
        /// The device id.
        /// Name of the device.
        /// The remote end point.
        /// The user identifier.
        /// The username.
        /// SessionInfo.
        private SessionInfo GetSessionInfo(string clientType, string appVersion, string deviceId, string deviceName, string remoteEndPoint, Guid? userId, string username)
        {
            var key = clientType + deviceId + appVersion;
            var connection = _activeConnections.GetOrAdd(key, keyName => new SessionInfo
            {
                Client = clientType,
                DeviceId = deviceId,
                ApplicationVersion = appVersion,
                Id = Guid.NewGuid()
            });
            connection.DeviceName = deviceName;
            connection.UserId = userId;
            connection.UserName = username;
            connection.RemoteEndPoint = remoteEndPoint;
            if (!userId.HasValue)
            {
                connection.AdditionalUsers.Clear();
            }
            if (connection.SessionController == null)
            {
                connection.SessionController = _sessionFactories
                    .Select(i => i.GetSessionController(connection))
                    .FirstOrDefault(i => i != null);
            }
            return connection;
        }
        private List GetUsers(SessionInfo session)
        {
            var users = new List();
            if (session.UserId.HasValue)
            {
                var user = _userManager.GetUserById(session.UserId.Value);
                if (user == null)
                {
                    throw new InvalidOperationException("User not found");
                }
                users.Add(user);
                var additionalUsers = session.AdditionalUsers
                    .Select(i => _userManager.GetUserById(new Guid(i.UserId)))
                    .Where(i => i != null);
                users.AddRange(additionalUsers);
            }
            return users;
        }
        /// 
        /// Used to report that playback has started for an item
        /// 
        /// The info.
        /// Task.
        /// info
        public async Task OnPlaybackStart(PlaybackInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (info.SessionId == Guid.Empty)
            {
                throw new ArgumentNullException("info");
            }
            var session = Sessions.First(i => i.Id.Equals(info.SessionId));
            var item = info.Item;
            UpdateNowPlayingItem(session, item, false, false);
            session.CanSeek = info.CanSeek;
            session.QueueableMediaTypes = info.QueueableMediaTypes;
            var key = item.GetUserDataKey();
            var users = GetUsers(session);
            foreach (var user in users)
            {
                await OnPlaybackStart(user.Id, key, item).ConfigureAwait(false);
            }
            // Nothing to save here
            // Fire events to inform plugins
            EventHelper.QueueEventIfNotNull(PlaybackStart, this, new PlaybackProgressEventArgs
            {
                Item = item,
                Users = users
            }, _logger);
        }
        /// 
        /// Called when [playback start].
        /// 
        /// The user identifier.
        /// The user data key.
        /// The item.
        /// Task.
        private async Task OnPlaybackStart(Guid userId, string userDataKey, IHasUserData item)
        {
            var data = _userDataRepository.GetUserData(userId, userDataKey);
            data.PlayCount++;
            data.LastPlayedDate = DateTime.UtcNow;
            if (!(item is Video))
            {
                data.Played = true;
            }
            await _userDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.PlaybackStart, CancellationToken.None).ConfigureAwait(false);
        }
        /// 
        /// Used to report playback progress for an item
        /// 
        /// The info.
        /// Task.
        /// 
        /// positionTicks
        public async Task OnPlaybackProgress(PlaybackProgressInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (info.PositionTicks.HasValue && info.PositionTicks.Value < 0)
            {
                throw new ArgumentOutOfRangeException("positionTicks");
            }
            var session = Sessions.First(i => i.Id.Equals(info.SessionId));
            UpdateNowPlayingItem(session, info.Item, info.IsPaused, info.IsMuted, info.PositionTicks);
            var key = info.Item.GetUserDataKey();
            var users = GetUsers(session);
            foreach (var user in users)
            {
                await OnPlaybackProgress(user.Id, key, info.Item, info.PositionTicks).ConfigureAwait(false);
            }
            EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
            {
                Item = info.Item,
                Users = users,
                PlaybackPositionTicks = info.PositionTicks
            }, _logger);
        }
        private async Task OnPlaybackProgress(Guid userId, string userDataKey, BaseItem item, long? positionTicks)
        {
            var data = _userDataRepository.GetUserData(userId, userDataKey);
            if (positionTicks.HasValue)
            {
                UpdatePlayState(item, data, positionTicks.Value);
                await _userDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.PlaybackProgress, CancellationToken.None).ConfigureAwait(false);
            }
        }
        /// 
        /// Used to report that playback has ended for an item
        /// 
        /// The info.
        /// Task.
        /// info
        /// positionTicks
        public async Task OnPlaybackStopped(PlaybackStopInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (info.Item == null)
            {
                throw new ArgumentException("PlaybackStopInfo.Item cannot be null");
            }
            if (info.SessionId == Guid.Empty)
            {
                throw new ArgumentException("PlaybackStopInfo.SessionId cannot be Guid.Empty");
            }
            if (info.PositionTicks.HasValue && info.PositionTicks.Value < 0)
            {
                throw new ArgumentOutOfRangeException("positionTicks");
            }
            var session = Sessions.First(i => i.Id.Equals(info.SessionId));
            RemoveNowPlayingItem(session, info.Item);
            var key = info.Item.GetUserDataKey();
            var users = GetUsers(session);
            var playedToCompletion = false;
            foreach (var user in users)
            {
                playedToCompletion = await OnPlaybackStopped(user.Id, key, info.Item, info.PositionTicks).ConfigureAwait(false);
            }
            EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackStopEventArgs
            {
                Item = info.Item,
                Users = users,
                PlaybackPositionTicks = info.PositionTicks,
                PlayedToCompletion = playedToCompletion
            }, _logger);
        }
        private async Task OnPlaybackStopped(Guid userId, string userDataKey, BaseItem item, long? positionTicks)
        {
            var data = _userDataRepository.GetUserData(userId, userDataKey);
            bool playedToCompletion;
            if (positionTicks.HasValue)
            {
                playedToCompletion = UpdatePlayState(item, data, positionTicks.Value);
            }
            else
            {
                // If the client isn't able to report this, then we'll just have to make an assumption
                data.PlayCount++;
                data.Played = true;
                data.PlaybackPositionTicks = 0;
                playedToCompletion = true;
            }
            await _userDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.PlaybackFinished, CancellationToken.None).ConfigureAwait(false);
            return playedToCompletion;
        }
        
        /// 
        /// Updates playstate position for an item but does not save
        /// 
        /// The item
        /// User data for the item
        /// The current playback position
        private bool UpdatePlayState(BaseItem item, UserItemData data, long positionTicks)
        {
            var playedToCompletion = false;
            var hasRuntime = item.RunTimeTicks.HasValue && item.RunTimeTicks > 0;
            // If a position has been reported, and if we know the duration
            if (positionTicks > 0 && hasRuntime)
            {
                var pctIn = Decimal.Divide(positionTicks, item.RunTimeTicks.Value) * 100;
                // Don't track in very beginning
                if (pctIn < _configurationManager.Configuration.MinResumePct)
                {
                    positionTicks = 0;
                }
                // If we're at the end, assume completed
                else if (pctIn > _configurationManager.Configuration.MaxResumePct || positionTicks >= item.RunTimeTicks.Value)
                {
                    positionTicks = 0;
                    data.Played = playedToCompletion = true;
                }
                else
                {
                    // Enforce MinResumeDuration
                    var durationSeconds = TimeSpan.FromTicks(item.RunTimeTicks.Value).TotalSeconds;
                    if (durationSeconds < _configurationManager.Configuration.MinResumeDurationSeconds)
                    {
                        positionTicks = 0;
                        data.Played = playedToCompletion = true;
                    }
                }
            }
            else if (!hasRuntime)
            {
                // If we don't know the runtime we'll just have to assume it was fully played
                data.Played = playedToCompletion = true;
                positionTicks = 0;
            }
            if (item is Audio)
            {
                positionTicks = 0;
            }
            data.PlaybackPositionTicks = positionTicks;
            return playedToCompletion;
        }
        /// 
        /// Gets the session.
        /// 
        /// The session identifier.
        /// SessionInfo.
        /// 
        private SessionInfo GetSession(Guid sessionId)
        {
            var session = Sessions.First(i => i.Id.Equals(sessionId));
            if (session == null)
            {
                throw new ResourceNotFoundException(string.Format("Session {0} not found.", sessionId));
            }
            return session;
        }
        /// 
        /// Gets the session for remote control.
        /// 
        /// The session id.
        /// SessionInfo.
        /// 
        private SessionInfo GetSessionForRemoteControl(Guid sessionId)
        {
            var session = GetSession(sessionId);
            if (!session.SupportsRemoteControl)
            {
                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
            }
            return session;
        }
        /// 
        /// Sends the system command.
        /// 
        /// The session id.
        /// The command.
        /// The cancellation token.
        /// Task.
        public Task SendSystemCommand(Guid sessionId, SystemCommand command, CancellationToken cancellationToken)
        {
            var session = GetSessionForRemoteControl(sessionId);
            return session.SessionController.SendSystemCommand(command, cancellationToken);
        }
        /// 
        /// Sends the message command.
        /// 
        /// The session id.
        /// The command.
        /// The cancellation token.
        /// Task.
        public Task SendMessageCommand(Guid sessionId, MessageCommand command, CancellationToken cancellationToken)
        {
            var session = GetSessionForRemoteControl(sessionId);
            return session.SessionController.SendMessageCommand(command, cancellationToken);
        }
        /// 
        /// Sends the play command.
        /// 
        /// The session id.
        /// The command.
        /// The cancellation token.
        /// Task.
        public Task SendPlayCommand(Guid sessionId, PlayRequest command, CancellationToken cancellationToken)
        {
            var session = GetSessionForRemoteControl(sessionId);
            var items = command.ItemIds.Select(i => _libraryManager.GetItemById(new Guid(i)))
                .ToList();
            if (items.Any(i => i.LocationType == LocationType.Virtual))
            {
                throw new ArgumentException("Virtual items are not playable.");
            }
            if (command.PlayCommand != PlayCommand.PlayNow)
            {
                if (items.Any(i => !session.QueueableMediaTypes.Contains(i.MediaType, StringComparer.OrdinalIgnoreCase)))
                {
                    throw new ArgumentException(string.Format("{0} is unable to queue the requested media type.", session.DeviceName ?? session.Id.ToString()));
                }
            }
            else
            {
                if (items.Any(i => !session.PlayableMediaTypes.Contains(i.MediaType, StringComparer.OrdinalIgnoreCase)))
                {
                    throw new ArgumentException(string.Format("{0} is unable to play the requested media type.", session.DeviceName ?? session.Id.ToString()));
                }
            }
            return session.SessionController.SendPlayCommand(command, cancellationToken);
        }
        /// 
        /// Sends the browse command.
        /// 
        /// The session id.
        /// The command.
        /// The cancellation token.
        /// Task.
        public Task SendBrowseCommand(Guid sessionId, BrowseRequest command, CancellationToken cancellationToken)
        {
            var session = GetSessionForRemoteControl(sessionId);
            return session.SessionController.SendBrowseCommand(command, cancellationToken);
        }
        /// 
        /// Sends the playstate command.
        /// 
        /// The session id.
        /// The command.
        /// The cancellation token.
        /// Task.
        public Task SendPlaystateCommand(Guid sessionId, PlaystateRequest command, CancellationToken cancellationToken)
        {
            var session = GetSessionForRemoteControl(sessionId);
            if (command.Command == PlaystateCommand.Seek && !session.CanSeek)
            {
                throw new ArgumentException(string.Format("Session {0} is unable to seek.", session.Id));
            }
            return session.SessionController.SendPlaystateCommand(command, cancellationToken);
        }
        /// 
        /// Sends the restart required message.
        /// 
        /// The cancellation token.
        /// Task.
        public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
        {
            var sessions = Sessions.Where(i => i.IsActive && i.SessionController != null).ToList();
            var tasks = sessions.Select(session => Task.Run(async () =>
            {
                try
                {
                    await session.SessionController.SendRestartRequiredNotification(cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error in SendRestartRequiredNotification.", ex);
                }
            }, cancellationToken));
            return Task.WhenAll(tasks);
        }
        /// 
        /// Sends the server shutdown notification.
        /// 
        /// The cancellation token.
        /// Task.
        public Task SendServerShutdownNotification(CancellationToken cancellationToken)
        {
            var sessions = Sessions.Where(i => i.IsActive && i.SessionController != null).ToList();
            var tasks = sessions.Select(session => Task.Run(async () =>
            {
                try
                {
                    await session.SessionController.SendServerShutdownNotification(cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error in SendServerShutdownNotification.", ex);
                }
            }, cancellationToken));
            return Task.WhenAll(tasks);
        }
        /// 
        /// Sends the server restart notification.
        /// 
        /// The cancellation token.
        /// Task.
        public Task SendServerRestartNotification(CancellationToken cancellationToken)
        {
            var sessions = Sessions.Where(i => i.IsActive && i.SessionController != null).ToList();
            var tasks = sessions.Select(session => Task.Run(async () =>
            {
                try
                {
                    await session.SessionController.SendServerRestartNotification(cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error in SendServerRestartNotification.", ex);
                }
            }, cancellationToken));
            return Task.WhenAll(tasks);
        }
        /// 
        /// Adds the additional user.
        /// 
        /// The session identifier.
        /// The user identifier.
        /// Cannot modify additional users without authenticating first.
        /// The requested user is already the primary user of the session.
        public void AddAdditionalUser(Guid sessionId, Guid userId)
        {
            var session = GetSession(sessionId);
            if (!session.UserId.HasValue)
            {
                throw new UnauthorizedAccessException("Cannot modify additional users without authenticating first.");
            }
            if (session.UserId.Value == userId)
            {
                throw new ArgumentException("The requested user is already the primary user of the session.");
            }
            if (session.AdditionalUsers.All(i => new Guid(i.UserId) != userId))
            {
                var user = _userManager.GetUserById(userId);
                session.AdditionalUsers.Add(new SessionUserInfo
                {
                    UserId = userId.ToString("N"),
                    UserName = user.Name
                });
            }
        }
        /// 
        /// Removes the additional user.
        /// 
        /// The session identifier.
        /// The user identifier.
        /// Cannot modify additional users without authenticating first.
        /// The requested user is already the primary user of the session.
        public void RemoveAdditionalUser(Guid sessionId, Guid userId)
        {
            var session = GetSession(sessionId);
            if (!session.UserId.HasValue)
            {
                throw new UnauthorizedAccessException("Cannot modify additional users without authenticating first.");
            }
            if (session.UserId.Value == userId)
            {
                throw new ArgumentException("The requested user is already the primary user of the session.");
            }
            var user = session.AdditionalUsers.FirstOrDefault(i => new Guid(i.UserId) == userId);
            if (user != null)
            {
                session.AdditionalUsers.Remove(user);
            }
        }
        /// 
        /// Authenticates the new session.
        /// 
        /// The user.
        /// The password.
        /// Type of the client.
        /// The application version.
        /// The device identifier.
        /// Name of the device.
        /// The remote end point.
        /// Task{SessionInfo}.
        /// 
        public async Task AuthenticateNewSession(User user, string password, string clientType, string appVersion, string deviceId, string deviceName, string remoteEndPoint)
        {
            var result = await _userManager.AuthenticateUser(user, password).ConfigureAwait(false);
            if (!result)
            {
                throw new UnauthorizedAccessException("Invalid user or password entered.");
            }
            return await LogSessionActivity(clientType, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
        }
    }
}