﻿using Autofac;
using MyJetWallet.Sdk.NoSql;
using MyJetWallet.Sdk.ServiceBus;
using MyNoSqlServer.Abstractions;
using MyServiceBus.Abstractions;
using Service.AssetsDictionary.Client;
using Service.AssetsDictionary.Client.Grpc;
using Service.AssetsDictionary.Grpc;
using Service.BaseCurrencyConverter.Client;
using Service.IndexPrices.Domain.Implementation;
using Service.IndexPrices.Domain.Models;
using Service.IndexPrices.Domain.Models.IndexInstructions;
using Service.IndexPrices.Domain.Services;
using Service.IndexPrices.Grpc;
using Service.IndexPrices.Job.Jobs;
using Service.IndexPrices.Job.Services;

namespace Service.IndexPrices.Job.Modules
{
    public class ServiceModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // --------------------------------------
            var myNoSqlClient = builder.CreateNoSqlClient(() => Program.Settings.MyNoSqlReaderHostPort);

            var assetDictionaryFactory = new AssetsDictionaryClientFactory(Program.Settings.AssetDictionaryGrpcServiceUrl);
            builder
                .RegisterInstance(assetDictionaryFactory.GetIndexAssetDictionaryService())
                .As<IIndexAssetDictionary>()
                .SingleInstance();

            builder.RegisterAssetsDictionaryClients(myNoSqlClient);
            builder.RegisterBaseCurrencyConverterClient(Program.Settings.BaseCurrencyConverterGrpcServiceUrl, myNoSqlClient);
            builder.RegisterIndexAssetClients(myNoSqlClient);
            builder.RegisterCrossInstrumentConfigClient(myNoSqlClient);

            builder.RegisterMyNoSqlWriter<CurrentPricesNoSql>(() => Program.Settings.MyNoSqlWriterUrl, CurrentPricesNoSql.TableName, true);
            builder.RegisterMyNoSqlReader<CurrentPricesNoSql>(myNoSqlClient, CurrentPricesNoSql.TableName);

            builder.RegisterMyNoSqlWriter<IndexInstructionNoSql>(() => Program.Settings.MyNoSqlWriterUrl, IndexInstructionNoSql.TableName, true);
            builder.RegisterMyNoSqlReader<IndexInstructionNoSql>(myNoSqlClient, IndexInstructionNoSql.TableName);

            // --------------------------------------

            var myServiceBus = builder.RegisterMyServiceBusTcpClient(() => Program.Settings.ServicebusHostPort, Program.LogFactory);
            builder.RegisterMyServiceBusPublisher<MyJetWallet.Domain.Prices.BidAsk>(myServiceBus, "jetwallet-external-prices", false);


            // --------------------------------------

            builder.RegisterMyNoSqlWriter<IndexPricesNoSql>(() => Program.Settings.MyNoSqlWriterUrl, IndexPricesNoSql.TableName);
            builder.RegisterMyNoSqlWriter<ConvertIndexPricesNoSql>(() => Program.Settings.MyNoSqlWriterUrl, ConvertIndexPricesNoSql.TableName);

            builder
                .RegisterType<IndexInstructionService>()
                .As<IIndexInstructionsService>()
                .SingleInstance();

            builder
                .RegisterType<IndexPricesStorage>()
                .As<IIndexPricesStorage>()
                .As<IStartable>()
                .AutoActivate()
                .SingleInstance();

            builder
                .RegisterType<RefreshIndicativePricesJob>()
                .AsSelf()
                .AutoActivate()
                .SingleInstance();

            builder
                .RegisterType<IndexPriceCalculator>()
                .As<IIndexPriceCalculator>()
                .SingleInstance();

            builder
                .RegisterType<PricesMetrics>()
                .AsSelf()
                .SingleInstance();

            builder
                .RegisterType<ConvertIndexPricesStorage>()
                .As<IConvertIndexPricesStorage>()
                .As<IStartable>()
                .AutoActivate()
                .SingleInstance();

            builder
                .RegisterType<RefreshConvertIndicativePricesJob>()
                .AsSelf()
                .AutoActivate()
                .SingleInstance();

            builder
                .RegisterType<CurrentPriceMemoryCache>()
                .As<ICurrentPriceMemoryCache>()
                .AsSelf()
                .SingleInstance()
                .AutoActivate();

            builder
                .RegisterType<CrossPriceProduser>()
                .AsSelf()
                .SingleInstance();
        }
    }
}