//  
//   Rebex Sample Code License
// 
//   Copyright 2025, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using Rebex.Net;

namespace Rebex.Samples
{
    /// <summary>
    /// This sample connects to a Syslog server, sends a message and disconnects.
    /// </summary>
    class Program
    {
        static SyslogTransportProtocol Protocol;
        static string Hostname;
        static int Port;
        static bool AcceptAll;
        static string Message;

        static void Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            // parse arguments
            if (!ParseArguments(args))
            {
                ShowHelp();
                return;
            }

            // create Syslog client instance
            using (var client = new SyslogClient())
            {
                if (AcceptAll)
                {
                    // skip certificate validation is requested
                    // DO NOT do this in production environment!
                    client.Settings.SslAcceptAllCertificates = true;
                }

                Console.WriteLine("Connecting to {0}:{1} using '{2}' protocol.", Hostname, Port, Protocol);

                // connect to desired Syslog server
                client.Connect(Hostname, Port, Protocol);

                Console.WriteLine("Client connected.");

                // send a message
                client.Send(Message);

                Console.WriteLine("Message sent.");
            }
        }

        static bool ParseArguments(string[] args)
        {
            if (args == null || args.Length < 3 || args.Length > 4)
            {
                Console.WriteLine("ERROR: Wrong number of arguments.");
                return false;
            }

            if (!ParseProtocol(args[0]))
                return false;

            if (!ParseAddress(args[1]))
                return false;

            Message = args[2];

            if (args.Length == 4)
            {
                if (args[3].Equals("-acceptAll", StringComparison.OrdinalIgnoreCase))
                {
                    AcceptAll = true;
                }
                else
                {
                    Console.WriteLine("ERROR: Invalid argument '{0}'.", args[3]);
                    return false;
                }
            }

            return true;
        }

        private static bool ParseProtocol(string protocol)
        {
            switch (protocol.ToUpperInvariant())
            {
                case "UDP": Protocol = SyslogTransportProtocol.Udp; return true;
                case "TCP": Protocol = SyslogTransportProtocol.Tcp; return true;
                case "TLS": Protocol = SyslogTransportProtocol.Tls; return true;
                default: Console.WriteLine("ERROR: Invalid protocol."); return false;
            }
        }

        private static bool ParseAddress(string address)
        {
            if (address.Contains(":"))
            {
                var parts = address.Split(':');
                if (parts.Length != 2)
                {
                    Console.WriteLine("ERROR: Invalid address.");
                    return false;
                }

                Hostname = parts[0];
                if (!int.TryParse(parts[1], out Port) || Port <= 0 || Port > ushort.MaxValue)
                {
                    Console.WriteLine("ERROR: Invalid port.");
                    return false;
                }
            }
            else
            {
                Hostname = address;
                Port = Protocol == SyslogTransportProtocol.Tls
                     ? SyslogServer.DefaultTlsPort
                     : SyslogServer.DefaultPort;
            }
            return true;
        }

        static void ShowHelp()
        {
            Console.WriteLine();
            Console.WriteLine("Usage: SyslogClient protocol host[:port] message [-acceptAll]");
            Console.WriteLine();
            Console.WriteLine("Protocol can be one of: UDP, TCP, TLS");
            Console.WriteLine();
            Console.WriteLine("Example: SyslogClient UDP localhost \"Hello World!\"");
            Console.WriteLine("Example: SyslogClient TCP localhost:514 \"Hello World!\"");
            Console.WriteLine("Example: SyslogClient TLS localhost:6514 \"Hello World!\" -acceptAll");
        }
    }
}
