using System;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using XYY.TaskTrack.Standard;
using EasyNetQ;
using Serilog;
using XYY.WindowsService.ReshMQ.Works;
using Grpc.Net.Client;
using Agile.Config.Client;
using System.Net.Http;
using Grpc.Core;

namespace XYY.WindowsService.ReshMQ
{
    class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }



        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            string trackingGRpcUrl = "https://47.244.232.78:7002";
            string zipUpdateGrpcUrl = "https://47.244.232.78:7003";

//#if DEBUG
//            trackingGRpcUrl = "https://localhost:7002";
//            zipUpdateGrpcUrl = "https://localhost:7003";
//            //System.Threading.Thread.Sleep(3000);
//#endif

            return Host.CreateDefaultBuilder(args)
                .UseWindowsService()//使用windows服务
                .ConfigureServices((hostContext, services) =>
                {
                    var httpHandler = new HttpClientHandler();
                    // Return `true` to allow certificates that are untrusted/invalid
                    httpHandler.ServerCertificateCustomValidationCallback =
                        HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

                    var channel = GrpcChannel.ForAddress(trackingGRpcUrl,
                        new GrpcChannelOptions { HttpHandler = httpHandler });
                    var TrackingGrpcChanel = new TrackingGrpcChanel
                    {
                        Channel = channel
                    };

                    var OrderGrpcChannel = new OrderGrpcChannel
                    {
                        Channel = GrpcChannel.ForAddress(zipUpdateGrpcUrl,
                        new GrpcChannelOptions { HttpHandler = httpHandler })
                    };


                    services.AddSingleton(x => RabbitHutch.CreateBus(hostContext.Configuration["DBConnectionStrings:RabbitConnection"]));
                    services.AddSingleton<IMQManager, MQManager>();
                    services.AddSingleton(x => (TrackingGrpcChanel));
                    services.AddSingleton(x => (OrderGrpcChannel));
                    services.AddSingleton<ISeventeenGRPC, SeventeenGRPC>();
                    services.AddSingleton<IMoreThanOneTicketGrpc, MoreThanOneTicketGrpc>();
                    var fiddlerLog = new LoggerConfiguration()
                        .MinimumLevel.Warning()
                        .MinimumLevel.Override("LogginService", Serilog.Events.LogEventLevel.Warning)
                        .Enrich.FromLogContext()
                        .WriteTo.Seq("http://47.244.232.78:5341/")
                        .WriteTo.Debug()
                      .CreateLogger();

                    services.AddLogging(logging =>
                    {
                        logging.AddSerilog(fiddlerLog);
                    });
                    services.AddHostedService<Work>();
                }).ConfigureAppConfiguration((context, config) =>
                {
                    //使用AddAgileConfig配置一个新的IConfigurationSource
                    config.AddAgileConfig();
                });
        }
    }
}