using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HBLConsole.Model;
using HBLConsole.Service;
using HBLConsole.GVL;
using HBLConsole.Communication;
using BPA.Message;
using HBLConsole.Business;
using HBLConsole.Factory;
using BPA.Message.Enum;

namespace HBLConsole.MainConsole
{
    public class Main
    {

        private volatile static Main _Instance;
        public static Main GetInstance => _Instance ?? (_Instance = new Main());
        private Main() { }

        List<string> Topics = new List<string>();

        public void DataInit()
        {
            Json<SetPar>.Read();
            if (Enum.TryParse(Json<SetPar>.Data.ClientDeviceType, out DeviceClientType dct))
            {
                GeneralConfig.DeviceType = dct;
            }
            else { GeneralConfig.DeviceType = DeviceClientType.MORKS; }
            Json<MorkOrderPushPar>.Read();
            Json<BatchingInfoPar>.Read();
            Json<SimOrderConfig>.Read();
            Json<KeepDataBase>.Read();
            MessageLog.GetInstance.Show($"启动【{GeneralConfig.DeviceType}】设备");
        }


        public void DataSave()
        {
            Json<MorkOrderPushPar>.Save();
            Json<BatchingInfoPar>.Save();
            Json<SetPar>.Save();
            Json<SimOrderConfig>.Save();
            Json<KeepDataBase>.Save();
            TextHelper.GetInstance.SaveLogInfo(MessageLog.GetInstance.LogInfo, "LogInfo");
        }

        public void BusinessInit()
        {
            ThreadOperate.GetInstance.Start(new Action(() =>
            {
                InternetInfo.ConfigInit();//从 consul 获取配置数据
                //SimpleFactory.GetInstance.DeviceInit();//设备初始化
                Topics.Clear();
                Topics.Add(TOPIC.GetInstance.GetOrderPushTopic(GeneralConfig.DeviceType, InternetInfo.ClientId));
                Topics.Add(TOPIC.GetInstance.GetBusinessTopic(GeneralConfig.DeviceType, InternetInfo.ClientId));

                //MQTT 连接成功
                MqttHelper.GetInstance.ConnectOk = new Action(() =>
                {
                    SimpleFactory.GetInstance.DeviceInit();//设备初始化

                    MqttHelper.GetInstance.MqttSubscriptionAsync(Topics.ToArray());//主题订阅

                    //w IotReport.GetInstance.Init();//IOT 上报

                    HeartbeatReport.GetInstance.Init();//心跳上报

                    ServerData.GetInstance.Init();//数据处理初始化

                    //接收MQTT消息
                    MqttHelper.GetInstance.MqttReceive = new Action<MQTTnet.MqttApplicationMessageReceivedEventArgs>((receivce) =>
                    {
                        ServerData.GetInstance.ReceiveData(Encoding.UTF8.GetString(receivce.ApplicationMessage.Payload));
                    });
                });

                //MQTT 重连成功
                MqttHelper.GetInstance.Reconnection = new Action(() => { MqttHelper.GetInstance.MqttSubscriptionAsync(Topics.ToArray()); });

                //MQTT 连接
                MqttHelper.GetInstance.MqttInitAsync(InternetInfo.MqttUserName, InternetInfo.MqttPassword,
                    InternetInfo.MqttAddress, InternetInfo.MqttPort, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));

            }), "业务逻辑初始化");
        }

    }
}
