using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Session;
namespace MediaBrowser.Controller.Syncplay
{
///
/// Class GroupInfo.
///
public class GroupInfo
{
///
/// Default ping value used for users.
///
public readonly long DefaulPing = 500;
///
/// Gets or sets the group identifier.
///
/// The group identifier.
public readonly Guid GroupId = Guid.NewGuid();
///
/// Gets or sets the playing item.
///
/// The playing item.
public BaseItem PlayingItem { get; set; }
///
/// Gets or sets whether playback is paused.
///
/// Playback is paused.
public bool IsPaused { get; set; }
///
/// Gets or sets the position ticks.
///
/// The position ticks.
public long PositionTicks { get; set; }
///
/// Gets or sets the last activity.
///
/// The last activity.
public DateTime LastActivity { get; set; }
///
/// Gets the partecipants.
///
/// The partecipants.
public readonly ConcurrentDictionary Partecipants =
new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
///
/// Checks if a user is in this group.
///
/// true if the user is in this group; false otherwise.
public bool ContainsUser(string sessionId)
{
return Partecipants.ContainsKey(sessionId);
}
///
/// Adds the user to the group.
///
/// The session.
public void AddUser(SessionInfo user)
{
if (ContainsUser(user.Id.ToString())) return;
var member = new GroupMember();
member.Session = user;
member.Ping = DefaulPing;
member.IsBuffering = false;
Partecipants[user.Id.ToString()] = member;
}
///
/// Removes the user from the group.
///
/// The session.
public void RemoveUser(SessionInfo user)
{
if (!ContainsUser(user.Id.ToString())) return;
GroupMember member;
Partecipants.Remove(user.Id.ToString(), out member);
}
///
/// Updates the ping of a user.
///
/// The session.
/// The ping.
public void UpdatePing(SessionInfo user, long ping)
{
if (!ContainsUser(user.Id.ToString())) return;
Partecipants[user.Id.ToString()].Ping = ping;
}
///
/// Gets the highest ping in the group.
///
/// The highest ping in the group.
public long GetHighestPing()
{
long max = Int64.MinValue;
foreach (var user in Partecipants.Values)
{
max = Math.Max(max, user.Ping);
}
return max;
}
///
/// Sets the user's buffering state.
///
/// The session.
/// The state.
public void SetBuffering(SessionInfo user, bool isBuffering)
{
if (!ContainsUser(user.Id.ToString())) return;
Partecipants[user.Id.ToString()].IsBuffering = isBuffering;
}
///
/// Gets the group buffering state.
///
/// true if there is a user buffering in the group; false otherwise.
public bool IsBuffering()
{
foreach (var user in Partecipants.Values)
{
if (user.IsBuffering) return true;
}
return false;
}
///
/// Checks if the group is empty.
///
/// true if the group is empty; false otherwise.
public bool IsEmpty()
{
return Partecipants.Count == 0;
}
}
}