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

    [DataContract]
    public class Device
    {
        private static IgnorableSerializerContractResolver jsonSaveResolver;
        private static IgnorableSerializerContractResolver jsonCreateResolver;

        static Device()
        {
            Device.jsonSaveResolver = new IgnorableSerializerContractResolver();
            Device.jsonSaveResolver.Ignore(typeof(Device), "device_id");
            Device.jsonSaveResolver.Ignore(typeof(Device), "remotecontrol_id");
            Device.jsonSaveResolver.Ignore(typeof(Device), "online_state");
            Device.jsonSaveResolver.Ignore(typeof(Device), "supported_features");

            Device.jsonCreateResolver = new IgnorableSerializerContractResolver();
            Device.jsonCreateResolver.Ignore(typeof(Device), "device_id");
            Device.jsonCreateResolver.Ignore(typeof(Device), "online_state");
            Device.jsonCreateResolver.Ignore(typeof(Device), "supported_features");
        }

        [DataMember(Name = "device_id")]
        public string DeviceId { get; set; }
        
        [DataMember(Name = "remotecontrol_id")]
        public string RemoteControlId { get; set; }

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

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

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

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

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

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

        /// <summary>
        /// Save device changes
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="device">Device</param>
        public static void SaveDevice(string accessToken, Device device)
        {
            string json = JsonConvert.SerializeObject(device, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = Device.jsonSaveResolver });

            string url = string.Format("{0}/{1}", TeamViewer.URL.DEVICES, device.DeviceId);

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

            connection.SendRequest();
        }

        /// <summary>
        /// Create new device
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="device">Device</param>
        public static Device CreateDevice(string accessToken, Device device)
        {
            string json = JsonConvert.SerializeObject(device, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = Device.jsonCreateResolver });

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

            json = connection.SendRequest();

            Device result = null;

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

            return result;
        }

        /// <summary>
        /// Delete device
        /// </summary>
        /// <param name="accessToken">Token</param>
        /// <param name="device">Device</param>
        public static void DeleteDevice(string accessToken, Device device)
        {
            string url = string.Format("{0}/{1}", TeamViewer.URL.DEVICES, device.DeviceId);

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

            connection.SendRequest();
        }
    }
}
