using PARR.API.Contracts.V1.Requests.Queries;
using PARR.API.Services.Interfaces;
using PARR.BLL.Services.Interfaces;
using PARR.DAL.Models;

namespace PARR.API.Services.Implementations
{
    internal class WorkLoadService : IWorkLoadService
    {
        private readonly ICalendarService calendarService;

        public WorkLoadService(ICalendarService calendarService)
        {
            this.calendarService = calendarService;
        }

        private DateTime GetStartDate(int timeZoneOffsetHours)
        {
            //(так уже не делаем) начинаем не с сегодняшнего дня, а с завтра, так как на сегодня уже некоторые работы были проведены и их дата изменилась
            //return DateTimeOffset.UtcNow.AddHours(timeZoneOffsetHours).Date.AddDays(1);

            var dateWithTimeZoneOffset = calendarService.GetDateWithTimeZoneOffset(DateTimeOffset.UtcNow, timeZoneOffsetHours);
            //var dateWithAddDays = dateWithTimeZoneOffset.DateTime.AddDays(1);
            var dateWithAddDays = dateWithTimeZoneOffset.DateTime.AddDays(0);

            //var bbb = dateWithTimeZoneOffset.DateTime.AddDays(1);

            //return calendarService.GetDateWithTimeZoneOffset(DateTimeOffset.UtcNow, timeZoneOffsetHours).UtcDateTime.AddDays(1);
            return dateWithAddDays;
        }


        private DateTime GetEndDate(bool? isMonth, int timeZoneOffsetHours)
        {
            var startDate = GetStartDate(timeZoneOffsetHours);

            return isMonth == true ? startDate.AddDays(30) : startDate.AddDays(6);
        }


        public List<DateOnly> GetDatesForPeriod(WorkloadBaseQuery requestQuery)
        {
            var startDate = GetStartDate(requestQuery.TimeZoneOffsetHours);
            var endDate = GetEndDate(requestQuery.IsMonth, requestQuery.TimeZoneOffsetHours);

            //var daysList = new List<DateTimeOffset>();

            //daysList.Add(new DateTimeOffset(startDate, new TimeSpan(0)));

            //while (daysList.Last() < endDate)
            //    daysList.Add(daysList.Last().AddDays(1));

            //return daysList.OrderBy(t => t).ToList();

            //var aaa = DateOnly.FromDateTime(startDate);

            return calendarService.GetDatesForPeriod(DateOnly.FromDateTime(startDate), DateOnly.FromDateTime(endDate));
        }


        public IQueryable<Template> FilterTemplatesQuery(IQueryable<Template> query, WorkloadBaseQuery requestQuery)
        {
            var startDate = GetStartDate(requestQuery.TimeZoneOffsetHours);
            var endDate = GetEndDate(requestQuery.IsMonth, requestQuery.TimeZoneOffsetHours);

            //query = query.Where(t =>
            //            t.NextRun.DateTime.Date >= startDate.Date && t.NextRun.DateTime.Date <= endDate.Date
            //            );

            query = query.Where(t =>
                      t.NextRun.DateTime.AddHours(requestQuery.TimeZoneOffsetHours).Date >= startDate.Date && t.NextRun.DateTime.AddHours(requestQuery.TimeZoneOffsetHours).Date <= endDate.Date
                      );

            if (requestQuery.IsActiveTemplate.HasValue)
                query = query.Where(t => t.IsActiveTemplate == requestQuery.IsActiveTemplate.Value);

            if (requestQuery.IsActiveSchedule.HasValue)
                query = query.Where(t => t.IsActiveSchedule == requestQuery.IsActiveSchedule.Value);

            return query;
        }
    }
}
