using JiShe.CollectBus.Protocol.Interfaces;
using Microsoft.Extensions.DependencyInjection;

namespace JiShe.CollectBus.Protocol
{

    public class AnalysisStrategyContext(IServiceProvider provider)
    {
        private readonly IServiceProvider _provider = provider;

        /// <summary>
        /// 执行策略
        /// </summary>
        /// <typeparam name="TInput"></typeparam>
        /// <param name="type"></param>
        /// <param name="input"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public async Task<bool> ExecuteAsync<TInput>(string type, TInput input,Action<dynamic>? result=null)
        {
            var factory = _provider.GetRequiredService<Func<string, Type, object>>();
            var strategy = (IAnalysisStrategy<TInput>)factory(type, typeof(TInput));
            return await strategy.ExecuteAsync(input, result);
        }

    }
}
