using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using PepperDash.Core; using PepperDash.Essentials.Core; using PepperDash.Essentials.Devices.Common.VideoCodec; namespace PepperDash.Essentials.Devices.Common.Codec { /// /// Defines the API for codecs with a directory /// public interface IHasDirectory { event EventHandler DirectoryResultReturned; CodecDirectory DirectoryRoot { get; } CodecDirectory CurrentDirectoryResult { get; } CodecPhonebookSyncState PhonebookSyncState { get; } void SearchDirectory(string searchString); void GetDirectoryFolderContents(string folderId); void SetCurrentDirectoryToRoot(); void GetDirectoryParentFolderContents(); BoolFeedback CurrentDirectoryResultIsNotDirectoryRoot { get; } } public interface IHasDirectoryHistoryStack : IHasDirectory { Stack DirectoryBrowseHistoryStack { get; } } /// /// /// public class DirectoryEventArgs : EventArgs { public CodecDirectory Directory { get; set; } public bool DirectoryIsOnRoot { get; set; } } /// /// Represents a codec directory /// public class CodecDirectory { /// /// Represents the contents of the directory /// We don't want to serialize this for messages to MobileControl. MC can combine Contacts and Folders to get the same data /// [JsonIgnore] public List CurrentDirectoryResults { get; private set; } [JsonProperty("contacts")] public List Contacts { get { return CurrentDirectoryResults.OfType().Cast().ToList(); } } [JsonProperty("folders")] public List Folders { get { return CurrentDirectoryResults.OfType().Cast().ToList(); } } /// /// Used to store the ID of the current folder for CurrentDirectoryResults /// [JsonProperty("resultsFolderId")] public string ResultsFolderId { get; set; } public CodecDirectory() { CurrentDirectoryResults = new List(); } /// /// Adds folders to the directory /// /// public void AddFoldersToDirectory(List folders) { if(folders != null) CurrentDirectoryResults.AddRange(folders); SortDirectory(); } /// /// Adds contacts to the directory /// /// public void AddContactsToDirectory(List contacts) { if(contacts != null) CurrentDirectoryResults.AddRange(contacts); SortDirectory(); } /// /// Filters the CurrentDirectoryResults by the predicate /// /// public void FilterContacts(Func predicate) { CurrentDirectoryResults = CurrentDirectoryResults.Where(predicate).ToList(); } /// /// Sorts the DirectoryResults list to display all folders alphabetically, then all contacts alphabetically /// private void SortDirectory() { var sortedFolders = new List(); sortedFolders.AddRange(CurrentDirectoryResults.Where(f => f is DirectoryFolder)); sortedFolders.OrderBy(f => f.Name); var sortedContacts = new List(); sortedContacts.AddRange(CurrentDirectoryResults.Where(c => c is DirectoryContact)); sortedFolders.OrderBy(c => c.Name); CurrentDirectoryResults.Clear(); CurrentDirectoryResults.AddRange(sortedFolders); CurrentDirectoryResults.AddRange(sortedContacts); } } /// /// Used to decorate a contact to indicate it can be invided to a meeting /// public interface IInvitableContact { bool IsInvitableContact { get; } } public class InvitableDirectoryContact : DirectoryContact, IInvitableContact { [JsonProperty("isInvitableContact")] public bool IsInvitableContact { get { return this is IInvitableContact; } } } /// /// Represents an item in the directory /// public class DirectoryItem : ICloneable { public object Clone() { return this.MemberwiseClone(); } [JsonProperty("folderId")] public string FolderId { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("parentFolderId")] public string ParentFolderId { get; set; } } /// /// Represents a folder type DirectoryItem /// public class DirectoryFolder : DirectoryItem { [JsonProperty("contacts")] public List Contacts { get; set; } public DirectoryFolder() { Contacts = new List(); } } /// /// Represents a contact type DirectoryItem /// public class DirectoryContact : DirectoryItem { [JsonProperty("contactId")] public string ContactId { get; set; } [JsonProperty("title")] public string Title { get; set; } [JsonProperty("contactMethods")] public List ContactMethods { get; set; } public DirectoryContact() { ContactMethods = new List(); } } /// /// Represents a method of contact for a contact /// public class ContactMethod { [JsonProperty("contactMethodId")] public string ContactMethodId { get; set; } [JsonProperty("number")] public string Number { get; set; } [JsonProperty("device")] [JsonConverter(typeof(StringEnumConverter))] public eContactMethodDevice Device { get; set; } [JsonProperty("callType")] [JsonConverter(typeof(StringEnumConverter))] public eContactMethodCallType CallType { get; set; } } /// /// /// public enum eContactMethodDevice { Unknown = 0, Mobile, Other, Telephone, Video } /// /// /// public enum eContactMethodCallType { Unknown = 0, Audio, Video } }