using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Calls;
using Hotline.DI;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Notifications.NewRockCallCenter;
using Hotline.Tools;
using MediatR;

namespace Hotline.Application.Handlers.CallCenter.CallState
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class RingExtToOuterNotificationHandler : INotificationHandler<RingExtToOuterNotification>
    {
        private readonly ICallRepository _callRepository;
        private readonly ICallDetailRepository _callDetailRepository;
        private readonly IUserCacheManager _userCacheManager;

        public RingExtToOuterNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager)
        {
            _callRepository = callRepository;
            _callDetailRepository = callDetailRepository;
            _userCacheManager = userCacheManager;
        }

        public async Task Handle(RingExtToOuterNotification notification, CancellationToken cancellationToken)
        {
            var model = await _callRepository.GetAsync(x =>
                x.FromNo == notification.TelNo && x.ToNo == notification.Outer.To && x.CreationTime >= DateTime.Now.AddHours(-2), true, x => x.CreationTime, cancellationToken);
            if (model == null)
            {
                model = await _callRepository.GetAsync(
                    x => x.ConversationId == notification.Outer.Id && x.ToNo == notification.Outer.To &&
                         x.Trunk == notification.Outer.Trunk, true, x => x.CreationTime, cancellationToken);
            }

            if (model != null)
            {
                var workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
                model.UserId = workModel.UserId;
                model.UserName = workModel.UserName;
                model.CallStatus = ECallStatus.Ring;
                await _callRepository.UpdateAsync(model, cancellationToken);
                var detail = new CallDetail()
                {
                    CallId = model.Id,
                    CallStatus = ECallStatus.Ring,
                    EventName = notification.Attribute,
                    ConversationId = notification.Outer.Id,
                    OMCallId = notification.Outer.CallId,
                    FromNo = notification.Outer.From,
                    ToNo = notification.Outer.To,
                };
                if (string.IsNullOrEmpty(detail.FromNo))
                    detail.FromNo = notification.TelNo;

                await _callDetailRepository.AddAsync(detail, cancellationToken);
            }
            else
            {
                var isp = PhoneIspTool.GetPhoneIsp(notification.Outer.To);
                var callModel = new Call()
                {
                    CallStatus = ECallStatus.Alert,
                    CallDirection = ECallDirection.Out,
                    CallType = ECallType.ExtToOuter,
                    ConversationId = notification.Outer.Id,
                    FromNo = notification.Outer.From,
                    ToNo = notification.Outer.To,
                    Trunk = notification.Outer.Trunk,
                    PhoneIsp = isp
                };
                callModel.Modified();
                var callId = await _callRepository.AddAsync(callModel, cancellationToken);
                //写入明细
                var detail = new CallDetail()
                {
                    CallId = callId,
                    CallStatus = ECallStatus.Alert,
                    ConversationId = notification.Outer.Id,
                    OMCallId = notification.Outer.CallId,
                    EventName = notification.Attribute,
                    FromNo = notification.Outer.From,
                    ToNo = notification.Outer.To,
                };
                await _callDetailRepository.AddAsync(detail, cancellationToken);
            }
        }
    }
}
