using System.Reflection;
using System.Runtime.InteropServices;
using CFX;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Sln.Iot.Business;
using Sln.Iot.CFX;
using Sln.Iot.CFX.CFXConnect;
using Sln.Iot.CFX.Event;
using Sln.Iot.Config;
using Sln.Iot.PLC;
using Sln.Iot.Repository;
using Sln.Iot.Serilog;
using TouchSocket.Sockets;

namespace Sln.Iot
{
    /// <summary>
    /// 程序主入口
    /// </summary>
    public class Program
    {
        public delegate bool ControlCtrlDelegate(int CtrlType);
        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);
        private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerRoutine);

        public static bool HandlerRoutine(int CtrlType)
        {
            try
            {
                switch (CtrlType)
                {
                    case 0:
                        ProgramClose();
                        break;
                    case 2:
                        ProgramClose();
                        break;
                }
            }
            catch (Exception ex)
            {
                SerilogHelper.Instance.Error("程序退出事件异常", ex);
            }
            finally
            {
                SerilogHelper.Instance.Info("系统关闭");
            }
            
            return false;
        }

        static async Task Main(string[] args)
        {
            SetConsoleCtrlHandler(cancelHandler, true);
            //Serilog中间件
            SerilogExtensions.UseSerilogExtensions();
            //日志实例
            var log = SerilogHelper.Instance;
            //配置文件加载
            var appConfig = AppConfigSetting.Load();
            //PLC连接初始化
            PLCConnect.Instance.InitConnect();

            //CFX接口启动
            //真空注胶机
            CFXConnect1.Instance.Init("CFX.A00.SDSH000001",
                new Uri("amqp://127.0.0.1:1235"),
                new Uri("amqp://127.0.0.1:30031"));
            //升降回流
            CFXConnect2.Instance.Init("CFX.A00.SDSH000002",
                new Uri("amqp://127.0.0.1:1236"),
                new Uri("amqp://127.0.0.1:30031"));
            //隧道烤箱
            CFXConnect3.Instance.Init("CFX.A00.SDSH000003",
                new Uri("amqp://127.0.0.1:1237"),
                new Uri("amqp://127.0.0.1:30031"));


            ////CFX接口启动
            ////真空注胶机
            //CFXConnect1.Instance.Init("CFX.A00.SDSH000001", 
            //    new Uri("amqp://127.0.0.1:1235"), 
            //    new Uri("amqp://127.0.0.1:8888"));
            ////升降回流
            //CFXConnect2.Instance.Init("CFX.A00.SDSH000002",
            //    new Uri("amqp://127.0.0.1:1235"),
            //    new Uri("amqp://127.0.0.1:8888"));
            ////隧道烤箱
            //CFXConnect3.Instance.Init("CFX.A00.SDSH000003",
            //    new Uri("amqp://127.0.0.1:1235"),
            //    new Uri("amqp://127.0.0.1:8888"));


            //业务类启动
            BusinessStart business = new BusinessStart();

            log.Info($"系统启动成功,日志存放位置:{appConfig.logPath}");

            //CFXTest cFXTest = new CFXTest();
            //cFXTest.Test1();

            //TestBusiness test = new TestBusiness();

            await Task.Delay(-1);
        }

        /// <summary>
        /// 程序退出事件
        /// </summary>
        public static void ProgramClose()
        {
            CFXConnect1.Instance.PublishEvent(new CFXEnvelope(new EndpointShuttingDownEvent().Handle(CFXConnect1.Instance.CFXHandle)));
            CFXConnect2.Instance.PublishEvent(new CFXEnvelope(new EndpointShuttingDownEvent().Handle(CFXConnect2.Instance.CFXHandle)));
            CFXConnect3.Instance.PublishEvent(new CFXEnvelope(new EndpointShuttingDownEvent().Handle(CFXConnect3.Instance.CFXHandle)));
        }
    }
}