namespace QuanTAlib.Tests;

public class HanmaTests
{
    [Fact]
    public void Hanma_Constructor_ValidatesInput()
    {
        var ex1 = Assert.Throws<ArgumentException>(() => new Hanma(0));
        Assert.Equal("period", ex1.ParamName);

        var ex2 = Assert.Throws<ArgumentException>(() => new Hanma(-1));
        Assert.Equal("period", ex2.ParamName);

        var hanma = new Hanma(10);
        Assert.NotNull(hanma);
    }

    [Fact]
    public void Hanma_Calc_ReturnsValue()
    {
        // Note: Hanning window has edge weight = 0, so first value with count=1
        // gets zero weight. We need at least 2 values for non-zero result.
        var hanma = new Hanma(10);
        hanma.Update(new TValue(DateTime.UtcNow, 100));
        TValue result = hanma.Update(new TValue(DateTime.UtcNow, 100));
        Assert.True(result.Value > 0);
    }

    [Fact]
    public void Hanma_IsHot_BecomesTrueWhenBufferFull()
    {
        var hanma = new Hanma(5);

        Assert.False(hanma.IsHot);

        for (int i = 0; i < 4; i++)
        {
            hanma.Update(new TValue(DateTime.UtcNow, 100));
            Assert.False(hanma.IsHot);
        }

        hanma.Update(new TValue(DateTime.UtcNow, 100));
        Assert.True(hanma.IsHot);
    }

    [Fact]
    public void Hanma_StreamingMatchesBatch()
    {
        var hanmaStreaming = new Hanma(10);
        var hanmaBatch = new Hanma(10);
        var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
        var series = new TSeries();

        for (int i = 0; i < 100; i++)
        {
            var bar = gbm.Next(isNew: true);
            series.Add(new TValue(bar.Time, bar.Close));
        }

        // Streaming
        var streamingResults = new TSeries();
        Assert.True(series.Count > 0);
        foreach (var item in series)
        {
            streamingResults.Add(hanmaStreaming.Update(item));
        }

        // Batch
        var batchResults = hanmaBatch.Update(series);

        Assert.Equal(streamingResults.Count, batchResults.Count);
        for (int i = 0; i < batchResults.Count; i++)
        {
            Assert.Equal(streamingResults[i].Value, batchResults[i].Value, 1e-9);
        }
    }

    [Fact]
    public void Hanma_StaticCalculate_MatchesInstance()
    {
        var series = new TSeries();
        var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
        for (int i = 0; i < 100; i++)
        {
            var bar = gbm.Next(isNew: true);
            series.Add(bar.Time, bar.Close);
        }

        var instanceResults = new Hanma(10).Update(series);
        var staticResults = Hanma.Batch(series, 10);

        for (int i = 0; i < instanceResults.Count; i++)
        {
            Assert.Equal(instanceResults[i].Value, staticResults[i].Value, 1e-9);
        }
    }

    [Fact]
    public void Hanma_SpanCalculate_MatchesSeries()
    {
        var series = new TSeries();
        var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
        for (int i = 0; i < 100; i++)
        {
            var bar = gbm.Next(isNew: true);
            series.Add(bar.Time, bar.Close);
        }

        var seriesResults = Hanma.Batch(series, 10);

        double[] input = series.Values.ToArray();
        double[] output = new double[input.Length];

        Hanma.Calculate(input.AsSpan(), output.AsSpan(), 10);

        for (int i = 0; i < input.Length; i++)
        {
            Assert.Equal(seriesResults[i].Value, output[i], 1e-9);
        }
    }

    [Fact]
    public void Hanma_Update_IsNewFalse_CorrectsValue()
    {
        // Note: Hanning window has edge weight = 0, so the newest value (position period-1)
        // contributes 0 to the weighted average when buffer is full. We need to test
        // correction on a value that has non-zero weight, so we add one more value
        // after the correction target to shift it away from the edge.
        var hanma = new Hanma(10);
        var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);

        // Feed initial data
        for (int i = 0; i < 20; i++)
        {
            var bar = gbm.Next(isNew: true);
            hanma.Update(new TValue(bar.Time, bar.Close), isNew: true);
        }

        // Add a value that we'll correct
        var targetBar = gbm.Next(isNew: true);
        hanma.Update(new TValue(targetBar.Time, targetBar.Close), isNew: true);

        // Add one more value so the target bar moves to position period-2 (which has non-zero weight)
        var nextBar = gbm.Next(isNew: true);
        hanma.Update(new TValue(nextBar.Time, nextBar.Close), isNew: true);

        double valueAfterCommit = hanma.Last.Value;

        // Now correct the previous bar (targetBar) with a different value using 2 corrections:
        // First rollback the last bar, then update targetBar with different value, then re-add nextBar
        // This simulates bar correction where we need to re-apply subsequent bars

