using Binance.Net.Clients;
using Binance.Net.Interfaces.Clients;
using Kraken.Net.Clients;
using Kraken.Net.Interfaces.Clients;
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Application.Accounts;
using Managing.Application.Backtesting;
using Managing.Application.Bots.Base;
using Managing.Application.ManageBot;
using Managing.Application.MoneyManagements;
using Managing.Application.Scenarios;
using Managing.Application.Shared;
using Managing.Application.Trading;
using Managing.Application.Trading.Commands;
using Managing.Application.Users;
using Managing.Application.Workers;
using Managing.Application.Workers.Abstractions;
using Managing.Domain.Trades;
using Managing.Infrastructure.Databases;
using Managing.Infrastructure.Databases.InfluxDb;
using Managing.Infrastructure.Databases.InfluxDb.Abstractions;
using Managing.Infrastructure.Databases.InfluxDb.Models;
using Managing.Infrastructure.Databases.MongoDb;
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
using Managing.Infrastructure.Databases.MongoDb.Configurations;
using Managing.Infrastructure.Evm;
using Managing.Infrastructure.Evm.Abstractions;
using Managing.Infrastructure.Evm.Models.Privy;
using Managing.Infrastructure.Evm.Services;
using Managing.Infrastructure.Evm.Subgraphs;
using Managing.Infrastructure.Exchanges;
using Managing.Infrastructure.Exchanges.Abstractions;
using Managing.Infrastructure.Exchanges.Exchanges;
using Managing.Infrastructure.Messengers.Discord;
using Managing.Infrastructure.Storage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Managing.Bootstrap;

public static class WorkersBootstrap
{
    public static IServiceCollection RegisterWorkersDependencies(this IServiceCollection services,
        IConfiguration configuration)
    {
        return services
            .AddApplication()
            .AddInfrastructure(configuration);
    }

    private static IServiceCollection AddApplication(this IServiceCollection services)
    {
        services.AddSingleton<IUserService, UserService>();
        services.AddSingleton<ITradingService, TradingService>();
        services.AddSingleton<IBotFactory, BotFactory>();
        services.AddSingleton<IScenarioService, ScenarioService>();
        services.AddSingleton<IMoneyManagementService, MoneyManagementService>();
        services.AddSingleton<IPricesService, PricesService>();
        services.AddSingleton<IWorkerService, WorkerService>();
        services.AddSingleton<IAccountService, AccountService>();
        services.AddSingleton<IStatisticService, StatisticService>();
        services.AddSingleton<ITradingService, TradingService>();
        services.AddSingleton<ISettingsService, SettingsService>();
        services.AddSingleton<IBacktester, Backtester>();
        services.AddSingleton<IBotService, BotService>();
        services.AddTransient<ICommandHandler<OpenPositionRequest, Position>, OpenPositionCommandHandler>();
        services.AddTransient<ICommandHandler<ClosePositionCommand, Position>, ClosePositionCommandHandler>();

        return services;
    }

    private static IServiceCollection AddWorkers(this IServiceCollection services, IConfiguration configuration)
    {
        return services;
    }

    private static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        // Database
        services.AddSingleton<IManagingDatabaseSettings>(sp =>
            sp.GetRequiredService<IOptions<ManagingDatabaseSettings>>().Value);
        services.AddTransient(typeof(IMongoRepository<>), typeof(MongoRepository<>));

        services.AddSingleton<IInfluxDbSettings>(sp =>
            sp.GetRequiredService<IOptions<InfluxDbSettings>>().Value);

        services.AddTransient<IInfluxDbRepository, InfluxDbRepository>();

        services.AddSingleton<IPrivySettings>(sp =>
            sp.GetRequiredService<IOptions<PrivySettings>>().Value);

        // Evm 
        services.AddUniswapV2();
        services.AddGbcFeed();
        services.AddChainlink();
        services.AddChainlinkGmx();
        services.AddSingleton<IEvmManager, EvmManager>();

        // Repositories
        services.AddTransient<ICandleRepository, CandleRepository>();
        services.AddTransient<IAgentBalanceRepository, AgentBalanceRepository>();
        services.AddTransient<IWorkerRepository, WorkerRepository>();
        services.AddTransient<IStatisticRepository, StatisticRepository>();
        services.AddTransient<ICandleRepository, CandleRepository>();
        services.AddTransient<IAccountRepository, AccountRepository>();
        services.AddTransient<ISettingsRepository, SettingsRepository>();
        services.AddTransient<ITradingRepository, TradingRepository>();
        services.AddTransient<IBacktestRepository, BacktestRepository>();
        services.AddTransient<IBotRepository, BotRepository>();
        services.AddTransient<IUserRepository, UserRepository>();

        // Cache
        services.AddDistributedMemoryCache();
        services.AddTransient<ICacheService, CacheService>();
        services.AddTransient<ITaskCache, TaskCache>();

        // Processors
        services.AddTransient<IExchangeProcessor, EvmProcessor>();

        // Web Clients
        services.AddTransient<ITradaoService, TradaoService>();
        services.AddTransient<IExchangeService, ExchangeService>();
        services.AddSingleton<IBinanceSocketClient, BinanceSocketClient>();
        services.AddSingleton<IKrakenSocketClient, KrakenSocketClient>();
        services.AddSingleton<IPrivyService, PrivyService>();

        // Web3Proxy Configuration
        services.Configure<Web3ProxySettings>(configuration.GetSection("Web3Proxy"));
        services.AddTransient<IWeb3ProxyService, Web3ProxyService>();

        // Http Clients
        services.AddHttpClient();

        // Messengers
        services.AddSingleton<IMessengerService, MessengerService>();
        services.AddSingleton<IDiscordService, DiscordService>();
        services.AddSingleton<IWebhookService, WebhookService>();

        return services;
    }
}