using Hncore.Infrastructure.AliYun;
using Hncore.Infrastructure.WebApi;
using Hncore.Wx.Open;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Text;
using Hncore.Wx.Open;
using Senparc.Weixin.MP.Containers;
using Senparc.CO2NET.RegisterServices;
using Senparc.Weixin.RegisterServices;
using Microsoft.Extensions.Options;
using Senparc.CO2NET;
using Senparc.Weixin;
using Senparc.Weixin.Entities;
using Senparc.Weixin.MP;

namespace Host
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        Hncore.Pass.BaseInfo.Startup _BaseInfoStartup;
        Hncore.Pass.Manage.Startup _ManageStartup;
        Hncore.Pass.OSS.Startup _OssStartup;
        Hncore.Pass.Vpn.Startup _CourseStartup;               
        Hncore.Pass.Sells.Startup _SellStartup;

        Hncore.Pass.PaymentCenter.Startup _PaymentCenterStartup;

        public Startup(IHostingEnvironment env)
        {
            Configuration = env.UseAppsettings();

            _BaseInfoStartup = new Hncore.Pass.BaseInfo.Startup(env);
            _ManageStartup = new Hncore.Pass.Manage.Startup(env);
            _OssStartup = new Hncore.Pass.OSS.Startup(env);
            _CourseStartup= new Hncore.Pass.Vpn.Startup(env);   
            _SellStartup = new Hncore.Pass.Sells.Startup(env);
            _PaymentCenterStartup = new Hncore.Pass.PaymentCenter.Startup(env);

        }
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddWxApi();
            //删除cookie需要
            //services.Configure<CookiePolicyOptions>(options =>
            //{                
            //    options.CheckConsentNeeded = context => false;
            //    options.MinimumSameSitePolicy = SameSiteMode.None;
            //});

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new MobileViewLocationExpander());
            });

            services.AddSingleton<AliDayu>();          
             _OssStartup.ConfigureServices(services);
            _ManageStartup.ConfigureServices(services);
            _CourseStartup.ConfigureServices(services);         
            _SellStartup.ConfigureServices(services);
            _PaymentCenterStartup.ConfigureServices(services);

            //foreach(var service in services)
            //{
            //    Console.WriteLine(service.ServiceType.FullName);
            //}
            services.AddSenparcGlobalServices(Configuration)//Senparc.CO2NET 全局注册
                    .AddSenparcWeixinServices(Configuration);//Senparc.Weixin 注册                

            return _BaseInfoStartup.ConfigureServices(services);            
        }

        public void Configure(IApplicationBuilder app
            , IApplicationLifetime applicationLifetime
            ,ILoggerFactory loggerFactory
            , IHostingEnvironment env
            , IHttpClientFactory httpFactory
            , IServiceProvider serviceProvider
            ,IOptions<SenparcSetting> senparcSetting
            , IOptions<SenparcWeixinSetting> senparcWeixinSetting)
        {          
            app.UseStaticFiles();
            _ManageStartup.Configure(app, env, loggerFactory, applicationLifetime);
            _BaseInfoStartup.Configure(app, applicationLifetime, loggerFactory);
            _CourseStartup.Configure(app, applicationLifetime,loggerFactory);
            _OssStartup.Configure(app, applicationLifetime, loggerFactory);                   
            _SellStartup.Configure(app, applicationLifetime, loggerFactory);
            _PaymentCenterStartup.Configure(app, applicationLifetime, loggerFactory);

            Home.Map.MapConfig.Config();
            app.MapWhen(context =>
            {
                return context.Request.Path.Value.StartsWith("/admin");
            }, appBuilder =>
            {
                var option = new RewriteOptions();
                option.AddRewrite(".*", "/admin/index.html", true);
                appBuilder.UseRewriter(option);
                appBuilder.UseStaticFiles();
                //appBuilder.Run(async c =>
                //{
                //    var file = env.WebRootFileProvider.GetFileInfo("index.html");

                //    c.Response.ContentType = "text/html";
                //    using (var fileStream = new FileStream(file.PhysicalPath, FileMode.Open, FileAccess.Read))
                //    {
                //        await StreamCopyOperation.CopyToAsync(fileStream, c.Response.Body, null, BufferSize, c.RequestAborted);
                //    }
                //});
            });
            app.UseWxApi();

            //关于 UseSenparcGlobal() 的更多用法见 CO2NET Demo：https://github.com/Senparc/Senparc.CO2NET/blob/master/Sample/Senparc.CO2NET.Sample.netcore/Startup.cs
            //IRegisterService register = RegisterService.Start(env, senparcSetting.Value)                                                       
            //                                            .UseSenparcGlobal();
            //register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value)
            //    .RegisterMpAccount(senparcWeixinSetting.Value, "【盛派网络小助手】公众号");
            AccessTokenContainer.RegisterAsync(Configuration["WxApps:AppID"], Configuration["WxApps:AppSecret"]);
        }
    }
}
