using Hotline.Caching.Interfaces;
using Hotline.CallCenter.BlackLists;
using Hotline.CallCenter.Calls;
using Hotline.CallCenter.Configs;
using Hotline.DI;
using Hotline.Settings;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Notifications.NewRockCallCenter;
using Hotline.Tools;
using MediatR;
using Microsoft.Extensions.Options;
using NewRock.Sdk;
using NewRock.Sdk.Accept.Request;
using NewRock.Sdk.Control.Request;

namespace Hotline.Application.Handlers.CallCenter.FlowControl
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class InviteNotificationHandler : INotificationHandler<InviteNotification>
    {
        private readonly ICallRepository _callRepository;
        private readonly ICallDetailRepository _callDetailRepository;
        private readonly IUserCacheManager _userCacheManager;
        private readonly IBlacklistDomainService _blacklistDomainService;
        private readonly INewRockClient _newRockClient;
        private readonly IOptionsSnapshot<CallCenterConfiguration> _options;
        private readonly IOptionsSnapshot<WorkTimeSettings> _workTimeOptions;
        public InviteNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager, IBlacklistDomainService blacklistDomainService, INewRockClient newRockClient, IOptionsSnapshot<CallCenterConfiguration> options, IOptionsSnapshot<WorkTimeSettings> workTimeOptions)
        {
            _callRepository = callRepository;
            _callDetailRepository = callDetailRepository;
            _userCacheManager = userCacheManager;
            _blacklistDomainService = blacklistDomainService;
            _newRockClient = newRockClient;
            _options = options;
            _workTimeOptions = workTimeOptions;
        }

        public bool IsInWrokTime()
        {
            return false;
        }

        public async Task Handle(InviteNotification notification, CancellationToken cancellationToken)
        {
            //验证来电是否在黑名单中
            bool isblacklist = _blacklistDomainService.IsInBlacklist(notification.Visitor.From);
            //获取星期数
            //string weeekNum = WeekTool.GetWeekNum();

            //TODO 去掉绑定用户信息
            //var workModel = _userCacheManager.GetWorkByTel(notification.Visitor.To);
            var isp = PhoneIspTool.GetPhoneIsp(notification.Visitor.From);
            var model = new Call()
            {
                CallStatus = ECallStatus.PreIncoming,
                CallDirection = ECallDirection.In,
                CallType = ECallType.VisitorCallIn,
                ConversationId = notification.Visitor.Id,
                FromNo = notification.Visitor.From,
                ToNo = notification.Visitor.To,
                Trunk = notification.TrunkId,
                //UserId = workModel.UserId,
                //UserName = workModel.UserName,
                PhoneIsp = isp,
            };
            if (isblacklist)
                model.RingOffType = ERingOffType.BlackList;

            model.Modified();
            var callid = await _callRepository.AddAsync(model, cancellationToken);
            var detail = new CallDetail()
            {
                CallId = callid,
                CallStatus = ECallStatus.PreIncoming,
                OMCallId = notification.Visitor.CallId,
                ConversationId = notification.Visitor.Id,
                EventName = notification.Attribute,
                FromNo = notification.Visitor.From,
                ToNo = notification.Visitor.To,
            };
            await _callDetailRepository.AddAsync(detail, cancellationToken);

            //如果是在黑名单中直接挂断
            if (isblacklist)
            {
                await _newRockClient.ClearCall(new ClearCallRequest()
                {
                    Attribute = "Clear",
                    Visitor = new ClearCallVisitor()
                    {
                        Id = notification.Visitor.Id
                    }
                }, _options.Value.NewRock.ReceiveKey, _options.Value.NewRock.Expired, cancellationToken);
            }
            else
            {
                await _newRockClient.AcceptVisitor(
                    new AcceptVisitorRequest()
                    { Attribute = "Accept", Visitor = new AcceptVisitorModel() { Id = notification.Visitor.Id } },
                    _options.Value.NewRock.ReceiveKey, _options.Value.NewRock.Expired, cancellationToken);
            }
        }
    }
}
