using System.Net;
using System.Net.NetworkInformation;

internal class Program
{
    private static async Task Main(string[] args)
    {
#if DEBUG
        args = new string[4];
        args[0] = "set";
        args[1] = "172.16.51.2";
        args[2] = "admin";
        args[3] = "4NUDZhJ7";
#endif
        string server, login, password;
        if (args.Length != 4) return;
        server = args[1];
        login = args[2];
        password = args[3];


        switch (args[0])
        {
            case "check":
                string baseUrl = $"http://{server}";
                var credCache = new CredentialCache();
                credCache.Add(new Uri(baseUrl), "Digest", new NetworkCredential(login, password));
                using (var handler = new HttpClientHandler
                {
                    Credentials = credCache,
                    ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
                })
                using (var httpClient = new HttpClient(handler))
                {
                    string requestUrl = $"{baseUrl}/cgi-bin/global.cgi?action=getCurrentTime";
                    var response = await httpClient.GetAsync(requestUrl);
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("1");
                        return;
                    }
                    var r = await response.Content.ReadAsStringAsync();
                    var r2 = Convert.ToDateTime(r.Split("=")[1]);
                    if (r2 <= DateTime.Now.AddMinutes(2) && r2 >= DateTime.Now.AddMinutes(-2))
                        Console.WriteLine("0");
                    else Console.WriteLine("1");
                }
                break;
            case "update":
                string baseUrlU = $"http://{server}";
                var credCacheU = new CredentialCache();
                credCacheU.Add(new Uri(baseUrlU), "Digest", new NetworkCredential(login, password));
                using (var handler = new HttpClientHandler
                {
                    Credentials = credCacheU,
                    ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
                })
                using (var httpClient = new HttpClient(handler))
                {
                    string requestUrl = $"{baseUrlU}/cgi-bin/global.cgi?action=setCurrentTime&time={DateTime.Now:yyyy-MM-dd HH:mm:ss}";
                    var response = await httpClient.GetAsync(requestUrl);
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("1");
                        return;
                    }
                    var r = await response.Content.ReadAsStringAsync();
                    //r = r.Replace("\r\n", "");
                    if (r.StartsWith("OK"))
                    {
                        Console.WriteLine("0");
                    }
                    else Console.WriteLine("1");
                }
                break;
            case "set":
                var list = new List<string>();
                var lines = File.ReadAllLines("listCam.csv");
                foreach (var line in lines)
                {
                    list.Add(line);
                }
                foreach (var host in list)
                {
                    using (Ping ping = new Ping())
                    {
                        PingReply pingReply = ping.Send(host);
                        if (pingReply.Status != IPStatus.Success)
                        {
                            Console.WriteLine($"{host}\t NO PING");
                            continue;
                        }
                    }

                    string baseUrlS = $"http://{host}";
                    var credCacheS = new CredentialCache();
                    credCacheS.Add(new Uri(baseUrlS), "Digest", new NetworkCredential(login, password));
                    using (var handler = new HttpClientHandler
                    {
                        Credentials = credCacheS,
                        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
                    })
                    using (var httpClient = new HttpClient(handler))
                    {
                        string requestUrl = $"{baseUrlS}/cgi-bin/configManager.cgi?" +
                            $"action=setConfig&" +
                            $"SNMP.Enable=true&" +
                            $"SNMP.V2Enable=false&" +
                            $"SNMP.V1Enable=false&" +
                            $"SNMP.V3Enable=false&" +
                            $"SNMP.Port=161&" +
                            $"SNMP.ReadCommon=public&" +
                            $"SNMP.WriteCommon=private";
                        var response = await httpClient.GetAsync(requestUrl);
                        response.EnsureSuccessStatusCode();
                        var r = await response.Content.ReadAsStringAsync();
                        if (r.StartsWith("OK"))
                        {
                            string requestUrl2 = $"{baseUrlS}/cgi-bin/configManager.cgi?" +
                            $"action=setConfig&" +
                            $"SNMP.Enable=true&" +
                            $"SNMP.V2Enable=true&" +
                            $"SNMP.V1Enable=false&" +
                            $"SNMP.V3Enable=false&" +
                            $"SNMP.Port=161&" +
                            $"SNMP.ReadCommon=public&" +
                            $"SNMP.WriteCommon=private";
                            var response2 = await httpClient.GetAsync(requestUrl2);
                            var r2 = await response2.Content.ReadAsStringAsync();
                            if (r2.StartsWith("OK")) Console.WriteLine("0");
                            else Console.WriteLine("1");
                        }
                        else Console.WriteLine("1");
                    }
                }
                break;
            default:
                break;
        }
    }
}