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

    [DataContract]
    public class Devices
    {
        [DataMember(Name = "devices")]
        public Device[] Items { get; set; }

        /// <summary>
        /// Get devices for the account
        /// </summary>
        /// <param name="accessToken">Token</param>
        public static Devices GetDevices(string accessToken, string state = "", string groupId = "")
        {
            string url = TeamViewer.URL.DEVICES;

            if (!string.IsNullOrEmpty(state))
                url += "?online_state=" + state;

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

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

            string json = connection.SendRequest();

            Devices result = null;

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

            return result;
        }
    }
}
