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

    [DataContract]
    public class Ping
    {
        [DataMember(Name = "token_valid")]
        public bool IsTokenValid { get; set; }

        /// <summary>
        /// Check if token is valid
        /// </summary>
        /// <param name="accessToken">Token</param>
        public static bool Check(string accessToken)
        {
            Rest.Properties properties = new Rest.Properties(accessToken, TeamViewer.URL.PING, Rest.Method.GET);
            Rest.Connection connection = new Rest.Connection(properties);

            string json = connection.SendRequest();

            if (!string.IsNullOrEmpty(json))
                return JsonConvert.DeserializeObject<Ping>(json).IsTokenValid;
            else
                return false;
        }
    }
}
