using Adaptation._Tests.Shared;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;

namespace Adaptation._Tests.Static;

[TestClass]
public class EAF : LoggingUnitTesting, IDisposable
{

#pragma warning disable CA2254
#pragma warning disable IDE0060

    internal static EAF LoggingUnitTesting { get; private set; }

    internal static AdaptationTesting AdaptationTesting { get; private set; }

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

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

    [ClassInitialize]
    public static void ClassInitialize(TestContext testContext)
    {
        LoggingUnitTesting ??= new EAF(testContext);
        string dummyRoot = string.Empty;
        bool skipEquipmentDictionary = true;
        AdaptationTesting = new(dummyRoot, testContext, skipEquipmentDictionary, LoggingUnitTesting.TestContextPropertiesAsJson, LoggingUnitTesting.HasWaitForProperty);
    }

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

    [TestMethod]
    public void Production()
    {
        string testName;
        Task<string> task;
        string[] segments;
        HttpClient httpClient = new();
        MethodBase methodBase = new StackFrame().GetMethod();
        string currentActiveVersionTag = "CurrentActiveVersion>";
        string managementSite = "http://messa08ec.infineon.com:9003/CellInstances/entity/";
        LoggingUnitTesting.Logger.LogInformation(string.Concat(methodBase.Name, " - Getting configuration"));
        string[] cellInstances = new string[]
        {
            "BACKLOG-EQPT",
            "BACKLOG",
            "BIORAD2-EDA",
            "BIORAD2",
            "BIORAD3-EDA",
            "BIORAD3",
            "BIORAD4-EDA",
            "BIORAD4",
            "BIORAD5-EDA",
            "BIORAD5",
            "CDE2-EDA",
            "CDE2",
            "CDE3-EDA",
            "CDE3-EQPT",
            "CDE3",
            "CDE4-EDA",
            "CDE4-EQPT",
            "CDE4",
            "CDE5-EDA",
            "CDE5-EQPT",
            "CDE5",
            "CDE6-EDA",
            "CDE6-EQPT",
            "CDE6",
            "DEP08SIASM",
            "DEP08SIHTRPLC",
            "EC",
            "HGCV1-EDA",
            "HGCV1-EQPT",
            "HGCV1",
            "HGCV2-EDA",
            "HGCV2-EQPT",
            "HGCV2",
            "HGCV3-EDA",
            "HGCV3-EQPT",
            "HGCV3",
            "MET08ANLYSDIFAAST230",
            "MET08DDUPSFS6420",
            "MET08DDUPSP1TBI",
            "MET08RESIHGCV",
            "MET08RESIMAPCDE",
            "MET08RESISRP2100",
            "MET08THFTIRQS408M",
            "MET08THFTIRSTRATUS",
            "R34-EDA",
            "R34-EQPT",
            "R34",
            "R47-PLC-EDA",
            "R47-PLC",
            "R53-EDA",
            "R53-EQPT",
            "R53",
            "R55-EDA",
            "R55-EQPT",
            "R55",
            "R70-PLC-EDA",
            "R70-PLC",
            "R72-PLC-EDA",
            "R72-PLC",
            "R73-PLC-EDA",
            "R73-PLC",
            "R74-PLC-EDA",
            "R74-PLC",
            "SP101-EDA",
            "SP101-EQPT",
            "SP101",
            "SPV01-EDA",
            "SPV01",
            "SRP-EDA",
            "SRP-EQPT",
            "SRP",
            "T21",
            "T27-EDA",
            "T27",
            "TENCOR1-EDA",
            "TENCOR1",
            "TENCOR2-EDA",
            "TENCOR2",
            "TENCOR3-EDA",
            "TENCOR3",
        };
        foreach (string cellInstance in cellInstances.OrderBy(l => l))
        {
            if (!cellInstance.Contains('-'))
                continue;
            task = httpClient.GetStringAsync($"{managementSite}{cellInstance}");
            task.Wait();
            if (task.Result is not string response)
                continue;
            segments = response.Split(currentActiveVersionTag);
            if (segments.Length < 2)
                continue;
            testName = string.Concat("Production__v", segments[1].Split('<')[0].Replace('.', '_'), "__", cellInstance.Replace('-', '_'), "__");
            (string i, string v, string c, string n, int p, string f) = AdaptationTesting.GetCellInstanceVersionCore(testName);
            LoggingUnitTesting.Logger.LogInformation($"{p},{v},{i},{c},{n},{f}");
        }
        Assert.IsTrue(true);
        LoggingUnitTesting.Logger.LogInformation(string.Concat(methodBase.Name, " - Exit"));
    }

}

