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

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

        static Contact()
        {
            Contact.jsonResolver = new IgnorableSerializerContractResolver();
            Contact.jsonResolver.Ignore(typeof(Contact), "contact_id");
            Contact.jsonResolver.Ignore(typeof(Contact), "name");
            Contact.jsonResolver.Ignore(typeof(Contact), "groupid");
            Contact.jsonResolver.Ignore(typeof(Contact), "online_state");
            Contact.jsonResolver.Ignore(typeof(Contact), "profilepicture_url");
            Contact.jsonResolver.Ignore(typeof(Contact), "supported_features");
            Contact.jsonResolver.Ignore(typeof(Contact), "invitations");
        }

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

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

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

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

        [DataMember(Name = "groupname")]
        public string GroupName { get; set; } // used only on creation

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

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

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

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

        [DataMember(Name = "invitations")]
        public Invitation[] Invitations { get; set; }

        [DataMember(Name = "invite")]
        public bool SendInviteEmail { get; set; } // used only on creation

        /// <summary>
        /// Add a contact
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="contact">Contact</param>
        public static Contact CreateContact(string accessToken, Contact contact)
        {
            string json = JsonConvert.SerializeObject(contact, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = Contact.jsonResolver });

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

            json = connection.SendRequest();

            Contact result = null;

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

            return result;
        }

        /// <summary>
        /// Delete contact
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="contact">Contact</param>
        public static void DeleteContact(string accessToken, Contact contact)
        {
            string url = string.Format("{0}/{1}", TeamViewer.URL.CONTACTS, contact.ContactId);

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

            connection.SendRequest();
        }
    }
}
