using Microsoft.Extensions.Logging;
using System;
using System.Net;

namespace NetworkResurrector.Server.Application.Services
{
    public class ValidationService : IValidationService
    {
        private readonly ILogger<ValidationService> _logger;

        public ValidationService(ILogger<ValidationService> logger)
        {
            _logger = logger;
        }

        public bool IsValidIPAddress(string ipAddress)
        {
            if (IPAddress.TryParse(ipAddress, out IPAddress address))
            {
                switch (address.AddressFamily)
                {
                    case System.Net.Sockets.AddressFamily.InterNetwork:
                        _logger.LogDebug($"IP address '{ipAddress}' is v4.");
                        break;
                    case System.Net.Sockets.AddressFamily.InterNetworkV6:
                        _logger.LogDebug($"IP address '{ipAddress}' is v6.");
                        break;
                    default:
                        _logger.LogWarning($"IP address '{ipAddress}' is valid but is not v4 or v6! Family: [{address.AddressFamily}]");
                        break;
                }

                return true;
            }

            return false;
        }
    }
}
