using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HBLConsole.Service;
using System.Collections.Concurrent;
using BPA.Message;
using HBLConsole.GVL;
using HBLConsole.Interface;
using HBLConsole.Factory;

namespace HBLConsole.Business
{
    public class ServerData
    {

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

        ConcurrentQueue<string> receives = new ConcurrentQueue<string>();

        public void Init()
        {
            ThreadOperate.GetInstance.StartLong(new Action(() =>
            {
                while (receives.Count > 0)
                {
                    if (receives.TryDequeue(out string msg))
                    {
                        var package = BPAPackage.Deserialize(msg);
                        if (package?.ClientId == InternetInfo.ClientId)
                        {
                            if (package.Message != null)
                            {
                                SimpleFactory.GetInstance.MqttMessage(package.Message);
                            }
                        }
                    }
                }
                Thread.Sleep(100);
            }), "mqtt消息处理");
        }


        public void ReceiveData(string info)
        {
            receives.Enqueue(info);
        }

    }
}
