using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using WCS.BLL.Config;
using WCS.BLL.DbModels;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.WebApi;
using WCS.WebApi.Controllers;

namespace WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                //初始化websocket
                WebSoceketManager.InitWebSocket();

                //初始化数据库
                DbInit.InitDb();

                //初始化配置文件
                LocalFile.SaveConfig();

                //报警信息重发线程
                WarningManager.StartWarningMessageThread();

                //赛特公司AGV后台线程
                AGVManager.InitBackgroundThread();

                ////盟讯公司后台线程
                //if (LocalFile.Config.IsMx)
                //{
                //    MXBackgroundThread.InitBackgroundThread();
                //    var str = string.Empty;
                //    MXBackgroundThread.SendDingDingMsg($"【{LocalFile.Config.GroupName}】后台服务启动成功", new List<string> { "104379" }, ref str);
                //}

                //清理后台文本日志
                Task.Run(async () =>
                {
                    while (true)
                    {
                        try
                        {
                            //清理三个月以前的日志
                            WCS.BLL.Logs.Clear(TimeSpan.FromDays(92));
                            //每一天执行一次
                            await Task.Delay(1000 * 60 * 60 * 24);
                        }
                        catch (Exception ex)
                        { 
                        }
                    }
                });



                var builder = WebApplication.CreateBuilder(args);
                // Add services to the container.
                builder.Services.AddControllers();
                // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
                builder.Services.AddEndpointsApiExplorer();
                builder.Services.AddSwaggerGen();

                builder.Services.AddScoped<IInstoreService, InstoreService>();
                builder.Services.AddScoped<IOutstoreService, OutstoreService>();
                builder.Services.AddScoped<IHomerService, HomerService>();
                builder.Services.AddScoped<IUserService, UserService>();
                builder.Services.AddScoped<IInterfaceRecordService, InterfaceRecordService>();
                builder.Services.AddScoped<IMatBaseInfoService, MatBaseInfoService>();
                builder.Services.AddScoped<IMatInventoryDetailService, MatInventoryDetailService>();
                builder.Services.AddScoped<IStoreInfoService, StoreInfoService>();
                builder.Services.AddScoped<IStockTakingService, StockTakingService>();
                builder.Services.AddScoped<ISelfCheckService, SelfCheckService>();
                builder.Services.AddScoped<IWarningService, WarningService>();
                builder.Services.AddScoped<IInOutRecordService, InOutRecordService>();
                builder.Services.AddScoped<IUploadService, UploadService>();
                builder.Services.AddScoped<ILocationInfoService, LocationInfoService>();
                builder.Services.AddScoped<IMatDetailCurrentInfoService,MatDetailCurrentInfoService>();
                builder.Services.AddScoped<IBatchBindMatDetailService, BatchBindMatDetailService>();
                builder.Services.AddScoped<IPDAShelfLocationBindUnbindService, PDAShelfLocationBindUnbindService>();

                //生成物料码、生成单据码采用单例模式
                builder.Services.AddSingleton<IGenerateService, GenerateService>();
                builder.Services.AddSingleton<IPDAMatBindService, PDAMatBindService>();

                var app = builder.Build();

                app.UseMiddleware<RequestResponseLoggingMiddleware>();
                // Configure the HTTP request pipeline.
                if (app.Environment.IsDevelopment())
                {
                    app.UseSwagger();
                    app.UseSwaggerUI();
                }

                app.UseAuthorization();
                app.MapControllers();
                app.Run("http://0.0.0.0:8888");//0.0.0.0表示仅接收ipv4
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
