using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using IRaCIS.Core.Domain.BaseModel;
using IRaCIS.Core.Domain.Share;
using MassTransit;
using NPOI.SS.Formula.Functions;

public class CultureInfoFilter<T> (IRepository<EventStoreRecord> _eventStoreRecordRepository) : IFilter<ConsumeContext<T>> where T : DomainEvent
{

    public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
    {
        var cultureInfoName = context.Message.CultureInfoName;

        var eventId = context.Message.EventId;

        if (!string.IsNullOrEmpty(cultureInfoName))
        {
            CultureInfo.CurrentCulture = new CultureInfo(cultureInfoName);
            CultureInfo.CurrentUICulture = new CultureInfo(cultureInfoName);
        }


        await next.Send(context);

        await _eventStoreRecordRepository.BatchUpdateNoTrackingAsync(t => t.Id == eventId, u => new EventStoreRecord() { EventState= EventStateEnum.ConsumeSuccessed } );
    }

    public void Probe(ProbeContext context)
    {
        context.CreateFilterScope("CultureInfoFilter");
    }
}
