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

    [DataContract]
    public class Groups
    {
        [DataMember(Name = "groups")]
        public Group[] Items { get; set; }

        /// <summary>
        /// Get groups for the account
        /// </summary>
        /// <param name="accessToken">Token</param>
        public static Groups GetGroups(string accessToken, string name = "", bool shared = false)
        {
            string url = TeamViewer.URL.GROUPS;

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

            if (shared)
            {
                if (!string.IsNullOrEmpty(name))
                    url += "&shared=true";
                else
                    url += "?shared=true";
            }

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

            string json = connection.SendRequest();

            Groups result = null;

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

            return result;
        }
    }
}
