using System.Net;

namespace sync_time_dh
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
#if DEBUG
            args = new string[1];
            args[0] = "172.16.54.30";
#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 requestUrl1 = $"{baseUrlU}/cgi-bin/global.cgi?action=setCurrentTime&time={DateTime.Now:yyyy-MM-dd HH:mm:ss}";
                string requestUrl2 = $"{baseUrlU}/cgi-bin/configManager.cgi?action=setConfig&NTP.Enable=false&NTP.TimeZone=3&NTP.TimeZoneDesc=Moscow";
                string requestUrl3 = $"{baseUrlU}/cgi-bin/configManager.cgi?action=setConfig&Locales.TimeFormat=dd-MM-yyyy HH:mm:ss";
                var response1 = await httpClient.GetAsync(requestUrl1);
                var response2 = await httpClient.GetAsync(requestUrl2);
                var response3 = await httpClient.GetAsync(requestUrl3);
                try
                {
                    response1.EnsureSuccessStatusCode();
                    response2.EnsureSuccessStatusCode();
                    response3.EnsureSuccessStatusCode();
                }
                catch (Exception)
                {
                    Console.WriteLine("1");
                    return;
                }
                var r1 = await response1.Content.ReadAsStringAsync();
                var r2 = await response2.Content.ReadAsStringAsync();
                var r3 = await response3.Content.ReadAsStringAsync();
                if (r1.StartsWith("OK") && r2.StartsWith("OK") && r3.StartsWith("OK"))
                {
                    Console.WriteLine("0");
                }
                else Console.WriteLine("1");
            }
        }
    }
}
