using Hotline.Ai.Visit;
using Hotline.DI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Orders
{
    [Injection(AppScopes = EAppScope.YiBin)]
    public class AiVisitDomainService : IAiVisitDomainService, IScopeDependency
    {
        private readonly IAiVisitService _aiVisitService;
        private readonly IRepository<AiOrderVisit> _aiOrderVisitRepository;
        private readonly IRepository<AiOrderVisitDetail> _aiOrderVisitDetailRepository;
        private readonly IRepository<OrderVisit> _orderVisitRepository;

        public AiVisitDomainService(IAiVisitService aiVisitService,IRepository<AiOrderVisit> aiOrderVisitRepository, IRepository<AiOrderVisitDetail> aiOrderVisitDetailRepository, IRepository<OrderVisit> orderVisitRepository)
        {
            _aiVisitService = aiVisitService;
            _aiOrderVisitRepository = aiOrderVisitRepository;
            _aiOrderVisitDetailRepository = aiOrderVisitDetailRepository;
            _orderVisitRepository = orderVisitRepository;
        }


        public async Task OrderVisitStatusService(CancellationToken cancellationToken)
        {
            try
            {
                var aivisitList = await _aiOrderVisitRepository.Queryable()
                .Includes(x => x.AiOrderVisitDetails, d => d.OrderVisit).Where(x => x.TaskState == Share.Enums.Ai.EAiOrderVisitTaskState.InProgress).ToListAsync();
                if (aivisitList != null)
                {
                    foreach (var aivisit in aivisitList)
                    {
                        foreach (var aivisitDetail in aivisit.AiOrderVisitDetails)
                        {
                            if (!string.IsNullOrEmpty(aivisit.BatchUid) && !string.IsNullOrEmpty(aivisitDetail.TaskUid))
                            {
                                var res = await _aiVisitService.QueryAiVisitTask(aivisit.BatchUid, aivisitDetail.TaskUid, cancellationToken);
                                if (res != null && res.Status == 4)
                                {
                                    aivisitDetail.AiOrderVisitState = Share.Enums.Ai.EAiOrderVisitState.LoseEfficacy;
                                    await _aiOrderVisitDetailRepository.UpdateAsync(aivisitDetail, cancellationToken);
                                    aivisitDetail.OrderVisit.VisitState = Share.Enums.Order.EVisitState.WaitForVisit;
                                    aivisitDetail.OrderVisit.IsCanAiVisit = true;
                                    await _orderVisitRepository.UpdateAsync(aivisitDetail.OrderVisit, cancellationToken);
                                    aivisit.VisitedFailCount++;
                                    if ((aivisit.VisitedFailCount + aivisit.VisitedCount) == aivisit.HasVisitCount)
                                    {
                                        aivisit.TaskState = Share.Enums.Ai.EAiOrderVisitTaskState.Ended;
                                    }
                                    await _aiOrderVisitRepository.UpdateAsync(aivisit, cancellationToken);
                                }
                            }

                        }
                    }
                }

            }
            catch { }
        }
    }
}