// Production - Getting configuration
// 8500,v4.16.0,BIORAD2-EDA,,,messa010ec
// 8501,v4.16.0,BIORAD3-EDA,,,messa010ec
// 8510,v4.16.0,BIORAD4-EDA,,,messa010ec
// 8511,v4.16.0,BIORAD5-EDA,,,messa010ec
// 8520,v4.16.0,CDE2-EDA,,,messa010ec
// 8524,v4.16.0,CDE3-EDA,,,messa010ec
// 8526,v2.49.2,CDE3-EQPT,DownloadRsMFile.Communicating,DownloadRsMFile.NotCommunicating,mestsa06ec
// 8524,v4.16.0,CDE4-EDA,,,messa010ec
// 8526,v2.49.2,CDE4-EQPT,DownloadRsMFile.Communicating,DownloadRsMFile.NotCommunicating,mestsa06ec
// 8521,v4.16.0,CDE5-EDA,,,messa010ec
// 7589,v2.49.2,CDE5-EQPT,DownloadRsMFile.Communicating,DownloadRsMFile.NotCommunicating,mestsa06ec
// 8522,v4.16.0,CDE6-EDA,,,messa010ec
// 7590,v2.49.2,CDE6-EQPT,DownloadRsMFile.Communicating,DownloadRsMFile.NotCommunicating,mestsa06ec
// 8530,v4.16.0,HGCV1-EDA,,,messa010ec
// 8536,v2.12.3,HGCV1-EQPT,,,messa010ec
// 8531,v4.16.0,HGCV2-EDA,,,messa010ec
// 8537,v2.12.3,HGCV2-EQPT,,,messa010ec
// 8532,v4.16.0,HGCV3-EDA,,,messa010ec
// 8538,v2.12.3,HGCV3-EQPT,,,messa010ec
// 8134,v4.16.0,R34-EDA,,,messa010ec
// 8034,v2.43.4,R34-EQPT,DownloadJpegFile.Communicating,DownloadJpegFile.NotCommunicating,messa010ec
// 553,v2.19.0,R47-PLC,,,messtec102
// 553,v2.19.0,R47-PLC-EDA,,,messtec102
// 8153,v4.16.0,R53-EDA,,,messa010ec
// 8653,v2.43.4,R53-EQPT,DownloadJpegFile.Communicating,DownloadJpegFile.NotCommunicating,messa010ec
// 8155,v4.16.0,R55-EDA,,,messa010ec
// 8655,v2.43.4,R55-EQPT,DownloadJpegFile.Communicating,DownloadJpegFile.NotCommunicating,messa010ec
// 8770,v2.19.0,R70-PLC,,,messtec102
// 8770,v1.0.1,R70-PLC-EDA,,,messtec102
// 8772,v2.19.0,R72-PLC,,,messtec102
// 8772,v1.0.1,R72-PLC-EDA,,,messtec102
// 8773,v2.19.0,R73-PLC,,,messtec102
// 8773,v1.0.1,R73-PLC-EDA,,,messtec102
// 8774,v2.19.0,R74-PLC,,,messtec102
// 8774,v1.0.1,R74-PLC-EDA,,,messtec102
// 8550,v4.16.0,SP101-EDA,,,messa010ec
// 8555,v2.49.3,SP101-EQPT,MoveAllFiles.Communicating,MoveAllFiles.NotCommunicating,
// 8569,v4.16.0,SPV01-EDA,,,messa010ec
// 8574,v4.16.0,SRP-EDA,,,messa010ec
// 8572,v2.49.2,SRP-EQPT,csv.Communicating,csv.NotCommunicating,mestsa06ec
// 7627,v4.16.0,T27-EDA,,,messa010ec
// 8540,v4.16.0,TENCOR1-EDA,,,messa010ec
// 8541,v4.16.0,TENCOR2-EDA,,,messa010ec
// 8542,v4.16.0,TENCOR3-EDA,,,messa010ec
// Production - Exit
// Cleanup