using System.Net;
using System.Web;
using adas_core.Domain.Models.MongoModels;
using Serilog;

namespace adas_core.Domain.Utils;

public class RelayHelper
{
    public static bool GetRelayStatusFromApiRest(Relay relay)
    {
        UriBuilder builder = new()
        {
            Scheme = "https",
            Host = "localhost",
            Port = 7186,
            Path = $"{relay.RelayNumber}/status"
        };

        var query = HttpUtility.ParseQueryString(builder.Query);
        query["driver"] = relay.Driver;
        query["host"] = relay.Ip;
        query["port"] = relay.Port.ToString();
        query["relays"] = "8"; //todo:tendría que recoger el total

        builder.Query = query.ToString();
        HttpRequestMessage request = new()
        {
            RequestUri = builder.Uri,
            Method = HttpMethod.Get
        };


        try
        {
            using var client = new HttpClient();
            var httpResponse = client.SendAsync(request).Result;

            if (httpResponse.StatusCode.Equals(HttpStatusCode.OK))
            {
                var responseContent = httpResponse.Content;
                var response = responseContent.ReadAsStringAsync().Result;


                return Convert.ToBoolean(response);
            }

            return false;
        }
        catch (Exception e)
        {
            Log.Debug("Exception Getting Relay Status From Api Rest: {relay}", e);
            return false;
        }
    }


    //TODO son provisionales mientras se añade como nuget Smacs.Divers
    public static void PowerOnRelay(Relay relay)
    {
        UriBuilder builder = new()
        {
            Scheme = "https",
            Host = "localhost",
            Port = 7186,
            Path = $"{relay.RelayNumber}/powerOn"
        };

        PowerRelay(relay, builder);
    }

    public static void PowerOffRelay(Relay relay)
    {
        UriBuilder builder = new()
        {
            Scheme = "https",
            Host = "localhost",
            Port = 7186,
            Path = $"{relay.RelayNumber}/powerOff"
        };

        PowerRelay(relay, builder);
    }

    private static void PowerRelay(Relay relay, UriBuilder builder)
    {
        var query = HttpUtility.ParseQueryString(builder.Query);
        query["driver"] = relay.Driver;
        query["host"] = relay.Ip;
        query["port"] = relay.Port.ToString();
        query["relays"] = "8"; //todo:tendría que recoger el total

        builder.Query = query.ToString();
        HttpRequestMessage request = new()
        {
            RequestUri = builder.Uri,
            Method = HttpMethod.Post
        };

        try
        {
            using var client = new HttpClient();
            var httpResponse = client.SendAsync(request).Result;

            if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK)) Log.Information("relay: {relay} powered", relay);
        }
        catch (Exception e)
        {
            Log.Debug("Exception {e} powering Relay: {relay}", e.Message, relay);
        }
    }


    public static bool GetRelayStatus(Relay relay)
    {
        return false;
    }
}