using Microsoft.AspNetCore.Mvc.Testing;
using ReportingServices.Shared.HelperClasses;
using Serilog;

namespace ReportingServices.Test;

[TestClass]
public class APIHelperTester
{

#pragma warning disable CS8618

    private static ILogger _Logger;
    private static string _ControllerName;
    private static TestContext _TestContext;
    private static WebApplicationFactory<API.Program> _WebApplicationFactory;

#pragma warning restore

    [ClassInitialize]
    public static void ClassInitAsync(TestContext testContext)
    {
        _TestContext = testContext;
        _Logger = Log.ForContext<APIHelperTester>();
        _WebApplicationFactory = new WebApplicationFactory<API.Program>();
        _ControllerName = nameof(API.Controllers.ScrapeDBController)[..^10];
    }
    /*
    [TestMethod]
    public async Task ReactorOuts()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        DateTime endDate = DateTime.Now;
        DateTime startDate = endDate.AddHours(-24);
        YieldInformation? result = await httpClient.GetFromJsonAsync<YieldInformation>($"api/{_ControllerName}/ReactorOuts/?startDate={startDate}&endDate={endDate}");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task PSNWO()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        DateTime endDate = DateTime.Now;
        DateTime startDate = endDate.AddHours(-3);
        List<ReactorPSNWORuns>? result = await httpClient.GetFromJsonAsync<List<ReactorPSNWORuns>>($"api/{_ControllerName}/PSNWO/?startDate={startDate}&endDate={endDate}");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task PartChanges()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        DateTime endDate = DateTime.Now;
        DateTime startDate = endDate.AddHours(-3);
        int? result = await httpClient.GetFromJsonAsync<int>($"api/{_ControllerName}/PartChanges/?startDate={startDate}&endDate={endDate}");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task Targets()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        QuarterlyTargets? result = await httpClient.GetFromJsonAsync<QuarterlyTargets>($"api/{_ControllerName}/Targets");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task Reactors()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        List<Reactor>? result = await httpClient.GetFromJsonAsync<List<Reactor>>($"api/{_ControllerName}/Reactors");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task RDS()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        DateTime date = DateTime.Now;
        List<RDS>? result = await httpClient.GetFromJsonAsync<List<RDS>>($"api/{_ControllerName}/RDS/?date={date}");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task ReactorEvents()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        string reactorNumber = "37";
        DateTime endDate = DateTime.Now;
        DateTime startDate = endDate.AddHours(-3);
        List<ReactorEvent>? result = await httpClient.GetFromJsonAsync<List<ReactorEvent>>($"api/{_ControllerName}/ReactorEvents/?startDate={startDate}&endDate={endDate}&reactorNumber={reactorNumber}");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }

    [TestMethod]
    public async Task ToolEvents()
    {
        HttpClient httpClient = _WebApplicationFactory.CreateClient();
        _Logger.Information("Starting Web Application");
        string toolID = "37";
        ToolEvent? result = await httpClient.GetFromJsonAsync<ToolEvent>($"api/{_ControllerName}/ToolEvents/?toolID={toolID}");
        Assert.IsNotNull(result);
        _Logger.Information($"{_TestContext?.TestName} completed");
    }
    */
    [TestMethod]
    public void CheckShortDateWithPassedInDate()
    {
        // Arrange
        string date = DateTime.Now.ToString("yyyy-M-d") + " 0:0:0";

        // Act
        string callerDate = APIHelperFunctions.GetDateTimeAsAPIString(DateTime.Now.ToString(), false);

        // Assert
        Assert.AreEqual(date, callerDate);
    }

    [TestMethod]
    public void CheckFullDateWithPassedInDate()
    {
        // Arrange
        string date = DateTime.Now.ToString("yyyy-M-d HH:mm:ss");

        // Act
        string callerDate = APIHelperFunctions.GetDateTimeAsAPIString(DateTime.Now.ToString(), true);

        // Assert
        Assert.AreEqual(date, callerDate);
    }

    [TestMethod]
    public void CheckStartDateForWeekNow()
    {
        // Arrange
        string date = DetermineDate();

        // Act
        string callerDate = APIHelperFunctions.GetBeginningOfWeekAsAPIString();

        // Assert
        Assert.AreEqual(date, callerDate);
    }

    [TestMethod]
    public void CheckStartDateForDayPassedInAndHoursAdded()
    {
        // Arrange
        string date = "2022-11-23 00:00:00";

        // Act
        string callerDate = APIHelperFunctions.GetDateWithOffsetAsAPIString("11/23/2022 12:30 PM", -12.5f);

        // Assert
        Assert.AreEqual(date, callerDate);
    }

    private string DetermineDate()
    {
        DateTime date = DateTime.Now;

        if (date.DayOfWeek == DayOfWeek.Monday)
            date = date.AddDays(0);

        if (date.DayOfWeek == DayOfWeek.Tuesday)
            date = date.AddDays(-1);

        if (date.DayOfWeek == DayOfWeek.Wednesday)
            date = date.AddDays(-2);

        if (date.DayOfWeek == DayOfWeek.Thursday)
            date = date.AddDays(-3);

        if (date.DayOfWeek == DayOfWeek.Friday)
            date = date.AddDays(-4);

        if (date.DayOfWeek == DayOfWeek.Saturday)
            date = date.AddDays(-5);

        if (date.DayOfWeek == DayOfWeek.Sunday)
            date = date.AddDays(-6);

        return date.Year + "-" + date.Month + "-" + date.Day + " 0:0:0";
        ;
    }
}