namespace SHH.CameraDashboard
{
    // 简单的上下文定义
    public class ProtocolContext
    {
        public string Protocol { get; set; }
        public byte[] Data { get; set; }
        public bool IsBlocked { get; set; } = false;
        public ProtocolContext(string p, byte[] d) { Protocol = p; Data = d; }
    }

    public interface IProtocolInterceptor
    {
        Task OnSendingAsync(ProtocolContext context);
        Task OnReceivedAsync(ProtocolContext context);
    }

    public class InterceptorPipeline
    {
        // 因为 Dashboard 可能没有复杂的 DI，这里支持手动添加列表
        private readonly List<IProtocolInterceptor> _interceptors = new List<IProtocolInterceptor>();

        public void Add(IProtocolInterceptor interceptor) => _interceptors.Add(interceptor);

        public async Task<ProtocolContext?> ExecuteSendAsync(string protocol, byte[] data)
        {
            var ctx = new ProtocolContext(protocol, data);
            foreach (var i in _interceptors)
            {
                await i.OnSendingAsync(ctx);
                if (ctx.IsBlocked) return null;
            }
            return ctx;
        }

        public async Task<ProtocolContext?> ExecuteReceiveAsync(string protocol, byte[] data)
        {
            var ctx = new ProtocolContext(protocol, data);
            foreach (var i in _interceptors)
            {
                await i.OnReceivedAsync(ctx);
                if (ctx.IsBlocked) return null;
            }
            return ctx;
        }
    }
}