using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Calls;
using Hotline.DI;
using Hotline.Realtimes;
using Hotline.Share.Dtos.Realtime;
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 AlertExtToOuterNotificationHandler:INotificationHandler<AlertExtToOuterNotification>
    {
        private readonly ICallRepository _callRepository;
        private readonly ICallDetailRepository _callDetailRepository;
        private readonly IUserCacheManager _userCacheManager;
        private readonly IRealtimeService _realtimeService;

        public AlertExtToOuterNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository,IUserCacheManager userCacheManager,IRealtimeService realtimeService)
        {
            _callRepository = callRepository;
            _callDetailRepository = callDetailRepository;
            _userCacheManager = userCacheManager;
            _realtimeService = realtimeService;
        }

        public async Task Handle(AlertExtToOuterNotification notification, CancellationToken cancellationToken)
        {
            string telNo = notification.Outer.From != "" ? notification.Outer.From : notification.TelNo;
            if (!string.IsNullOrEmpty(telNo))
            {
                var model =await _callRepository.GetAsync(x => x.ConversationId==notification.Outer.Id && x.ToNo==notification.Outer.To && x.Trunk==notification.Outer.Trunk && x.CreationTime>=DateTime.Now.AddHours(-2),true,x=>x.CreationTime, cancellationToken);
                var workModel = _userCacheManager.GetWorkByTel(telNo);
                //如果存在
                if (model!=null)
                {
                    model.CallStatus = ECallStatus.Alert;
                    await _callRepository.UpdateAsync(model, cancellationToken);
                    var detail = new CallDetail()
                    {
                        CallId = model.Id,
                        CallStatus = ECallStatus.Alert,
                        OMCallId = notification.Outer.CallId,
                        ConversationId = notification.Outer.Id,
                        EventName = notification.Attribute,
                        FromNo = notification.Outer.From,
                        ToNo = notification.Outer.To
                    };
                    await _callDetailRepository.AddAsync(detail, cancellationToken);

                    await _realtimeService.AlertAsync(workModel.UserId, new AlertDto() { Id = model.Id, From = telNo, To = notification.Outer.To, CallType = ECallType.ExtToOuter, ConversationId = notification.Outer.Id }, 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 = telNo,
                        ToNo = notification.Outer.To,
                        Trunk = notification.Outer.Trunk,
                        UserId = workModel?.UserId,
                        UserName = workModel?.UserName,
                        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 = "ALERT",
                        FromNo = telNo,
                        ToNo = notification.Outer.To,
                    };
                    await _callDetailRepository.AddAsync(detail, cancellationToken);

                    await _realtimeService.AlertAsync(workModel.UserId, new AlertDto() { Id = callId, From = telNo, To = notification.Outer.To, CallType = ECallType.ExtToOuter, ConversationId = notification.Outer.Id }, cancellationToken);
                }
            }

        }
    }
}
