using AyCode.Core.Extensions;
using Mango.Nop.Core;
using Mango.Nop.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Infrastructure;
using Nop.Data;
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.AuctionPlugin.Domains.EventConsumers;
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Catalog;
using Nop.Services.Configuration;
using Nop.Services.Directory;
using Nop.Services.Localization;
using Nop.Services.Orders;
using Nop.Web.Framework;
using Nop.Web.Framework.Mvc.Routing;

namespace Nop.Plugin.Misc.AuctionPlugin.Infrastructure
{
    public class PluginNopStartup : INopStartup
    {
        public int Order => 999;
        /// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration of the application</param>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });

            services.AddSingleton<ILockService, LockService>();
            services.AddSingleton<SessionService>();
            services.AddSignalR(hubOptions =>
            {
                hubOptions.ClientTimeoutInterval = TimeSpan.FromMinutes(20);
                hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(10);
            });

            services.AddScoped<ILocalizationService, LocalizationService>();
            services.AddScoped<ISettingService, SettingService>();
            services.AddScoped<AuctionEventConsumer>();
            services.AddScoped<IWorkContext, WebWorkContext>();
            services.AddScoped<IAnnouncementService, AnnouncementService>();
            services.AddScoped<AuctionService>();
            services.AddScoped<IProductAttributeService, ProductAttributeService>();
            services.AddScoped<UrlHelperFactory>();
            services.AddScoped<IActionContextAccessor, ActionContextAccessor>();

            services.AddScoped<AuctionDbTable>();
            services.AddScoped<ProductToAuctionDbTable>();
            services.AddScoped<AuctionBidDbTable>();
            services.AddScoped<AuctionDbContext>();
            
            services.AddScoped<MyProductModelFactory>(); //TODO: ez mi? - J.

            services.AddScoped<IOrderService, OrderService>();
            services.AddScoped<IOrderProcessingService, OrderProcessingService>();
            services.AddScoped<IShoppingCartService, ShoppingCartService>();
            services.AddScoped<IStoreContext, WebStoreContext>();
            services.AddScoped<ICurrencyService, CurrencyService>();

            services.AddHostedService<AuctionBackgroundService>();
        }

        /// <summary>
        /// Configure the using of added middleware
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        public void Configure(IApplicationBuilder application)
        {
            SerializeObjectExtensions.Options.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
            SerializeObjectExtensions.Options.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            SerializeObjectExtensions.Options.NullValueHandling = NullValueHandling.Ignore;
            SerializeObjectExtensions.Options.ContractResolver = new CamelCasePropertyNamesContractResolver();

            application.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<AuctionHub>("/auctionhub");
            });
            application.UseCors(options => {
                options.AllowAnyMethod().AllowAnyHeader().AllowCredentials().SetIsOriginAllowed((hosts) => true);
            });
        }

    }
}