using Xunit;
using TradingPlatform.BusinessLayer;

namespace QuanTAlib.Tests;

public class SqrttransIndicatorTests
{
    [Fact]
    public void SqrttransIndicator_Constructor_SetsDefaults()
    {
        var indicator = new SqrttransIndicator();

        Assert.Equal(SourceType.Close, indicator.Source);
        Assert.True(indicator.ShowColdValues);
        Assert.Equal("SQRTTRANS - Square Root Transform", indicator.Name);
        Assert.True(indicator.SeparateWindow);
        Assert.True(indicator.OnBackGround);
    }

    [Fact]
    public void SqrttransIndicator_MinHistoryDepths_IsOne()
    {
        var indicator = new SqrttransIndicator();
        Assert.Equal(1, indicator.MinHistoryDepths);
    }

    [Fact]
    public void SqrttransIndicator_ShortName_IsCorrect()
    {
        var indicator = new SqrttransIndicator();
        Assert.Equal("Sqrttrans", indicator.ShortName);
    }

    [Fact]
    public void SqrttransIndicator_Initialize_CreatesLineSeries()
    {
        var indicator = new SqrttransIndicator();
        indicator.Initialize();

        Assert.Single(indicator.LinesSeries);
        Assert.Equal("Sqrttrans", indicator.LinesSeries[0].Name);
    }

    [Fact]
    public void SqrttransIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
    {
        var indicator = new SqrttransIndicator();
        indicator.Initialize();

        var now = DateTime.UtcNow;
        indicator.HistoricalData.AddBar(now, 100, 110, 90, 100);

        var args = new UpdateArgs(UpdateReason.HistoricalBar);
        indicator.ProcessUpdate(args);

        // Sqrt of 100 is 10.0
        Assert.Equal(10.0, indicator.LinesSeries[0].GetValue(0), 1e-10);
    }

    [Fact]
    public void SqrttransIndicator_ProcessUpdate_NewBar_ComputesValue()
    {
        var indicator = new SqrttransIndicator();
        indicator.Initialize();

        var now = DateTime.UtcNow;
        indicator.HistoricalData.AddBar(now, 100, 110, 90, 100);
        indicator.HistoricalData.AddBar(now.AddMinutes(1), 25, 30, 20, 25);

        indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
        indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));

        Assert.Equal(2, indicator.LinesSeries[0].Count);
        // Sqrt of 25 is 5.0
        Assert.Equal(5.0, indicator.LinesSeries[0].GetValue(0), 1e-10);
    }

    [Fact]
    public void SqrttransIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
    {
        var indicator = new SqrttransIndicator();
        indicator.Initialize();

        var now = DateTime.UtcNow;
        indicator.HistoricalData.AddBar(now, 100, 110, 90, 100);

        indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
        indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));

        Assert.Equal(2, indicator.LinesSeries[0].Count);
    }

    [Fact]
    public void SqrttransIndicator_DifferentSourceTypes_Work()
    {
        var sources = new[]
        {
            SourceType.Open,
            SourceType.High,
            SourceType.Low,
            SourceType.Close,
            SourceType.HL2,
            SourceType.HLC3,
        };

        foreach (var source in sources)
        {
            var indicator = new SqrttransIndicator { Source = source };
            indicator.Initialize();

            var now = DateTime.UtcNow;
            indicator.HistoricalData.AddBar(now, 100, 144, 64, 81);
            indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));

            Assert.Equal(1, indicator.LinesSeries[0].Count);
            Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
        }
    }

    [Fact]
    public void SqrttransIndicator_PerfectSquareValues_ComputesExactly()
    {
        var indicator = new SqrttransIndicator();
        indicator.Initialize();

        var now = DateTime.UtcNow;

        // Test perfect squares: 1, 4, 9, 16, 25
        double[] squares = { 1, 4, 9, 16, 25 };
        double[] expectedRoots = { 1, 2, 3, 4, 5 };

        for (int i = 0; i < squares.Length; i++)
        {
            indicator.HistoricalData.AddBar(now.AddMinutes(i), squares[i], squares[i] + 1, squares[i] - 1, squares[i]);
            indicator.ProcessUpdate(new UpdateArgs(i == 0 ? UpdateReason.HistoricalBar : UpdateReason.NewBar));

            Assert.Equal(expectedRoots[i], indicator.LinesSeries[0].GetValue(0), 1e-10);
        }
    }
}
