#region << 版 本 注 释 >>

/*--------------------------------------------------------------------
* 版权所有 (c) 2026 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：Mr.Wen's MacBook Pro
* 命名空间：Sln.IntelliBelt.Business.base
* 唯一标识：6DE5D45A-D61B-4F49-ABE8-C98677E1CB69
*
* 创建者：WenJY
* 电子邮箱：
* 创建时间：2026-04-24 16:09:42
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using Newtonsoft.Json.Linq;
using Sln.IntelliBelt.Common;
using Sln.IntelliBelt.Config;
using Sln.IntelliBelt.Serilog;
using Sln.IntelliBelt.Socket.Adapter;
using Sln.IntelliBelt.WebSocket;
using TouchSocket.Core;
using TouchSocket.Sockets;

namespace Sln.IntelliBelt.Business.@base;

public abstract class BaseBusiness
{
    public SerilogHelper _logger;

    public AppConfig _appConfig;

    public StringChange _stringChange;
    
    public WebSocketHelper _webSocket;
    
    public BaseBusiness(SerilogHelper logger,AppConfig appConfig,StringChange stringChange, WebSocketHelper webSocket)
    {
        _logger = logger;
        _appConfig = appConfig;
        _stringChange = stringChange;
        _webSocket = webSocket;
    }
    
    /// <summary>
    /// 指令解析方法
    /// </summary>
    /// <param name="client"></param>
    /// <param name="requestInfo"></param>
    public abstract void BufferAnalysis(TcpSessionClient client,BufferRequestInfo requestInfo,long bodyLength);

    /// <summary>
    /// 应答响应
    /// </summary>
    /// <param name="client"></param>
    /// <param name="requestInfo"></param>
    public abstract void ResponseHandle(TcpSessionClient client,BufferRequestInfo requestInfo);
    
    /// <summary>
    /// 封装回复指令
    /// </summary>
    /// <param name="SendMessagePackInfo"></param>
    /// <param name="buffer"></param>
    public byte[] GetMessagePack(ref ResponsePack packInfo)
    {
        int bufferLength = packInfo.contentLen + 10 ;
        int index = 0;
        byte[] buffer = new byte[bufferLength];
        buffer[index++] = packInfo.header ;
        buffer[index++] = packInfo.terminalType ;
        Array.Copy(packInfo.terminalAddr, 0, buffer, index, 2); 
        index += 2;
        buffer[index++] = packInfo.serialNumber ;
        buffer[index++] = packInfo.identCode ;
        buffer[index++] = packInfo.objType ;
        buffer[index++] = packInfo.contentLen ;
        Array.Copy(packInfo.content, 0, buffer, index, packInfo.contentLen);
        index += packInfo.contentLen;
        Byte[] checkBitBytes = new Byte[bufferLength-2];
        Array.Copy(buffer, 0, checkBitBytes, 0, checkBitBytes.Length);
        buffer[index++] = _stringChange.CalculateChecksum(checkBitBytes);
        buffer[index++] = packInfo.tail ;
        return buffer;
        
    }

    public void GetJsonStr(string clientId, BufferRequestInfo requestInfo,out string jsonStr)
    {
        
        // {
        //     "terminalAddr": "0001",    //终端地址
        //     "identCode":30  //功能标识码
        //     "objType": 0,  //对象类型：0-终端；1-1#读写器；2-2#读写器
        //     "content": "123456789ABC",  //读写器指令
        //     "clientId": "01-00-00-00" //客户端Id（终端链接后的 Id）
        // }
        
        JObject jObj = new JObject();
        jObj["clientId"] = clientId;
        jObj["terminalAddr"] = requestInfo.terminalAddr;
        jObj["identCode"] = requestInfo.identCode;
        jObj["objType"] = requestInfo.objType;
        jObj["contentLen"] = requestInfo.contentLen;
        jObj["content"] = requestInfo.content;
        jsonStr = jObj.ToString();
    }

    /// <summary>
    /// 对象类别
    /// </summary>
    /// <param name="objTypeByte"></param>
    /// <param name="objTypeStr"></param>
    public void GetObjectTypeStr(int objTypeByte,out string objTypeStr)
    {
        if (objTypeByte == 0x00)
        {
            objTypeStr = "终端设备";
        }
        else
        {
            objTypeStr = $"{(int)objTypeByte}#读写器";
        }
    }
}