using System;
using System.Linq;

namespace TaskIt.Web.Services
{
    public static class ServiceUtils
    {
        public static IQueryable<Task> GetCompleteTasksQuery(IQueryable<Task> query, bool showCompleted, bool showDue)
        {
            if (showDue)
            {
                query = query.Where(t => t.DueDate != null);
            }
            return showCompleted ? query.Where(t => t.CompletionDate != null).OrderBy(t => t.CompletionDate) : query.Where(t => t.CompletionDate == null).OrderBy(t => t.DueDate);
        }

        public static DateTime GetStartOfCurrentWeek()
        {
            var daysToSubtract = (int)DateTime.Now.DayOfWeek;
            var dt = DateTime.Now.Subtract(TimeSpan.FromDays(daysToSubtract));
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfCurrentWeek()
        {
            var dt = GetStartOfCurrentWeek().AddDays(6);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        } 

    }
}
