using Hotline.Realtimes;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Article
{
    public class CircularRecordDomainService : ICircularRecordDomainService, IScopeDependency
    {
        private readonly IRepository<CircularRecord> _circularRecordRepository;
        private readonly IRealtimeService _realtimeService;
        public CircularRecordDomainService(IRepository<CircularRecord> circularRecordRepository, IRealtimeService realtimeService)
        {
            _circularRecordRepository = circularRecordRepository;
            _realtimeService = realtimeService;
        }
        public async Task RecordUserHandle(List<string> userIds, bool isAdd,int count = 1, CancellationToken cancellationToken= default)
        {
            foreach (var item in userIds)
            {
                var userRecord = await _circularRecordRepository.GetAsync(x => x.UserId == item);
                if (isAdd)
                {
                    if (userRecord != null)
                    {
                        userRecord.RecordCount = userRecord.RecordCount + count;
                        await _circularRecordRepository.UpdateAsync(userRecord);
                    }
                    else
                    {
                        userRecord = new CircularRecord();
                        userRecord.UserId = item;
                        userRecord.CircularType = Share.Enums.Article.ECircularType.Person;
                        userRecord.RecordCount = 1;
                        await _circularRecordRepository.AddAsync(userRecord);
                    }
                }
                else
                {
                    if (userRecord != null)
                    {
                        if (userRecord.RecordCount > 0)
                        {
                            userRecord.RecordCount = userRecord.RecordCount - count;
                        }
                        else
                        {
                            userRecord.RecordCount = 0;
                        }
                        await _circularRecordRepository.UpdateAsync(userRecord);
                    }
                }
                try
                {
                    await _realtimeService.CircularRecoordAsync(userRecord.Id, cancellationToken);
                }
                catch{}
            }
            
        }

        public async Task RecordOrgHandle(List<string> orgIds, bool isAdd,int count =1, CancellationToken cancellationToken = default)
        {
            foreach (var item in orgIds)
            {
                var orgRecord = await _circularRecordRepository.GetAsync(x => x.OrgId == item);
                if (isAdd)
                {
                    if (orgRecord != null)
                    {
                        if (orgRecord.RecordCount > 0)
                        {
                            orgRecord.RecordCount = orgRecord.RecordCount + count;
                        }
                        await _circularRecordRepository.UpdateAsync(orgRecord);
                    }
                    else
                    {
                        orgRecord = new CircularRecord();
                        orgRecord.OrgId = item;
                        orgRecord.CircularType = Share.Enums.Article.ECircularType.Org;
                        orgRecord.RecordCount = 1;
                        await _circularRecordRepository.AddAsync(orgRecord);
                    }
                }
                else
                {
                    if (orgRecord != null)
                    {
                        if (orgRecord.RecordCount > 0)
                        {
                            orgRecord.RecordCount = orgRecord.RecordCount - count;
                        }
                        else
                        {
                            orgRecord.RecordCount = 0;
                        }
                        await _circularRecordRepository.UpdateAsync(orgRecord);
                    }
                }
                try
                {
                    await _realtimeService.CircularRecoordAsync(orgRecord.Id, cancellationToken);
                }
                catch{}
            }
        }
    }
}
