using Adaptation._Tests.Shared;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace Adaptation._Tests.Static;

[TestClass]
public class BACKLOG : LoggingUnitTesting, IDisposable
{

#pragma warning disable CA2254
#pragma warning disable IDE0060

    internal static BACKLOG LoggingUnitTesting { get; private set; }

    public BACKLOG() : base(testContext: null, declaringType: null)
    {
        if (LoggingUnitTesting is null)
            throw new Exception();
    }

    public BACKLOG(TestContext testContext) : base(testContext, new StackFrame().GetMethod().DeclaringType)
    {
    }

    [ClassInitialize]
    public static void ClassInitialize(TestContext testContext) => LoggingUnitTesting ??= new BACKLOG(testContext);

    [ClassCleanup()]
    public static void ClassCleanup()
    {
        LoggingUnitTesting?.Logger?.LogInformation("Cleanup");
        LoggingUnitTesting?.Dispose();
    }

    private static void NonThrowTryCatch()
    {
        try
        { throw new Exception(); }
        catch (Exception) { }
    }

    [TestMethod]
    public void TestDateTime()
    {
        DateTime dateTime = DateTime.Now;
        Assert.IsTrue(dateTime.ToString("M/d/yyyy h:mm:ss tt") == dateTime.ToString());
    }

#if DEBUG
    [Ignore]
#endif
    [TestMethod]
    public void Development()
    {
        MethodBase methodBase = new StackFrame().GetMethod();
        StringBuilder results = new();
        (string cellInstanceName, string cellInstanceVersionName)[] collection = new (string, string)[]
        {
            new("BACKLOG", "v2.49.2"),
            new("BACKLOG-EQPT", "v2.49.2"),
        };
        string development = "http://eaf-dev.mes.infineon.com:9003/CellInstanceServiceV2";
        Shared.PasteSpecialXml.EAF.XML.API.CellInstance.CellInstanceVersion cellInstanceVersion;
        LoggingUnitTesting.Logger.LogInformation(string.Concat(methodBase.Name, " - Getting configuration"));
        foreach ((string cellInstanceName, string cellInstanceVersionName) in collection)
        {
            cellInstanceVersion = AdaptationTesting.GetCellInstanceVersion($"{development}/{cellInstanceName}/{cellInstanceVersionName}/configuration");
            _ = results.AppendLine($"{cellInstanceName}\t{cellInstanceVersionName}\t{cellInstanceVersion.EdaConnection.PortNumber}");
        }
        File.WriteAllText($"D:/Tmp/{methodBase.Module.Name}-{methodBase.ReflectedType.Name}-{methodBase.Name}.tsv", results.ToString());
        LoggingUnitTesting.Logger.LogInformation(string.Concat(methodBase.Name, " - Exit"));
        NonThrowTryCatch();
    }

    [Ignore]
    [TestMethod]
    public void Staging()
    {
        MethodBase methodBase = new StackFrame().GetMethod();
        StringBuilder results = new();
        (string cellInstanceName, string cellInstanceVersionName)[] collection = new (string, string)[]
        {
            new("BACKLOG", "v2.49.2"),
            new("BACKLOG-EQPT", "v2.49.2"),
        };
        string staging = "http://mestsa07ec.ec.local:9003/CellInstanceServiceV2";
        Shared.PasteSpecialXml.EAF.XML.API.CellInstance.CellInstanceVersion cellInstanceVersion;
        LoggingUnitTesting.Logger.LogInformation(string.Concat(methodBase.Name, " - Getting configuration"));
        foreach ((string cellInstanceName, string cellInstanceVersionName) in collection)
        {
            cellInstanceVersion = AdaptationTesting.GetCellInstanceVersion($"{staging}/{cellInstanceName}/{cellInstanceVersionName}/configuration");
            _ = results.AppendLine($"{cellInstanceName}\t{cellInstanceVersionName}\t{cellInstanceVersion.EdaConnection.PortNumber}");
        }
        File.WriteAllText($"D:/Tmp/{methodBase.Module.Name}-{methodBase.ReflectedType.Name}-{methodBase.Name}.tsv", results.ToString());
        LoggingUnitTesting.Logger.LogInformation(string.Concat(methodBase.Name, " - Exit"));
        NonThrowTryCatch();
    }

}