using System.Net;
using System.Text;

namespace sync_time_hik
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
#if DEBUG
            args = new string[1];
            args[0] = "172.16.50.13";
#endif
            string baseUrlU = $"http://{args[0]}";
            var credCacheU = new CredentialCache();
            credCacheU.Add(new Uri(baseUrlU), "Digest", new NetworkCredential("admin", "4NUDZhJ7"));
            using (var handler = new HttpClientHandler
            {
                Credentials = credCacheU,
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
            })
            using (var httpClient = new HttpClient(handler))
            {
                string requestUrl = $"{baseUrlU}/ISAPI/System/time";

                string xmlBody = $@"<?xml version=""1.0"" encoding=""UTF-8""?><Time><timeMode>manual</timeMode><localTime>{DateTime.Now:yyyy-MM-ddTHH:mm:ss}</localTime><timeZone>CST-3:00:00</timeZone></Time>";

                // Создаем контент для PUT запроса
                var content = new StringContent(xmlBody, Encoding.UTF8, "application/xml");

                // Выполняем PUT запрос
                var response = await httpClient.PutAsync(requestUrl, content);
                response.EnsureSuccessStatusCode();
                var r = response.ReasonPhrase;
                if (r.StartsWith("OK"))
                {
                    Console.WriteLine("0");
                }
                else Console.WriteLine("1");
            }
#if DEBUG
           Console.ReadKey();
#endif
        }
    }
}
