using GatherService.WattMeter.AnalysisData.AFN_10H;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto;
using JiShe.CollectBus.Protocol.Dto;
using JiShe.CollectBus.Protocol.Interfaces;
using JiShe.CollectBus.Protocol3761;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;

namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_10H
{
    /// <summary>
    /// 5.16 数据转发（AFN=10H）
    /// </summary>
    public class AFN16_F1_Analysis : IAnalysisStrategy<TB3761>
    {
        private readonly ILogger<AFN16_F1_Analysis> _logger;
        private readonly AnalysisStrategyContext _analysisStrategyContext;

        public AFN16_F1_Analysis(ILogger<AFN16_F1_Analysis> logger, AnalysisStrategyContext analysisStrategyContext)
        {
            _logger = logger;
            _analysisStrategyContext = analysisStrategyContext;
        }

        public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
        {
            try
            {
                ArgumentNullException.ThrowIfNull(input);
                ArgumentNullException.ThrowIfNull(input.BaseHexMessage.HexMessageList);
                ArgumentNullException.ThrowIfNull(input.UnitData.HexMessageString);
                // TODO: 待定，等确认如何匹配规则
                string value = input.UnitData.HexMessageString;
                if (value.Contains(F10TranspondMatch.ReadNormal))
                {
                    if (value.Contains(F10TranspondMatch.PowerHz))//电网频率
                        await _analysisStrategyContext.ExecuteAsync<TB3761>(nameof(AFN16_F97_Analysis), input, dto =>
                        {
                            result?.Invoke(dto);
                        });
                    //else if (value.Contains(F10TranspondMatch.ReadData))//读取电表地址
                    //    result = "AFN16_F105_Analysis";
                    else
                    {
                        _logger.LogWarning($"未能找到透明传发解析方式：{input.BaseHexMessage.HexMessageString}");
                    }
                }
               
                return await Task.FromResult(true);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"10_101解析失败:{input.A?.Code}-{input.DT?.Fn ?? 0}-{input?.BaseHexMessage?.HexMessageString},{ex.Message}");

            }
            return await Task.FromResult(false);
        }
    }
}
