using JiShe.CollectBus.Protocol.Dto;
using JiShe.CollectBus.Protocol.Interfaces;
using JiShe.CollectBus.Protocol3761;
using Microsoft.Extensions.Logging;

namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0AH
{
    /// <summary>
    /// 5.5.1.3.55 F68：定时上报 2 类数据任务启动/停止设置
    /// </summary>
    public class AFN10_F68_Analysis : IAnalysisStrategy<TB3761>
    {
        private readonly ILogger<AFN10_F68_Analysis> _logger;

        public AFN10_F68_Analysis(ILogger<AFN10_F68_Analysis> logger)
        {
            _logger = logger;
        }

        public Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
        {
            try
            {
                ArgumentNullException.ThrowIfNull(input);
                ArgumentNullException.ThrowIfNull(input.UnitData.HexMessageList);
                UnitDataAnalysis<bool> dto = new UnitDataAnalysis<bool>
                {
                    Code = input.A.Code!,
                    AFN = input.AFN_FC.AFN,
                    Fn = input.DT.Fn,
                    Pn = input.DA.Pn,
                    Data = input.UnitData.HexMessageList[4].Equals("55"),
                    HexMessage = input.BaseHexMessage.HexMessageString,
                    MessageId = input.MessageId,
                    ReceivedTime = input.ReceivedTime
                };
                result?.Invoke(dto);
                return Task.FromResult(true);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"0A_68解析失败:{input.A?.Code}-{input.DT?.Fn ?? 0}-{input?.BaseHexMessage?.HexMessageString},{ex.Message}");
            }
            return Task.FromResult(false);
        }
    }
}