        // Alternative approach: test the isNew=false mechanism directly on the LAST bar
        // even though it has weight=0, we verify the state rollback works correctly
        _ = hanma.Update(new TValue(nextBar.Time, nextBar.Close + 50.0), isNew: false);

        // Even though the newest bar has weight=0, the state rollback should still work
        // and the result may differ due to buffer state restoration
        // For Hanning, if only the edge changes, result stays same - this is mathematically correct
        // So we test state restoration instead:
        hanma.Update(new TValue(nextBar.Time, nextBar.Close), isNew: false);

        Assert.Equal(valueAfterCommit, hanma.Last.Value, 1e-9);
    }

    [Fact]
    public void Hanma_NaN_Input_UsesLastValidValue()
    {
        var hanma = new Hanma(5);

        hanma.Update(new TValue(DateTime.UtcNow, 100));
        hanma.Update(new TValue(DateTime.UtcNow, 110));

        var resultAfterNaN = hanma.Update(new TValue(DateTime.UtcNow, double.NaN));

        Assert.True(double.IsFinite(resultAfterNaN.Value));
        Assert.NotEqual(0, resultAfterNaN.Value);
    }

    [Fact]
    public void Hanma_Reset_ClearsState()
    {
        var hanma = new Hanma(10);
        hanma.Update(new TValue(DateTime.UtcNow, 100));
        hanma.Update(new TValue(DateTime.UtcNow, 110));

        Assert.True(hanma.Last.Value > 0);

        hanma.Reset();

        Assert.Equal(0, hanma.Last.Value);
        Assert.False(hanma.IsHot);
    }

    [Fact]
    public void Hanma_FirstValue_ReturnsExpected()
    {
        var hanma = new Hanma(10);
        TValue result = hanma.Update(new TValue(DateTime.UtcNow, 100));
        Assert.Equal(100.0, result.Value, 1e-9);
    }

    [Fact]
    public void Hanma_Properties_Accessible()
    {
        var hanma = new Hanma(10);
        Assert.False(hanma.IsHot);
        Assert.Equal(0, hanma.Last.Value);
    }

    [Fact]
    public void Hanma_Calc_IsNew_AcceptsParameter()
    {
        var hanma = new Hanma(10);
        hanma.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
        Assert.Equal(100, hanma.Last.Value);
    }

    [Fact]
    public void Hanma_IterativeCorrections_RestoreToOriginalState()
    {
        var hanma = new Hanma(10);
        var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);

        // Feed 10 new values
        TValue tenthInput = default;
        for (int i = 0; i < 10; i++)
        {
            var bar = gbm.Next(isNew: true);
            tenthInput = new TValue(bar.Time, bar.Close);
            hanma.Update(tenthInput, isNew: true);
        }

        // Remember state after 10 values
        double valueAfterTen = hanma.Last.Value;

        // Generate 9 corrections with isNew=false (different values)
        for (int i = 0; i < 9; i++)
        {
            var bar = gbm.Next(isNew: false);
            hanma.Update(new TValue(bar.Time, bar.Close), isNew: false);
        }

        // Feed the remembered 10th input again with isNew=false
        TValue finalValue = hanma.Update(tenthInput, isNew: false);

        // Should match the original state after 10 values
        Assert.Equal(valueAfterTen, finalValue.Value, 1e-9);
    }

    [Fact]
    public void Hanma_Infinity_Input_UsesLastValidValue()
    {
        var hanma = new Hanma(10);
        hanma.Update(new TValue(DateTime.UtcNow, 100));
        hanma.Update(new TValue(DateTime.UtcNow, 110));

        var resultPosInf = hanma.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
        Assert.True(double.IsFinite(resultPosInf.Value));

        var resultNegInf = hanma.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
        Assert.True(double.IsFinite(resultNegInf.Value));
    }

    [Fact]
    public void Hanma_MultipleNaN_ContinuesWithLastValid()
    {
        var hanma = new Hanma(10);
        hanma.Update(new TValue(DateTime.UtcNow, 100));

        var r1 = hanma.Update(new TValue(DateTime.UtcNow, double.NaN));
        var r2 = hanma.Update(new TValue(DateTime.UtcNow, double.NaN));

        Assert.True(double.IsFinite(r1.Value));
        Assert.True(double.IsFinite(r2.Value));
    }

    [Fact]
    public void Hanma_AllModes_ProduceSameResult()
    {
        // Arrange
        const int period = 10;
        var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
        var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
        var series = bars.Close;

        // 1. Batch Mode
        var batchSeries = Hanma.Batch(series, period);
        double expected = batchSeries.Last.Value;

        // 2. Span Mode
        var tValues = series.Values.ToArray();
        var spanInput = new ReadOnlySpan<double>(tValues);
        var spanOutput = new double[tValues.Length];
        Hanma.Calculate(spanInput, spanOutput, period);
        double spanResult = spanOutput[^1];

        // 3. Streaming Mode
        var streamingInd = new Hanma(period);
        for (int i = 0; i < series.Count; i++)
        {
            streamingInd.Update(series[i]);
        }
        double streamingResult = streamingInd.Last.Value;

        // 4. Eventing Mode
        var pubSource = new TSeries();
        var eventingInd = new Hanma(pubSource, period);
        for (int i = 0; i < series.Count; i++)
        {
            pubSource.Add(series[i]);
        }
        double eventingResult = eventingInd.Last.Value;

        // Assert
        Assert.Equal(expected, spanResult, 1e-9);
        Assert.Equal(expected, streamingResult, 1e-9);
        Assert.Equal(expected, eventingResult, 1e-9);
    }

    [Fact]
    public void Hanma_SpanCalc_ValidatesInput()
    {
        double[] source = [1, 2, 3, 4, 5];
        double[] output = new double[5];
        double[] wrongSizeOutput = new double[3];

        Assert.Throws<ArgumentException>(() => Hanma.Calculate(source.AsSpan(), output.AsSpan(), 0));
        Assert.Throws<ArgumentException>(() => Hanma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
    }

    [Fact]
    public void Hanma_SpanCalc_HandlesNaN()
    {
        double[] source = [100, 110, double.NaN, 120, 130];
        double[] output = new double[5];

        Hanma.Calculate(source.AsSpan(), output.AsSpan(), 3);

        foreach (var val in output)
        {
            Assert.True(double.IsFinite(val));
        }
    }

    [Fact]
    public void Hanma_HanningWindow_WeightSymmetry()
    {
        // Hanning window should be symmetric around center
        // w[i] = w[period-1-i] for all i
        int period = 11; // Odd for exact center

        // Verify weight symmetry by checking equal outputs for symmetric inputs
        var hanma1 = new Hanma(period);
        var hanma2 = new Hanma(period);

        // Feed ascending values to hanma1
        double[] ascending = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
        foreach (var v in ascending)
        {
            hanma1.Update(new TValue(DateTime.UtcNow, v));
        }

        // Feed descending values to hanma2
        double[] descending = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
        foreach (var v in descending)
        {
            hanma2.Update(new TValue(DateTime.UtcNow, v));
        }

        // Results should be the same (symmetric weights applied to symmetric data)
        Assert.Equal(hanma1.Last.Value, hanma2.Last.Value, 1e-9);
    }

    [Fact]
    public void Hanma_KnownValues_ManualCalculation()
    {
        // Manual verification with known Hanning weights
        // period=5: w[i] = 0.5 * (1 - cos(2π*i/4))
        // w[0] = 0.5 * (1 - cos(0)) = 0.5 * 0 = 0
        // w[1] = 0.5 * (1 - cos(π/2)) = 0.5 * 1 = 0.5
        // w[2] = 0.5 * (1 - cos(π)) = 0.5 * 2 = 1.0
        // w[3] = 0.5 * (1 - cos(3π/2)) = 0.5 * 1 = 0.5
        // w[4] = 0.5 * (1 - cos(2π)) = 0.5 * 0 = 0

        int period = 5;
        var hanma = new Hanma(period);

        double[] prices = [100, 102, 104, 103, 101];
        foreach (var price in prices)
        {
            hanma.Update(new TValue(DateTime.UtcNow, price));
        }

        // Calculate expected manually
        double twoPiOverPm1 = 2.0 * Math.PI / (period - 1);
        double[] weights = new double[period];
        double weightSum = 0;
        for (int i = 0; i < period; i++)
        {
            weights[i] = 0.5 * (1.0 - Math.Cos(twoPiOverPm1 * i));
            weightSum += weights[i];
        }

        double expected = 0;
        for (int i = 0; i < period; i++)
        {
            expected += prices[i] * weights[i];
        }
        expected /= weightSum;

        Assert.Equal(expected, hanma.Last.Value, 1e-9);
    }

    [Fact]
    public void Hanma_PeriodOne_ReturnsInputValue()
    {
        var hanma = new Hanma(1);

        for (int i = 1; i <= 10; i++)
        {
            var input = new TValue(DateTime.UtcNow, i * 10.0);
            var result = hanma.Update(input);
            Assert.Equal(i * 10.0, result.Value, 1e-9);
        }
    }

    [Fact]
    public void Hanma_EdgeWeights_AreZero()
    {
        // Hanning window uniquely has edge weights = 0
        // This means first and last values in window get zero weight
        int period = 5;
        var hanma = new Hanma(period);

        // All same values except edges
        double[] prices = [999, 100, 100, 100, 888];
        foreach (var price in prices)
        {
            hanma.Update(new TValue(DateTime.UtcNow, price));
        }

        // Because edge weights are 0, the 999 and 888 should have no effect
        // Result should be 100.0 (weighted average of middle values only)
        Assert.Equal(100.0, hanma.Last.Value, 1e-9);
    }
}
