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 sessions.
///
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 participants.
///
/// The participants, or members of the group.
public readonly ConcurrentDictionary Participants =
new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
///
/// Checks if a session is in this group.
///
/// true if the session is in this group; false otherwise.
public bool ContainsSession(string sessionId)
{
return Participants.ContainsKey(sessionId);
}
///
/// Adds the session to the group.
///
/// The session.
public void AddSession(SessionInfo session)
{
if (ContainsSession(session.Id.ToString())) return;
var member = new GroupMember();
member.Session = session;
member.Ping = DefaulPing;
member.IsBuffering = false;
Participants[session.Id.ToString()] = member;
}
///
/// Removes the session from the group.
///
/// The session.
public void RemoveSession(SessionInfo session)
{
if (!ContainsSession(session.Id.ToString())) return;
GroupMember member;
Participants.Remove(session.Id.ToString(), out member);
}
///
/// Updates the ping of a session.
///
/// The session.
/// The ping.
public void UpdatePing(SessionInfo session, long ping)
{
if (!ContainsSession(session.Id.ToString())) return;
Participants[session.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 session in Participants.Values)
{
max = Math.Max(max, session.Ping);
}
return max;
}
///
/// Sets the session's buffering state.
///
/// The session.
/// The state.
public void SetBuffering(SessionInfo session, bool isBuffering)
{
if (!ContainsSession(session.Id.ToString())) return;
Participants[session.Id.ToString()].IsBuffering = isBuffering;
}
///
/// Gets the group buffering state.
///
/// true if there is a session buffering in the group; false otherwise.
public bool IsBuffering()
{
foreach (var session in Participants.Values)
{
if (session.IsBuffering) return true;
}
return false;
}
///
/// Checks if the group is empty.
///
/// true if the group is empty; false otherwise.
public bool IsEmpty()
{
return Participants.Count == 0;
}
}
}