using Adaptation.Shared;
using Adaptation.Shared.Methods;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;

namespace Adaptation.FileHandlers.OpenInsight;

public class CommonGatewayInterface
{

    internal static async void PutReactorStatesStatus(IFileRead fileRead, Logistics logistics, HttpClient httpClient, List<jpeg.Description> descriptions, string logisticsSequenceMemoryDirectory)
    {
        HttpContent httpContent = null;
        string status = descriptions[0].MID;
        string reactorNumber = logistics.MesEntity.Substring(1);
        string statusDateTime = logistics.DateTimeFromSequence.ToString("MM-dd-yyyy HH:mm:ss");
        string contentFile = Path.Combine(logisticsSequenceMemoryDirectory, $"{logistics.DateTimeFromSequence.Ticks}.txt");
        Uri uri = new($"{httpClient.BaseAddress.OriginalString}reactno={reactorNumber}&status={status}&statusdtm={statusDateTime}");
        try
        {
            HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(uri, httpContent);
            if (httpResponseMessage.StatusCode is not System.Net.HttpStatusCode.OK)
                File.WriteAllText(contentFile, "Failed!");
            else
            {
                string content = await httpResponseMessage.Content.ReadAsStringAsync();
                File.WriteAllText(contentFile, content);
            }
        }
        catch (Exception ex)
        {
            File.WriteAllText(contentFile, $"{ex.Message}{Environment.NewLine}{ex.StackTrace}{Environment.NewLine}{fileRead.CellInstanceConnectionName}");
        }
    }

}