using Brizco.Domain.Entities.Tasks;
using Microsoft.Extensions.Logging;
using Quartz;
using Task = System.Threading.Tasks.Task;

namespace Brizco.Core.QuartzServices;

public class ShiftPlanNotificationScheduledJob : IJob
{
    private readonly ILogger<ShiftPlanNotificationScheduledJob> _logger;
    private readonly IRepositoryWrapper _repositoryWrapper;
    private readonly IMediator _mediator;

    public ShiftPlanNotificationScheduledJob(ILogger<ShiftPlanNotificationScheduledJob> logger,IRepositoryWrapper repositoryWrapper,IMediator mediator)
    {
        _logger = logger;
        _repositoryWrapper = repositoryWrapper;
        _mediator = mediator;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        try
        {
            var notifyType = (ShiftPlanNotifyType)int.Parse(context.JobDetail.Key.Name);
            var shiftPlanId = Guid.Parse(context.JobDetail.Key.Group);
            var shiftPlan = await _repositoryWrapper.SetRepository<ShiftPlan>().TableNoTracking
                .Where(f => f.Id == shiftPlanId)
                .Select(ShiftPlanMapper.ProjectToSDto)
                .FirstOrDefaultAsync();
            if (shiftPlan == null)
                throw new Exception("Shift plan not found");
            var shiftPlanUsers = await _repositoryWrapper.SetRepository<ShiftPlanUser>()
                .TableNoTracking
                .Where(f => f.ShiftPlanId == shiftPlanId)
                .Select(ShiftPlanUserMapper.ProjectToSDto)
                .ToListAsync();
            var superVisorId = shiftPlan.SupervisorId;
            switch (notifyType)
            {
                case ShiftPlanNotifyType.None:
                    return;
                    break;
                case ShiftPlanNotifyType.BeforeEndShift30Min:
                    var activities = await _repositoryWrapper.SetRepository<Activity>().TableNoTracking
                        .Where(a => a.ShiftPlanId == shiftPlanId).ToListAsync();
                    foreach (var shiftPlanUser in shiftPlanUsers)
                    {
                        var unDoneCount = activities.Count(a => a.UserId == shiftPlanUser.UserId && a.IsDone == false);
                        if(unDoneCount == 0)
                            continue;
                        string message = $"نیم ساعت مونده تا شیفت تموم شه و {unDoneCount} عدد از تست هات مونده، حالا چه خاکی به سر کنیم!😱";
                        await _mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
                    }
                    break;
                case ShiftPlanNotifyType.EndOfShift:
                    var activitiesEndShift = await _repositoryWrapper.SetRepository<Activity>().TableNoTracking
                        .Where(a => a.ShiftPlanId == shiftPlanId).ToListAsync();
                    foreach (var shiftPlanUser in shiftPlanUsers)
                    {
                        var unDoneCount = activitiesEndShift.Count(a => a.UserId == shiftPlanUser.UserId && a.IsDone == false);
                        if (unDoneCount == 0)
                            continue;
                        string message = $"{shiftPlanUser.UserFullName} {shiftPlan.ShiftTitle} {shiftPlan.PlanFor.ToPersianDateTime().ToLongDateTimeString()} تموم شده است و {unDoneCount} - عدد از تسک های شما کاری روشون انجام نشده ، خطر سوپروایزر در کمین است";
                        await _mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
                    }
                    string superVisorAfterShiftMessage = $"{shiftPlan.SupervisorFullName} محترم {shiftPlan.ShiftTitle} تموم شد. برو به کار بچه ها نمره بده و مو رو از ماست بکش و کامنت یادت نره";
                    await _mediator.Send(new CreateNotificationCommand(superVisorAfterShiftMessage, shiftPlan.SupervisorId, shiftPlan.ComplexId));
                    break;
                case ShiftPlanNotifyType.AfterStartShift2Hour:
                    var activities2AfterStartShift = await _repositoryWrapper.SetRepository<Activity>().TableNoTracking
                        .Where(a => a.ShiftPlanId == shiftPlanId).ToListAsync();
                    foreach (var shiftPlanUser in shiftPlanUsers)
                    {
                        var unDoneCount = activities2AfterStartShift.Count(a => a.UserId == shiftPlanUser.UserId && a.IsDone == false);
                        if (unDoneCount == 0)
                            continue;
                        string message = $"{shiftPlanUser.UserFullName} دوساعت از {shiftPlan.ShiftTitle} گذشته ، اون انگشت و بزن روی تیک تسک ها که وقت طلاس مشتیییییی ";
                        await _mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
                    }
                    break;
                default:
                    return;
            }
        }
        catch (Exception e)
        {
            _logger.LogError(e.Message);
        }
    }
}