using System;
using System.IO;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
using TouchSocket.WebApi.Swagger;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本:4.0.30319.42000
* 机器名称:T14-GEN3-7895
* 命名空间:SlnMesnac.RfidUpload.TouchSocket
* 唯一标识:2f39e963-acb9-44d1-9980-27aaa910de29
*
* 创建者:WenJY
* 电子邮箱:
* 创建时间:2024-07-02 15:35:25
* 版本:V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本:V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.RfidUpload.TouchSocket
{
    public sealed class WebApiServerApp
    {
        private static readonly Lazy<WebApiServerApp> _lazy = new Lazy<WebApiServerApp>(() => new WebApiServerApp());

        public static WebApiServerApp Instance
        {
            get
            {
                return _lazy.Value;
            }
        }

        private WebApiServerApp() { }

        private ApiServer apiServer = ApiServer.GetInstance();

        public void Init()
        {
            try
            {
                var service = new HttpService();
                service.Setup(new TouchSocketConfig()
                   .SetListenIPHosts(7789)
                   .ConfigureContainer(a =>
                   {
                       a.AddRpcStore(store =>
                       {
                           
                           store.RegisterServer<ApiServer>(apiServer);//注册服务
                       });

                       a.AddCors(corsOption =>
                       {
                           corsOption.Add("cors", corsBuilder =>
                           {
                               corsBuilder.AllowAnyMethod()
                                   .AllowAnyOrigin();
                           });
                       });

                       a.AddLogger(logger =>
                       {
                           logger.AddConsoleLogger();
                           logger.AddFileLogger();
                       });
                   })
                   .ConfigurePlugins(a =>
                   {
                       a.UseCheckClear();

                       a.Add<AuthenticationPlugin>();

                       a.UseWebApi()
                       .ConfigureConverter(converter =>
                       {
                           converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });
                       });

                       a.UseSwagger();//使用Swagger页面
                       //.UseLaunchBrowser();

                       a.UseDefaultHttpServicePlugin();
                   }));
                service.Start();

                Console.WriteLine("以下连接用于测试webApi");
                Console.WriteLine($"使用:http://127.0.0.1:7789/swagger/index.html");
            }catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            //Console.ReadLine();
        }
    }

    internal class AuthenticationPlugin : PluginBase, IHttpPlugin
    {
        public async Task OnHttpRequest(IHttpSocketClient client, HttpContextEventArgs e)
        {
            await e.InvokeNext();
        }
    }
}