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

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

        static Account()
        {
            Account.jsonResolver = new IgnorableSerializerContractResolver();
            Account.jsonResolver.Ignore(typeof(Account), "userid");
            Account.jsonResolver.Ignore(typeof(Account), "company");
        }

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

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

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

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

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

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

        /// <summary>
        /// Get current account
        /// </summary>
        /// <param name="accessToken">Token</param>
        public static Account GetAccount(string accessToken)
        {
            Rest.Properties properties = new Rest.Properties(accessToken, TeamViewer.URL.ACCOUNT, Rest.Method.GET);
            Rest.Connection connection = new Rest.Connection(properties);

            string json = connection.SendRequest();

            Account result = null;

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

            return result;
        }

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

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

            connection.SendRequest();
        }
    }
}
