using System.Numerics;
using Managing.Common;
using Managing.Infrastructure.Evm.Services.Gmx;
using Xunit;

namespace Managing.Infrastructure.Tests;

public class GmxHelpersTests
{
    [Fact]
    public void Should_return_correct_usd_formatting_for_sizeDelta()
    {
        // Arrange
        var sizeDelta = BigInteger.Parse("19970983955394369245200000000000");

        // Act
        var result = GmxHelpers.FormatAmount(sizeDelta, 30, 2);

        // Assert
        Assert.Equal(19.97m, result);
    }

    [Theory]
    [InlineData("2200000000000000", Enums.Ticker.ETH, 2200)]
    [InlineData("500000000000000000000000000", Enums.Ticker.BTC, 50000)]
    [InlineData("759434500000000000000000000", Enums.Ticker.BTC, 75943.45)]
    [InlineData("100000000000000000000000", Enums.Ticker.SOL, 100)]
    public void Should_return_correct_usd_formatting_for_triggerPrice(string triggerPriceString, Enums.Ticker ticker,
        decimal expected)
    {
        // Arrange
        var triggerPrice = BigInteger.Parse(triggerPriceString);
        var indexToken = TokenV2Service.TOKENS.First(t => t.Symbol == ticker.ToString());

        // Act
        var result = GmxV2Helpers.ParseContractPrice(triggerPrice, indexToken.Decimals, true);

        // Assert
        Assert.Equal(expected, result);
    }

    [Theory]
    [InlineData(50200, "502000000000000000000000000")]
    public void Should_correctly_parse_value_to_contract_price(decimal triggerPrice, string expected)
    {
        var indexToken = TokenV2Service.TOKENS.First(t => t.Symbol == Enums.Ticker.BTC.ToString());
        var triggerPriceValue = GmxV2Helpers.ConvertToContractPrice(triggerPrice, indexToken.Decimals, true);
        var expectedPrice = BigInteger.Parse(expected);

        Assert.IsType<BigInteger>(triggerPriceValue);
        Assert.Equal(expectedPrice, triggerPriceValue);
    }

    [Theory]
    [InlineData("10000000", 10)]
    public void Should_return_correct_usd_formatting_for_collateralDeltaAmount(string priceString, decimal expected)
    {
        // Arrange
        var price = BigInteger.Parse(priceString);
        var shortToken = TokenV2Service.TOKENS.First(t => t.Symbol == "USDC");

        // Act
        var result = GmxV2Helpers.ParseContractPrice(price, shortToken.Decimals, false);

        // Assert
        Assert.Equal(expected, result);
    }

    [Fact]
    public void Helpers_Should_Validate_Exact_Same_Address()
    {
        var address = "0x47904963Fc8b2340414262125aF798B9655E58Cd";
        var address2 = "0x47904963fc8b2340414262125aF798B9655E58Cd";

        Assert.True(GmxV2Helpers.SameAddress(address, address2));
    }
}