using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace EonaCat.Logger.Syslog
{
    // This file is part of the EonaCat project(s) which is released under the Apache License.
    // See the LICENSE file or go to https://EonaCat.com/License for full license details.

    /// <summary>
    /// Syslog server.
    /// </summary>
    public class SyslogServer
    {
        /// <summary>
        /// Hostname.
        /// </summary>
        public string Hostname
        {
            get
            {
                return _Hostname;
            }
            
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(Hostname));
                _Hostname = value;

                SetUdp();
            }
        }

        /// <summary>
        /// UDP port.
        /// </summary>
        public int Port
        {
            get
            {
                return _Port;
            }
            set
            {
                if (value < 0) throw new ArgumentException("Port must be zero or greater.");
                _Port = value;

                SetUdp();
            }
        }

        /// <summary>
        /// IP:port of the server.
        /// </summary>
        public string IpPort
        {
            get
            {
                return _Hostname + ":" + _Port;
            }
        }

        internal UdpClient Udp = null;
        private string _Hostname = "127.0.0.1";
        private int _Port = 514;

        /// <summary>
        /// Instantiate the object.
        /// </summary>
        public SyslogServer()
        {
        }

        /// <summary>
        /// Instantiate the object.
        /// </summary>
        /// <param name="hostname">Hostname.</param>
        /// <param name="port">Port.</param>
        public SyslogServer(string hostname = "127.0.0.1", int port = 514)
        {
            Hostname = hostname;
            Port = port;
        }

        private void SetUdp()
        {
            Udp = null;
            Udp = new UdpClient(_Hostname, _Port);
        }
    }
}
