// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddDbContext<AppsContext>(options =>
            //options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            //Selenium Driver
            //Action<OpenQA.Selenium.Chrome.ChromeDriver> chromeDriver = (cd => { 

            //});

            //services.addeaddeAddEntityFrameworkSqlite().AddDbContext<DatabaseContext>();
            
            services.AddSingleton<OpenQA.Selenium.Chrome.ChromeDriver>(new OpenQA.Selenium.Chrome.ChromeDriver(Environment.CurrentDirectory + "\\Libraries"));

            //Main db
            Action<AppsData> liteDBOptions = (opt =>
            {
                opt.AppsDBPath = System.Environment.CurrentDirectory + "\\Business\\Data\\Apps.db";
                opt.AppsDB = new LiteDB.LiteDatabase(opt.AppsDBPath);
            });

            //Main ef context
            Action<AppsContext> contextOptions = (opt => { opt = new AppsContext(); });
            services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<AppsContext>>().Value);

            ////Flows db
            //Action<FlowsData> flowDBOptions = (opt =>
            //{
            //    opt.FlowsDBPath = System.Environment.CurrentDirectory + "\\Flows.db";
            //    opt.FlowsDB = new LiteDB.LiteDatabase(opt.FlowsDBPath);
            //});

            services.Configure(liteDBOptions);
            services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<AppsData>>().Value);


            //services.Configure(flowDBOptions);
            //services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<FlowsData>>().Value);

            //services.AddCors(options =>
            //{
            //    options.AddPolicy(MyAllowSpecificOrigins,
            //    builder =>
            //    {
            //        builder.WithOrigins("http://localhost:52780");
            //    });
            //});
            services.AddSignalR();
            services.AddControllers();
            services.AddMvcCore().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });
            //services.AddOcelot();

            //var builder = services.AddIdentityServer()
            //    .AddDeveloperSigningCredential()        //This is for dev only scenarios when you don’t have a certificate to use.
            //    .AddInMemoryApiScopes(IdentityServerTest.Config.ApiScopes)
            //    .AddInMemoryClients(IdentityServerTest.Config.Clients);

            //// this comes from Ocelot.Tracing.Butterfly package
            //services.AddButterfly(option =>
            //{
            //    //this is the url that the butterfly collector server is running on...
            //    option.CollectorUrl = "https://localhost:9618";
            //    option.Service = "Ocelot";
            //});
            //services.AddIdentityServer()
            //    .AddDeveloperSigningCredential()
            //    //.AddInMemoryApiResources(new[] { new ApiResource("socialnetwork", "Social Network") })
            //    .AddInMemoryClients(new[] { new Client{
            //         ClientId = "socialnetwork",
            //         ClientSecrets = new [] { new Secret("secret".Sha256())},
            //         AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
            //         AllowedScopes = new[] {"socialnetwork"}
            //        } 
            //    }).AddInMemoryApiScopes(new[] { new ApiScope("socialnetwork", "My API") });
            //.AddTestUsers(new List<IdentityServer4.Test.TestUser> { 
            //    new IdentityServer4.Test.TestUser
            //    {
            //        SubjectId = "1",
            //        Username = "******",
            //        Password = "******"
            //    }

            //services.AddAuthentication("Bearer").AddJwtBearer("Bearer", options =>
            //{
            //    options.Authority = "https://localhost:54321";
            //    options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            //    {
            //        ValidateAudience = false
            //    };
            //});

            //// Add Hangfire services.
            //services.AddHangfire(configuration => configuration
            //    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            //    .UseSimpleAssemblyNameTypeSerializer()
            //    .UseRecommendedSerializerSettings()
            //    .UseMemoryStorage());

            //// Add the processing server as IHostedService
            //services.AddHangfireServer();
            //var flowsDb = new LiteDB.LiteDatabase(System.Environment.CurrentDirectory + "\\Flows.db");
            //FlowsData.FlowTable = flowsDb.GetCollection<AppFlowEvent>("Flows");

            //var clientConfig = new AppsClientConfig();
            //clientConfig.Load("Brooksoft.Apps", Environment.MachineName, Environment.CurrentDirectory, new System.Version(0, 0, 1), new List<string>(), new List<AppsCustomConfigItem>(), true, true, new AppFlow());

            AppsLog.LogInfo("Finished configure services.");
            //var perms = new Brooksoft.Apps.Business.Publish.Permissions();
            //perms.ReadEffectivePermissions();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppsData appsdb)
        {
            //app.UseCors(MyAllowSpecificOrigins);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseDefaultFiles(new DefaultFilesOptions { DefaultFileNames = new List<string> { "index.html" } });

            app.UseStaticFiles();

            //app.UseHangfireDashboard();
            
            app.UseHttpsRedirection();

            app.UseRouting();

            //app.UseAuthentication();

            //app.UseAuthorization();

            //app.addj.AddJsonOptions(options =>
            // {
            //     options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            //     options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            // });
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<AppsHub>("/appsHub");
            });

            //app.UseIdentityServer();
            //app.UseOcelot().Wait(); //Must go after id server

            AppsLog.Load("Apps"); //already done with config registration
            var hub = new AppsHub(appsdb);
            hub.Load();
            FlowsData.Load();

            //AppsHub.TestProgress("hiya");
            //var aTimer = new System.Timers.Timer(5000);
            //// Hook up the Elapsed event for the timer. 
            //aTimer.Elapsed += ATimer_Elapsed; ;
            //aTimer.AutoReset = true;
            //aTimer.Enabled = true;

            //AppsDB = appsdb.AppsDB;
            //FlowsDB = flowsdb.FlowsDB;
            //var solutionDirectory = @"D:\Work\Brooksoft\AppsJS\AppsJSDemo\AppsJSDemo\AppsJSDemo.sln";
            //var projectFilePath = @"D:\Work\Brooksoft\AppsJS\AppsJSDemo\AppsJSDemo\AppsJSDemo.csproj";

            //var globalProperties = new Dictionary<string, string> {
            //    { "DesignTimeBuild", "true" },
            //    { "BuildProjectReferences", "false" },
            //    { "_ResolveReferenceDependencies", "false" },
            //    { "SolutionDir", solutionDirectory + System.IO.Path.DirectorySeparatorChar }
            //};

            //var collection = new Microsoft.Build.Evaluation.ProjectCollection();
            //Microsoft.Build.Evaluation.Project project = collection.LoadProject(projectFilePath);

            //Run();

            //hangfireJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
            AppsLog.LogInfo("Finished configure.");
        }