using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDH.Data.Models;
using RDH.PharmacyPlatform.Sync.Core;

namespace Rdh.SocketServer.Client.MQ
{
    /// <summary>
    /// 表示消息服务的类
    /// </summary>
    public class MQService
    {
        private String? _serviceState;
        private Boolean _initialized;
        private static MQService _singleton;
        static MQService() 
        { 
            _singleton = new MQService(); 
        }
        private MQService() 
        {
            _serviceState = "未初始化";
            _initialized = false;
        }
        public static MQService Singleton 
        {
            get => _singleton;
        }
        public String? EleMedKitCabinetNodeName { get; set; }
        public String? Init() 
        {
            _initialized = true;
            _serviceState = Bootstrap.Init();
            if (_serviceState == null) 
            {
                Bootstrap.RegisteLogger(new MQLogger());
                _serviceState = MessageClient.Register();
            }
            return _serviceState;
        }
        public String? Send(MessageData data) 
        {
            if (!_initialized) 
            {
                Init();
            }
            if (_serviceState != null) 
            {
                return _serviceState;
            }
            return MessageClient.SendMessage(data);
        }
        /// <summary>
        /// 发送请求归还药箱的消息
        /// </summary>
        public String? RequestReturnMedKit(UserInfo userInfo, StorageSpacePortableLink storageLink) 
        {
            if (EleMedKitCabinetNodeName == null) 
            {
                return "请设置药箱柜同步节点名称";
            }
            MessageData data = new MessageData
            {
                CreateTime = DateTime.Now,
                FilterType = MessageFilterType.Id,
                MessageContent = JsonSerializer.SerializeObject(new Tuple<UserInfo, StorageSpacePortableLink>(userInfo, storageLink)),
                MessageContentType = "RequestReturnMedKit",
                SourceId = Bootstrap.Config.LocalNodeId,
                TargetId = EleMedKitCabinetNodeName,
            };
            return Send(data);
        }
    }
}
