using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using PlanTempus.Application.Features.Localization.Services;

namespace PlanTempus.Application.Features.Employees.Components;

public class EmployeeWorkScheduleViewComponent : ViewComponent
{
    private readonly ILocalizationService _localization;
    private readonly IWebHostEnvironment _environment;

    public EmployeeWorkScheduleViewComponent(ILocalizationService localization, IWebHostEnvironment environment)
    {
        _localization = localization;
        _environment = environment;
    }

    public IViewComponentResult Invoke(string key)
    {
        var weekSchedule = LoadMockData();
        var days = GenerateDays(weekSchedule.StartDate);

        var model = new EmployeeWorkScheduleViewModel
        {
            WeekNumber = weekSchedule.WeekNumber,
            Year = weekSchedule.Year,
            StartDate = weekSchedule.StartDate,
            EndDate = weekSchedule.EndDate,
            ClosedDays = weekSchedule.ClosedDays,
            Employees = weekSchedule.Employees,
            Days = days,

            // Labels
            LabelWeek = _localization.Get("employees.detail.schedule.week"),
            LabelHours = _localization.Get("employees.detail.schedule.hours"),
            LabelEditShift = _localization.Get("employees.detail.schedule.editShift"),
            LabelStatus = _localization.Get("employees.detail.schedule.status"),
            LabelWork = _localization.Get("employees.detail.schedule.work"),
            LabelOff = _localization.Get("employees.detail.schedule.off"),
            LabelVacation = _localization.Get("employees.detail.schedule.vacation"),
            LabelSick = _localization.Get("employees.detail.schedule.sick"),
            LabelTimeRange = _localization.Get("employees.detail.schedule.timeRange"),
            LabelNote = _localization.Get("employees.detail.schedule.note"),
            LabelType = _localization.Get("employees.detail.schedule.type"),
            LabelSingle = _localization.Get("employees.detail.schedule.single"),
            LabelRepeat = _localization.Get("employees.detail.schedule.repeat"),
            LabelRepeatInterval = _localization.Get("employees.detail.schedule.repeatInterval"),
            LabelRepeatEnd = _localization.Get("employees.detail.schedule.repeatEnd"),
            LabelWeekday = _localization.Get("employees.detail.schedule.weekday"),
            LabelEdit = _localization.Get("common.edit"),
            LabelSave = _localization.Get("common.save"),
            LabelCancel = _localization.Get("common.cancel"),
            ButtonEdit = _localization.Get("employees.detail.schedule.buttonEdit"),
            ButtonDone = _localization.Get("employees.detail.schedule.buttonDone")
        };

        return View(model);
    }

    private WeekScheduleData LoadMockData()
    {
        var jsonPath = Path.Combine(_environment.ContentRootPath, "Features", "Employees", "Data", "workScheduleMock.json");
        var json = System.IO.File.ReadAllText(jsonPath);
        return JsonSerializer.Deserialize<WeekScheduleData>(json, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        })!;
    }

    private List<DayInfo> GenerateDays(string startDate)
    {
        var start = DateTime.Parse(startDate);
        var dayNames = new[] { "Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag" };
        var shortDayNames = new[] { "Søn", "Man", "Tir", "Ons", "Tor", "Fre", "Lør" };

        var days = new List<DayInfo>();
        for (int i = 0; i < 7; i++)
        {
            var date = start.AddDays(i);
            days.Add(new DayInfo
            {
                Date = date.ToString("yyyy-MM-dd"),
                DayName = dayNames[(int)date.DayOfWeek],
                ShortDayName = shortDayNames[(int)date.DayOfWeek],
                DisplayDate = date.ToString("dd/MM")
            });
        }
        return days;
    }
}

public class EmployeeWorkScheduleViewModel
{
    public int WeekNumber { get; init; }
    public int Year { get; init; }
    public required string StartDate { get; init; }
    public required string EndDate { get; init; }
    public required List<string> ClosedDays { get; init; }
    public required List<EmployeeScheduleData> Employees { get; init; }
    public required List<DayInfo> Days { get; init; }

    // Labels
    public required string LabelWeek { get; init; }
    public required string LabelHours { get; init; }
    public required string LabelEditShift { get; init; }
    public required string LabelStatus { get; init; }
    public required string LabelWork { get; init; }
    public required string LabelOff { get; init; }
    public required string LabelVacation { get; init; }
    public required string LabelSick { get; init; }
    public required string LabelTimeRange { get; init; }
    public required string LabelNote { get; init; }
    public required string LabelType { get; init; }
    public required string LabelSingle { get; init; }
    public required string LabelRepeat { get; init; }
    public required string LabelRepeatInterval { get; init; }
    public required string LabelRepeatEnd { get; init; }
    public required string LabelWeekday { get; init; }
    public required string LabelEdit { get; init; }
    public required string LabelSave { get; init; }
    public required string LabelCancel { get; init; }
    public required string ButtonEdit { get; init; }
    public required string ButtonDone { get; init; }
}

public class DayInfo
{
    public required string Date { get; init; }
    public required string DayName { get; init; }
    public required string ShortDayName { get; init; }
    public required string DisplayDate { get; init; }
}

public class WeekScheduleData
{
    public int WeekNumber { get; init; }
    public int Year { get; init; }
    public required string StartDate { get; init; }
    public required string EndDate { get; init; }
    public required List<string> ClosedDays { get; init; }
    public required List<EmployeeScheduleData> Employees { get; init; }
}

public class EmployeeScheduleData
{
    public required string EmployeeId { get; init; }
    public required string Name { get; init; }
    public int WeeklyHours { get; init; }
    public required Dictionary<string, ShiftData> Schedule { get; init; }
}

public class ShiftData
{
    public required string Status { get; init; }
    public string? Start { get; init; }
    public string? End { get; init; }
    public string? Note { get; init; }
}
