using System;
using System.Net.Http;

namespace EonaCat.Logger.SplunkServer
{
    // 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>
    /// Splunk Server.
    /// </summary>
    public class SplunkServer
    {
        /// <summary>
        /// SplunkHecUrl
        /// </summary>
        public string SplunkHecUrl
        {
            get
            {
                return _splunkHecUrl;
            }

            set
            {
                if (!string.IsNullOrWhiteSpace(_splunkHecUrl) && !_splunkHecUrl.ToLower().Contains("http"))
                {
                    value = $"http://{value}";
                }
               _splunkHecUrl = value;
            }
        }

        public bool IsHttpsHecUrl => HasHecUrl && _splunkHecUrl.ToLower().StartsWith("https");

        /// <summary>
        /// SplunkHecToken
        /// </summary>
        public string SplunkHecToken
        {
            get
            {
                return _splunkHecToken;
            }
            set
            {
                _splunkHecToken = value;
            }
        }

        public HttpClientHandler SplunkClientHandler { get; internal set; }

        internal readonly object SendLock = new object();
        private string _splunkHecUrl = "https://127.0.0.1:8088/services/collector";
        private string _splunkHecToken = "splunk-hec-token";

        public bool HasHecToken => !string.IsNullOrWhiteSpace(SplunkHecToken) && SplunkHecToken != "splunk-hec-token";

        public bool HasHecUrl => !string.IsNullOrWhiteSpace(SplunkHecUrl);

        public bool IsLocalHost => HasHecUrl && (SplunkHecUrl.ToLower().Contains("127.0.0.1") || SplunkHecUrl.ToLower().Contains("localhost"));


        /// <summary>
        /// Instantiate the object.
        /// </summary>
        /// <param name="splunkHecUrl">splunkHecUrl.</param>
        /// <param name="splunkHecToken">splunkHecToken.</param>
        /// <param name="httpClientHandler">httpClientHandler. (optional)</param>
        public SplunkServer(string splunkHecUrl, string splunkHecToken, HttpClientHandler httpClientHandler = null)
        {
            SplunkHecUrl = splunkHecUrl;
            SplunkHecToken = splunkHecToken;

            if (httpClientHandler == null)
            {
                httpClientHandler = new HttpClientHandler();
            }

            SplunkClientHandler = httpClientHandler;
        }

        /// <summary>
        /// Disables SSL validation (can be used for insecure https urls)
        /// <note>This overwrites your own httpClientHandler</note>
        /// </summary>
        public void DisableSSLValidation()
        {
            HttpClientHandler clientHandler = new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
            };
            SplunkClientHandler = clientHandler;
        }
    }
}
