using System.Globalization;
using Microsoft.AspNetCore.Mvc;
using UWLecturePlan.Models;
using UWLib;
using Microsoft.EntityFrameworkCore;
using System;

namespace UWLecturePlan.Controllers
{
    public class LectureController : Controller
    {


        public IActionResult Index(LecturesViewModel model)
        {
            LectureContext db = new(@"lecture.db");

            model.Semester ??= GetSemester(DateTime.Now);

            var from = DateTime.Now;

            var date = DateTime.ParseExact(model.Date, "dd.MM.yyyy", CultureInfo.InvariantCulture);


            model.LectureEvents = [.. db.LectureEvents.Include(x=>x.Lecture)
                .Where(x => x.Lecture.Semester == model.Semester)
                .Where(x => x.Lecture.Branch == model.Branch || model.Branch == null)
                
                .Where(x => x.From.Date == date)];

            if (model.TypeFilter != null)
            {
                model.LectureEvents = model.LectureEvents
                    .Where(x => x.Lecture.Type == model.TypeFilter).ToList();
            }

            if (model.Location != null)
            {
                model.LectureEvents = model.LectureEvents
                    .Where(x => x.Location?.Contains(model.Location) ?? false).ToList();
            }

            model.Branches = [.. db.LectureEvents.Select(x => x.Lecture.Branch).Distinct().OrderBy(x => x)];

            model.Types = [.. db.LectureEvents.Select(x => x.Lecture.Type).Distinct().OrderBy(x => x)];

            model.Semesters = [.. db.LectureEvents.Select(x => x.Lecture.Semester).Distinct().OrderBy(x => x)];

            model.Days = db.LectureEvents
                .Where(x => x.Lecture.Semester == model.Semester)
                .Where(x => x.Lecture.Branch == model.Branch || model.Branch == null)
                .GroupBy(x => x.From.Date).ToDictionary(x => x.Key, x => x.Count());


            return View(model);
        }

        // This presumes that weeks start with Monday.
        // Week 1 is the 1st week of the year with a Thursday in it.
        public static int GetIso8601WeekOfYear(DateTime time)
        {
            // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll 
            // be the same week# as whatever Thursday, Friday or Saturday are,
            // and we always get those right
            DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
            if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
            {
                time = time.AddDays(3);
            }

            // Return the week of our adjusted day
            return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        }

        public static DateTime FirstDateOfWeekISO8601(int year, int weekOfYear)
        {
            DateTime jan1 = new(year, 1, 1);
            int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;

            // Use first Thursday in January to get first week of the year as
            // it will never be in Week 52/53
            DateTime firstThursday = jan1.AddDays(daysOffset);
            var cal = CultureInfo.CurrentCulture.Calendar;
            int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

            var weekNum = weekOfYear;
            // As we're adding days to a date in Week 1,
            // we need to subtract 1 in order to get the right date for week #1
            if (firstWeek == 1)
            {
                weekNum -= 1;
            }

            // Using the first Thursday as starting week ensures that we are starting in the right year
            // then we add number of weeks multiplied with days
            var result = firstThursday.AddDays(weekNum * 7);

            // Subtract 3 days from Thursday to get Monday, which is the first weekday in ISO8601
            return result.AddDays(-3);
        }


        public static string GetSemester(DateTime time)
        {
            return time.Year + (time.Month <= 6 ? "S" : "W");
        }
    }
}
