namespace TeamViewerApi.TeamViewer
{
    using System;
    using System.Net;
    using System.Runtime.Serialization;
    using Newtonsoft.Json;

    [DataContract]
    public class Sessions
    {
        [DataMember(Name = "sessions")]
        public Session[] Items { get; set; }

        /// <summary>
        /// Get registered sessions
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="fullList">Retreive full session data</param>
        /// <param name="groupId">Group filter</param>
        /// <param name="state">State filter</param>
        public static Sessions GetSessions(string accessToken, bool fullList = false, string groupId = "", string state = "")
        {
            string url = TeamViewer.URL.SESSIONS;

            if (fullList)
                url += "?full_list=true";

            if (!string.IsNullOrEmpty(groupId))
            {
                if (fullList)
                    url += "&groupid=" + groupId;
                else
                    url += "?groupid=" + groupId;
            }

            if (!string.IsNullOrEmpty(state) && (state == "open" || state == "closed"))
            {
                if (fullList || !string.IsNullOrEmpty(groupId))
                    url += "&state=" + state;
                else
                    url += "?state=" + state;
            }

            Rest.Properties properties = new Rest.Properties(accessToken, url, Rest.Method.GET);
            Rest.Connection connection = new Rest.Connection(properties);

            string json = connection.SendRequest();

            Sessions result = null;

            if (!string.IsNullOrEmpty(json))
                result = JsonConvert.DeserializeObject<Sessions>(json);

            return result;
        }
    }
}
