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

    [DataContract]
    public class Group
    {
        private static IgnorableSerializerContractResolver jsonResolver;

        static Group()
        {
            Group.jsonResolver = new IgnorableSerializerContractResolver();
            Group.jsonResolver.Ignore(typeof(Group), "id");
            Group.jsonResolver.Ignore(typeof(Group), "shared_with");
            Group.jsonResolver.Ignore(typeof(Group), "owner");
            Group.jsonResolver.Ignore(typeof(Group), "permissions");
        }

        [DataMember(Name = "id")]
        public string GroupId { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "shared_with")]
        public User[] SharedWithUsers { get; set; }

        [DataMember(Name = "owner")]
        public User Owner { get; set; }

        [DataMember(Name = "permissions")]
        public string Permissions { get; set; }

        /// <summary>
        /// Get group info
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="groupId">Group Id</param>
        public static Group GetGroup(string accessToken, string groupId)
        {
            string url = string.Format("{0}/{1}", TeamViewer.URL.GROUPS, groupId);

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

            string json = connection.SendRequest();

            Group result = null;

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

            return result;
        }

        /// <summary>
        /// Save group changes
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="group">Group</param>
        public static void SaveGroup(string accessToken, Group group)
        {
            string json = JsonConvert.SerializeObject(group, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = Group.jsonResolver });

            string url = string.Format("{0}/{1}", TeamViewer.URL.GROUPS, group.GroupId);

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

            connection.SendRequest();
        }

        /// <summary>
        /// Creates new group
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="group">Group</param>
        public static Group CreateGroup(string accessToken, Group group)
        {
            string json = JsonConvert.SerializeObject(group, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = Group.jsonResolver });

            Rest.Properties properties = new Rest.Properties(accessToken, TeamViewer.URL.GROUPS, Rest.Method.POST, json);
            Rest.Connection connection = new Rest.Connection(properties);
            
            json = connection.SendRequest();

            Group result = null;

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

            return result;
        }

        /// <summary>
        /// Delete group
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="group">Group</param>
        public static void DeleteGroup(string accessToken, Group group)
        {
            string url = string.Format("{0}/{1}", TeamViewer.URL.GROUPS, group.GroupId);

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

            connection.SendRequest();
        }
    }
}
