using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Calls;
using Hotline.CallCenter.Configs;
using Hotline.CallCenter.Ivrs;
using Hotline.DI;
using Hotline.Realtimes;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Notifications.NewRockCallCenter;
using MediatR;
using Microsoft.Extensions.Options;
using NewRock.Sdk;

namespace Hotline.Application.Handlers.CallCenter.CallState
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class DtmfNotificationHandler : NewRockBaseHandler, INotificationHandler<DtmfNotification>
    {
        private readonly IIvrDomainService _ivrDomainService;
        private readonly ICallRepository _callRepository;
        private readonly ICallDetailRepository _callDetailRepository;


        public DtmfNotificationHandler(IIvrDomainService ivrDomainService, ICallDetailRepository callDetailRepository, ICallRepository callRepository, INewRockClient newRockClient, IOptionsSnapshot<CallCenterConfiguration> callcenterOptions,ICallCacheManager callCacheManager,IRealtimeService realtimeService) :base(newRockClient, callcenterOptions,callRepository, callCacheManager,realtimeService)
        {
            _ivrDomainService = ivrDomainService;
            _callDetailRepository = callDetailRepository;
            _callRepository = callRepository;
        }

        public async Task Handle(DtmfNotification notification, CancellationToken cancellationToken)
        {
            bool isvis = true;
            string menuId = string.Empty;
            string info = string.Empty;
            var model = await _callRepository.GetAsync(
                x => x.ConversationId == notification.Visitor.Id && x.FromNo == notification.Visitor.From &&
                     x.ToNo == notification.Visitor.To,true,x=>x.CreationTime, cancellationToken);
            if (model == null)
            {
                model = await _callRepository.GetAsync(
                    x => x.ConversationId == notification.Outer.Id && x.FromNo == notification.Outer.From &&
                         x.ToNo == notification.Outer.To,true,x=>x.CreationTime, cancellationToken);
                isvis = false;
            }
            if (model != null)
            {
                var detail = new CallDetail()
                {
                    CallId = model.Id,
                    CallStatus = ECallStatus.Dtmf,
                    EventName = notification.Attribute,
                };
                if (isvis)
                {
                    detail.OMCallId = notification.Visitor.CallId;
                    detail.ConversationId = notification.Visitor.Id;
                    detail.FromNo = notification.Visitor.From;
                    detail.ToNo = notification.Visitor.To;
                    menuId = notification.Visitor.MenuId;
                    info = notification.Visitor.Info;
                }
                else
                {
                    detail.OMCallId = notification.Outer.CallId;
                    detail.ConversationId = notification.Outer.Id;
                    detail.FromNo = notification.Outer.From;
                    detail.ToNo = notification.Outer.To;
                    menuId = notification.Outer.MenuId;
                    info = notification.Outer.Info;
                }
                detail.Remark = info;
                await _callDetailRepository.AddAsync(detail, cancellationToken);

                #region 处理评价

                var callDetail = await _callDetailRepository.GetAsync(x => x.CallId == model.Id && x.EventName == "EVALUATE", cancellationToken);
                if (callDetail != null)
                {
                    callDetail.Remark = info;
                    await _callDetailRepository.UpdateAsync(callDetail, cancellationToken);
                }

                #endregion


                //调用事件处理
                var dtmfResult = await _ivrDomainService.GetDtmfAnswerAsync(menuId, info, cancellationToken);

                await base.HandlerIvr(dtmfResult, model, cancellationToken);
            }
        }
    }
}
