using Exam.Infrastructure.Data.Interface;
using Exam.Infrastructure.Enums;
using Hotline.Share.Exams.Interface;
using XF.Domain.Entities;

namespace Hotline.Application.Exam.Extensions
{
    public static class OperationStatusExtensions
    {
        public static void ResolveOperationStatus<T>(this List<T> actionRequests)
             where T : IOperationStatus, IAddRequest, new()
        {
            actionRequests.Where(x=>x.OperationStatus == EEOperationStatus.Default).ToList().ForEach(actionRequest =>
            {
                actionRequest.OperationStatus = EEOperationStatus.Add;
            });
        }

        public static void ResolveOperationStatus<T,TEntity>(this List<T> actionRequests,List<TEntity> currents) 
            where T:IOperationStatus,IActionRequest,new()            
            where TEntity:IEntity<string>
        {
            actionRequests.Where(x => x.OperationStatus == EEOperationStatus.Default).ToList().ForEach(actionRequest =>
            {
                if (currents.Any(x => x.Id == (actionRequest).Id))
                {
                    actionRequest.OperationStatus = EEOperationStatus.Update;
                }
            });

            currents.ForEach(item =>
            {
                if(!actionRequests.Any(x=> x.Id == item.Id))
                {
                    actionRequests.Add(new T()
                    {
                        Id = item.Id,
                        OperationStatus = EEOperationStatus.Delete
                    });
                }
            });
        }
    }
}
