using Bp.Sync.BackEnd.Application.Commands.Bills.Bills;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.Bills;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.InboundStatistics;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.OutboundStatistics;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.ProductInbounds;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.ProductOutbounds;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.Products;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.Products.ProductShipments;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.Products.ProductSummaries;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.RetailStatistics;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.UnitExchanges;
using Bp.Sync.BackEnd.Application.Commands.FcCustomers;
using Bp.Sync.BackEnd.Application.Commands.Finances.Transactions;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Accounts;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Batches;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Bills.Bills;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Bills.InboundBills;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Bills.InventoryBills;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Bills.OutboundBills;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Bills.RetailBills;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Branches;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Categories;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Configurations;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Customers;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Generals;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Invoices;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Orders;
using Bp.Sync.BackEnd.Application.Commands.Inventories.ProductInvoices;
using Bp.Sync.BackEnd.Application.Commands.Inventories.ProductOrders;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Products;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Products.ProductBills;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Promotions;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Reasons;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Suppliers;
using Bp.Sync.BackEnd.Application.Commands.Inventories.Units;
using CommonLibrary;
using EasyNetQ;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace Bp.Sync.BackEnd
{
    public static class AppBuilderExtension
    {
        public static IApplicationBuilder UseSubscribe(this IApplicationBuilder appBuilder)
        {
            var services = appBuilder.ApplicationServices.CreateScope().ServiceProvider;
            var lifeTime = services.GetService<IApplicationLifetime>();
            var bus = services.GetService<IBus>();
            lifeTime.ApplicationStarted.Register(() =>
            {
                var mediator = services.GetService<IMediator>();

                #region Transaction

                bus.Receive<string>("SyncToElasticTransaction0", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction1", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction2", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction3", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction4", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction5", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction6", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction7", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction8", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticTransaction9", @request => { mediator.Send(new SyncTransactionCommand(request.ConvertStringToListLong())); });

                #endregion Transaction

                #region Bill

                bus.Receive<string>("SyncToElasticBill", @request => { mediator.Send(new SyncBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticBill0", @request => { mediator.Send(new SyncBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticBill1", @request => { mediator.Send(new SyncBillCommand(request.ConvertStringToListLong())); });

                bus.Receive<string>("UpdateChildBillByBill", @request => { mediator.Send(new UpdateChildBillByBillIdCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateOrderById", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });

                bus.Receive<string>("SyncToElasticBatch", @request => { mediator.Send(new SyncBatchCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductBill", @request => { mediator.Send(new SyncProductBillCommand(request.ConvertStringToListLong())); });

                bus.Receive<string>("SyncToElasticOutboundBill", @request => { mediator.Send(new SyncOutboundBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInboundBill", @request => { mediator.Send(new SyncInboundBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInventoryBill", @request => { mediator.Send(new SyncInventoryBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateInventoryOutboundBill", @request => { mediator.Send(new UpdateOutboundBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateInventoryInboundBill", @request => { mediator.Send(new UpdateInboundBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateInventoryInventoryBill", @request => { mediator.Send(new UpdateInventoryBillCommand(request.ConvertStringToListLong())); });

                #endregion Bill

                #region Invoice

                bus.Receive<string>("SyncToElasticInvoice0", @request => { mediator.Send(new SyncInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInvoice1", @request => { mediator.Send(new SyncInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInvoice2", @request => { mediator.Send(new SyncInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInvoice3", @request => { mediator.Send(new SyncInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInvoice4", @request => { mediator.Send(new SyncInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInvoice5", @request => { mediator.Send(new SyncInvoiceCommand(request.ConvertStringToListLong())); });

                #endregion Invoice

                #region Product

                bus.Receive<string>("SyncToElasticProduct0", @request => { mediator.Send(new SyncProductCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProduct1", @request => { mediator.Send(new SyncProductCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProduct2", @request => { mediator.Send(new SyncProductCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProduct3", @request => { mediator.Send(new SyncProductCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProduct4", @request => { mediator.Send(new SyncProductCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProduct5", @request => { mediator.Send(new SyncProductCommand(request.ConvertStringToListLong())); });

                bus.Receive<string>("SyncToElasticUserDistrict", @request => { mediator.Send(new SyncUserDistrictCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticCategory", @request => { mediator.Send(new SyncCategoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticPromotion", @request => { mediator.Send(new SyncPromotionCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticPromotionProduct", @request => { mediator.Send(new SyncPromotionProductCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductGroup", @request => { mediator.Send(new SyncProductGroupCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductType", @request => { mediator.Send(new SyncProductTypeCommand(request.ConvertStringToListLong())); });

                #endregion Product

                #region ProductInventory

                bus.Receive<string>("SyncToElasticProductInventory0", @request => { mediator.Send(new SyncProductInventoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInventory1", @request => { mediator.Send(new SyncProductInventoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInventory2", @request => { mediator.Send(new SyncProductInventoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInventory3", @request => { mediator.Send(new SyncProductInventoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInventory4", @request => { mediator.Send(new SyncProductInventoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInventory5", @request => { mediator.Send(new SyncProductInventoryCommand(request.ConvertStringToListLong())); });
                //cập nhật
                bus.Receive<string>("UpdateElasticProductInventory0", @request => { mediator.Send(new UpdateInvoiceToProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateElasticProductInventory1", @request => { mediator.Send(new UpdateInvoiceToProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateElasticProductInventory2", @request => { mediator.Send(new UpdateInvoiceToProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateElasticProductInventory3", @request => { mediator.Send(new UpdateInvoiceToProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateElasticProductInventory4", @request => { mediator.Send(new UpdateInvoiceToProductInvoiceCommand(request.ConvertStringToListLong())); });

                bus.Receive<string>("UpdateToElasticProductInventoryEx", @request => { mediator.Send(new UpdateProductInventoryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateToElasticProductInventoryNotEx", @request => { mediator.Send(new UpdateExpiryDateProductInventoryCommand(request.ConvertStringToListLong())); });

                #endregion ProductInventory

                #region ProductInvoice

                bus.Receive<string>("SyncToElasticProductInvoice0", @request => { mediator.Send(new SyncProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInvoice1", @request => { mediator.Send(new SyncProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInvoice2", @request => { mediator.Send(new SyncProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInvoice3", @request => { mediator.Send(new SyncProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInvoice4", @request => { mediator.Send(new SyncProductInvoiceCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductInvoice5", @request => { mediator.Send(new SyncProductInvoiceCommand(request.ConvertStringToListLong())); });

                #endregion ProductInvoice

                #region ProductInvoice

                bus.Receive<string>("SyncToElasticOrder0", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticOrder1", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticOrder2", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticOrder3", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticOrder4", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticOrder5", @request => { mediator.Send(new SyncOrderCommand(request.ConvertStringToListLong())); });

                #endregion ProductInvoice

                #region ProductOrder

                bus.Receive<string>("SyncToElasticProductOrder0", @request => { mediator.Send(new SyncProductOrderCommand(request.ConvertStringToListLong(), new System.Collections.Generic.List<long>())); });
                bus.Receive<string>("SyncToElasticProductOrder1", @request => { mediator.Send(new SyncProductOrderCommand(request.ConvertStringToListLong(), new System.Collections.Generic.List<long>())); });
                bus.Receive<string>("SyncToElasticProductOrderByOrder", @request => { mediator.Send(new SyncProductOrderCommand(new System.Collections.Generic.List<long>(), request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateToElasticProductOrder", @request => { mediator.Send(new UpdateProductOrderCommand(request.ConvertStringToListLong(), new System.Collections.Generic.List<long>())); });
                bus.Receive<string>("UpdateToElasticProductOrderByOrder", @request => { mediator.Send(new UpdateProductOrderCommand(new System.Collections.Generic.List<long>(), request.ConvertStringToListLong())); });

                #endregion ProductOrder

                #region RetailBill

                bus.Receive<string>("SyncToElasticRetailBill0", @request => { mediator.Send(new SyncRetailBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticRetailBill1", @request => { mediator.Send(new SyncRetailBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticRetailBill2", @request => { mediator.Send(new SyncRetailBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticRetailBill3", @request => { mediator.Send(new SyncRetailBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticRetailBill4", @request => { mediator.Send(new SyncRetailBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticRetailBill5", @request => { mediator.Send(new SyncRetailBillCommand(request.ConvertStringToListLong())); });

                #endregion RetailBill

                #region Branch

                bus.Receive<string>("SyncToElasticBranch", @request => { mediator.Send(new SyncBranchCommand(request.ConvertStringToListLong())); });

                #endregion Branch

                #region Unit

                bus.Receive<string>("SyncToElasticContractDiscountLevel", @request => { mediator.Send(new SyncContractDiscountLevelCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticDiscountBranchDetailConfig", @request => { mediator.Send(new SyncDiscountBranchDetailConfigCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticBaseConfiguration", @request => { mediator.Send(new SyncBaseConfigurationCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticWholesalePriceList", @request => { mediator.Send(new SyncWholesalePriceListCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticUnit", @request => { mediator.Send(new SyncUnitCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductUnit", @request => { mediator.Send(new SyncProductUnitCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticDebt", @request => { mediator.Send(new SyncDebtCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticGeneralLedger", @request => { mediator.Send(new SyncGeneralLedgerCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticAccount", @request => { mediator.Send(new SyncAccountCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticReason", @request => { mediator.Send(new SyncReasonCommand(request.ConvertStringToListLong())); });

                #endregion Unit

                #region Customer

                bus.Receive<string>("SyncToElasticCustomer", @request => { mediator.Send(new SyncCustomerCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticSupplier", @request => { mediator.Send(new SyncSupplierCommand(request.ConvertStringToListLong())); });

                #endregion Customer

                #region InboundStatistic

                bus.Receive<string>("SyncToElasticInboundStatistic", @request => { mediator.Send(new SyncInboundStatisticCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticInboundStatistic0", @request => { mediator.Send(new SyncInboundStatisticCommand(request.ConvertStringToListLong())); });

                #endregion InboundStatistic

                #region SyncFcProductInboundCommand

                bus.Receive<string>("SyncToElasticFcProductOutbound", @request => { mediator.Send(new SyncFcProductOutboundCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcProductOutbound0", @request => { mediator.Send(new SyncFcProductOutboundCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcProductInbound", @request => { mediator.Send(new SyncFcProductInboundCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcProductInbound0", @request => { mediator.Send(new SyncFcProductInboundCommand(request.ConvertStringToListLong())); });

                #endregion SyncFcProductInboundCommand

                #region RetailStatistic

                bus.Receive<string>("SyncToElasticRetailStatistic", @request => { mediator.Send(new SyncRetailStatisticCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticRetailStatistic0", @request => { mediator.Send(new SyncRetailStatisticCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncUpdateRetailStatistic", @request => { mediator.Send(new UpdateRetailStatisticCommand(request.ConvertStringToListLong())); });

                #endregion RetailStatistic

                #region OutboundStatistic

                bus.Receive<string>("SyncToElasticOutboundStatistic", @request => { mediator.Send(new SyncOutboundStatisticCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticOutboundStatistic0", @request => { mediator.Send(new SyncOutboundStatisticCommand(request.ConvertStringToListLong())); });

                #endregion OutboundStatistic

                #region FcCustomer

                bus.Receive<string>("SyncToElasticFcUnitExchange", @request => { mediator.Send(new SyncFcUnitExchangeCommand(request.ConvertStringToListLong())); });

                bus.Receive<string>("SyncToElasticFcCustomer", @request => { mediator.Send(new SyncFcCustomerCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcInboundBill", @request => { mediator.Send(new SyncFcInboundBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcOutboundBill", @request => { mediator.Send(new SyncFcOutboundBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcReceiptBill", @request => { mediator.Send(new SyncFcReceiptBillCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcProductStorage", @request => { mediator.Send(new SyncFcProductStorageCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcProductShipment", @request => { mediator.Send(new SyncFcProductShipmentCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticFcProductSummary", @request => { mediator.Send(new SyncFcProductSummaryCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("SyncToElasticProductStorageDav", @request => { mediator.Send(new SyncProductStorageDavCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateFcProductStorage", @request => { mediator.Send(new UpdateFcProductStorageCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateFcProductStorageNotEx", @request => { mediator.Send(new UpdateExpiryDateFcProductStorageCommand(request.ConvertStringToListLong())); });
                bus.Receive<string>("UpdateFcChildBillByBill", @request => { mediator.Send(new UpdateFcChildBillByBillIdCommand(request.ConvertStringToListLong())); });

                #endregion FcCustomer

                bus.Receive<string>("AddHistoryElastic", @request => { mediator.Send(new Application.Commands.Systems.Histories.AddHistoryElasticCommand(request)); });
                bus.Receive<string>("ReadHistoryElastic", @request => { mediator.Send(new Application.Commands.Systems.Histories.ReadHistoryElasticCommand(request)); });
            });

            lifeTime.ApplicationStopped.Register(() => bus.Dispose());
            return appBuilder;
        }
    }
}