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

    [DataContract]
    public class Meetings
    {
        [DataMember(Name = "meetings")]
        public Meeting[] Items { get; set; }

        /// <summary>
        /// Get meetings for the account
        /// </summary>
        /// <param name="accessToken">Token</param>
        public static Meetings GetMeetings(string accessToken, DateTime? dateStart = null, DateTime? dateEnd = null)
        {
            string url = TeamViewer.URL.MEETINGS;

            if (dateStart.HasValue)
                url += "?from_date=" + dateStart.Value.ToUniversalTime().ToString("yyyy-MM-dd");

            if (dateEnd.HasValue)
            {
                if (dateStart.HasValue)
                    url += "&to_date=" + dateEnd.Value.ToUniversalTime().ToString("yyyy-MM-dd");
                else
                    url += "?to_date=" + dateEnd.Value.ToUniversalTime().ToString("yyyy-MM-dd");
            }

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

            string json = connection.SendRequest();

            Meetings result = null;

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

            return result;
        }
    }
}
