using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using Managing.Domain.Evm;
using static Managing.Common.Enums;

namespace Managing.Infrastructure.Evm.Services;

public static class SubgraphService
{
    private const string SUBGRAPH_UNISWAP_V2 = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2";
    private const string SUBGRAPH_CHAINLINK = "https://api.thegraph.com/subgraphs/name/openpredict/chainlink-prices-subgraph";
    private const string SUBGRAPH_CHAINLINK_GMX = "https://api.thegraph.com/subgraphs/name/deividask/chainlink";
    private const string SUBGRAPH_GBC = "https://api.thegraph.com/subgraphs/name/nissoh/gmx-arbitrum";
    
    public static GraphQLHttpClient GetSubgraphClient(SubgraphProvider subgraphProvider)
    {
        var url = GetSubgraph(subgraphProvider).Url;
        var graphQLOptions = new GraphQLHttpClientOptions
        {
            EndPoint = new Uri(url)
        };
        return new GraphQLHttpClient(graphQLOptions, new SystemTextJsonSerializer());
    }

    private static Subgraph GetSubgraph(SubgraphProvider subgraphProvider)
    {
        return new Subgraph()
        {
            SubgraphProvider = subgraphProvider,
            Url = GetSubgraphUrl(subgraphProvider)
        };
    }

    private static string GetSubgraphUrl(SubgraphProvider subgraphProvider)
    {
        return subgraphProvider switch
        {
            SubgraphProvider.UniswapV2 => SUBGRAPH_UNISWAP_V2,
            SubgraphProvider.ChainlinkPrice => SUBGRAPH_CHAINLINK,
            SubgraphProvider.ChainlinkGmx => SUBGRAPH_CHAINLINK_GMX,
            SubgraphProvider.Gbc => SUBGRAPH_GBC,
            _ => throw new Exception("No url for subgraphprovider")
        };
    }
}
