using Hotline.Early;
using Hotline.Orders.Notifications;
using Hotline.Share.Tools;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Repository;

namespace Hotline.Application.OrderApp.Handlers.OrderHandler
{
    public class OrderEarlyWarningNotifyHandler : INotificationHandler<OrderEarlyWarningNotify>
    {
        private readonly IRepository<EarlyWarningOrder> _orderEarlyWarningRepository;
        private readonly IRepository<EarlyWarningOrderDetail> _orderEarlyWarningDetailRepository;

        public OrderEarlyWarningNotifyHandler(IRepository<EarlyWarningOrder> orderEarlyWarningRepository, IRepository<EarlyWarningOrderDetail> orderEarlyWarningDetailRepository)
        {
            _orderEarlyWarningRepository = orderEarlyWarningRepository;
            _orderEarlyWarningDetailRepository = orderEarlyWarningDetailRepository;
        }


        public async Task Handle(OrderEarlyWarningNotify notification, CancellationToken cancellationToken)
        {
            var settiingList = notification.settingList;
            var order = notification.order;
            var now = DateTime.Now;
            foreach (var item in settiingList)
            {
                var model = new EarlyWarningOrderDetail()
                {
                    OrderId = order.Id,
                    OrderContent = order.Content,
                    OrderTitle = order.Title,
                    Sensitive = order.Sensitive,
                    OrderPushStatus = Share.Enums.Early.EOrderPushStatus.NoPush,
                };
                var orderEarly = await _orderEarlyWarningRepository.Queryable().Where(x => x.StartTime <= now && x.EndTime >= now && x.AreaCode == order.AreaCode && x.EarlyWarningSettingId == item.Id).FirstAsync();
                if (orderEarly != null)
                {
                    model.EarlyWarningOrderId = orderEarly.Id;
                    var isHas = await _orderEarlyWarningDetailRepository.AnyAsync(x => x.EarlyWarningOrderId == orderEarly.Id && x.OrderId == order.Id);
                    if (!isHas)
                    {
                        await _orderEarlyWarningDetailRepository.AddAsync(model);
                        await _orderEarlyWarningRepository.Updateable().SetColumns(x => new EarlyWarningOrder()
                        {
                            LastEarlyTime = now,
                        }).Where(x => x.Id == orderEarly.Id).ExecuteCommandAsync(cancellationToken);
                    }
                }
                else
                {
                    orderEarly = new EarlyWarningOrder()
                    {
                        EarlyWarningSettingId = item.Id,
                        LastEarlyTime = now,
                        AreaCode = order.AreaCode,
                        Province = order.Province,
                        City = order.City,
                        County = order.County,
                        Town = order.Town,
                        Village = order.Village,
                        FullAddress = order.City + "-" + order.County + "-" + order.Town + "-" + order.Village
                    };
                    switch (item.Frequency)
                    {
                        case Share.Enums.Early.EFrequency.Day:
                            orderEarly.StartTime = now.Date;
                            orderEarly.EndTime = now.Date.AddDays(1).AddSeconds(-1);
                            break;
                        case Share.Enums.Early.EFrequency.Week:
                            var (startTime,endTime) = now.GetWeekRange();
                            orderEarly.StartTime = startTime;
                            orderEarly.EndTime = endTime;
                            break;
                        case Share.Enums.Early.EFrequency.Month:
                            (startTime, endTime) = now.GetMonthRange();
                            break;
                        case Share.Enums.Early.EFrequency.Year:
                            (startTime, endTime) = now.GetYearRange();
                            break;
                        default:
                            break;
                    }
                    await _orderEarlyWarningRepository.AddAsync(orderEarly,cancellationToken);
                    model.EarlyWarningOrderId = orderEarly.Id;
                    await _orderEarlyWarningDetailRepository.AddAsync(model);
                }
            }
        }
    }
}
