@page "/"
@inject IBinanceRestClient binanceClient
@inject IBingXRestClient bingXClient
@inject IBitfinexRestClient bitfinexClient
@inject IBitgetRestClient bitgetClient
@inject IBitMartRestClient bitmartClient
@inject IBitMEXRestClient bitmexClient
@inject IBybitRestClient bybitClient
@inject ICoinbaseRestClient coinbaseClient
@inject ICoinExRestClient coinexClient
@inject ICoinWRestClient coinWClient
@inject ICryptoComRestClient cryptocomClient
@inject IDeepCoinRestClient deepCoinClient
@inject IGateIoRestClient gateioClient
@inject IHTXRestClient htxClient
@inject IHyperLiquidRestClient hyperLiquidClient
@inject IKrakenRestClient krakenClient
@inject IKucoinRestClient kucoinClient
@inject IMexcRestClient mexcClient
@inject IOKXRestClient okxClient
@inject IToobitRestClient toobitClient
@inject IWhiteBitRestClient whitebitClient
@inject IXTRestClient xtClient

<h3>BTC-USD prices:</h3>
@foreach(var price in _prices.OrderBy(p => p.Key))
{
    <div>@price.Key: @price.Value</div>
}

@code{
    private Dictionary<string, decimal> _prices = new Dictionary<string, decimal>();

    protected override async Task OnInitializedAsync()
    {
        var binanceTask = binanceClient.SpotApi.ExchangeData.GetTickerAsync("BTCUSDT");
        var bingXTask = bingXClient.SpotApi.ExchangeData.GetTickersAsync("BTC-USDT");
        var bitfinexTask = bitfinexClient.SpotApi.ExchangeData.GetTickerAsync("tBTCUSD");
        var bitgetTask = bitgetClient.SpotApiV2.ExchangeData.GetTickersAsync("BTCUSDT");
        var bitmartTask = bitmartClient.SpotApi.ExchangeData.GetTickerAsync("BTC_USDT");
        var bitmexTask = bitmexClient.ExchangeApi.ExchangeData.GetSymbolsAsync("XBT_USDT");
        var bybitTask = bybitClient.V5Api.ExchangeData.GetSpotTickersAsync("BTCUSDT");
        var coinbaseTask = coinbaseClient.AdvancedTradeApi.ExchangeData.GetSymbolAsync("BTC-USDT");
        var coinexTask = coinexClient.SpotApiV2.ExchangeData.GetTickersAsync(["BTCUSDT"]);
        var coinWTask = coinWClient.SpotApi.ExchangeData.GetTickersAsync();
        var cryptocomTask = cryptocomClient.ExchangeApi.ExchangeData.GetTickersAsync("BTC_USDT");
        var deepCoinTask = deepCoinClient.ExchangeApi.ExchangeData.GetTickersAsync(DeepCoin.Net.Enums.SymbolType.Spot);
        var gateioTask = gateioClient.SpotApi.ExchangeData.GetTickersAsync("BTC_USDT");
        var htxTask = htxClient.SpotApi.ExchangeData.GetTickerAsync("btcusdt");
        var hyperLiquidTask = hyperLiquidClient.FuturesApi.ExchangeData.GetExchangeInfoAndTickersAsync(); // HyperLiquid does not have BTC spot trading
        var krakenTask = krakenClient.SpotApi.ExchangeData.GetTickerAsync("XBTUSD");
        var kucoinTask = kucoinClient.SpotApi.ExchangeData.GetTickerAsync("BTC-USDT");
        var mexcTask = mexcClient.SpotApi.ExchangeData.GetTickerAsync("BTCUSDT");
        var okxTask = okxClient.UnifiedApi.ExchangeData.GetTickerAsync("BTC-USDT");
        var toobitTask = toobitClient.SpotApi.ExchangeData.GetTickersAsync("BTCUSDT");
        var whitebitTask = whitebitClient.V4Api.ExchangeData.GetTickersAsync();
        var xtTask = xtClient.SpotApi.ExchangeData.GetTickersAsync("btc_usdt");

        await Task.WhenAll(binanceTask, bingXTask, bitfinexTask, bitgetTask, bitmartTask, bybitTask, coinexTask, deepCoinTask, gateioTask, htxTask, krakenTask, kucoinTask, mexcTask, okxTask);

        if (binanceTask.Result.Success)
            _prices.Add("Binance", binanceTask.Result.Data.LastPrice);

        if (bingXTask.Result.Success)
            _prices.Add("BingX", bingXTask.Result.Data.First().LastPrice);

        if (bitfinexTask.Result.Success)
            _prices.Add("Bitfinex", bitfinexTask.Result.Data.LastPrice);

        if (bitgetTask.Result.Success)
            _prices.Add("Bitget", bitgetTask.Result.Data.Single().LastPrice);

        if (bitmartTask.Result.Success)
            _prices.Add("BitMart", bitmartTask.Result.Data.LastPrice);

        if (bitmexTask.Result.Success)
            _prices.Add("BitMEX", bitmexTask.Result.Data.First().LastPrice);

        if (bybitTask.Result.Success)
            _prices.Add("Bybit", bybitTask.Result.Data.List.First().LastPrice);

        if (coinbaseTask.Result.Success)
            _prices.Add("Coinbase", coinbaseTask.Result.Data.LastPrice ?? 0);

        if (coinexTask.Result.Success)
            _prices.Add("CoinEx", coinexTask.Result.Data.Single().LastPrice);

        if (coinWTask.Result.Success)
            _prices.Add("CoinW", coinWTask.Result.Data.Single(x => x.Symbol == "BTC_USDT").LastPrice);

        if (cryptocomTask.Result.Success)
            _prices.Add("CryptoCom", cryptocomTask.Result.Data.First().LastPrice ?? 0);

        if (deepCoinTask.Result.Success)
        {
            // DeepCoin API doesn't offer an endpoint to filter for a specific ticker, so we have to filter client side
            var tickers = deepCoinTask.Result.Data;
            _prices.Add("DeepCoin", tickers.Single(x => x.Symbol == "BTC-USDT").LastPrice ?? 0);
        }

        if (gateioTask.Result.Success)
            _prices.Add("GateIo", gateioTask.Result.Data.First().LastPrice);

        if (htxTask.Result.Success)
            _prices.Add("HTX", htxTask.Result.Data.ClosePrice ?? 0);

        if (hyperLiquidTask.Result.Success)
        {
            // HyperLiquid API doesn't offer an endpoint to filter for a specific ticker, so we have to filter client side
            var tickers = hyperLiquidTask.Result.Data.Tickers;
            _prices.Add("HyperLiquid", tickers.Single(x => x.Symbol == "BTC").MidPrice ?? 9);
        }

        if (krakenTask.Result.Success)
            _prices.Add("Kraken", krakenTask.Result.Data.First().Value.LastTrade.Price);

        if (kucoinTask.Result.Success)
            _prices.Add("Kucoin", kucoinTask.Result.Data.LastPrice ?? 0);

        if (mexcTask.Result.Success)
            _prices.Add("Mexc", mexcTask.Result.Data.LastPrice);

        if (okxTask.Result.Success)
            _prices.Add("OKX", okxTask.Result.Data.LastPrice ?? 0);

        if (toobitTask.Result.Success)
            _prices.Add("Toobit", toobitTask.Result.Data.Single().LastPrice ?? 0);

        if (whitebitTask.Result.Success){
            // WhiteBit API doesn't offer an endpoint to filter for a specific ticker, so we have to filter client side
            var tickers = whitebitTask.Result.Data;
            _prices.Add("WhiteBit", tickers.Single(x => x.Symbol == "BTC_USDT").LastPrice);
        }

        if (xtTask.Result.Success)
            _prices.Add("XT", xtTask.Result.Data.Single().LastPrice ?? 0);
    }

}