using Microsoft.AspNetCore.Components;

namespace Connected.Components;
public partial class DatePicker
{
	private bool loaded = false;

	private Selecting Selecting = Selecting.Days;
	private DateTime CalendarStart { get; set; } = DateTime.Today;
	private DateTime CalendarEnd { get; set; } = DateTime.Today;

	//private DateTime Today = DateTime.Today;

	[Parameter]
	public bool CloseOnDateSelect { get; set; } = false;

	[Parameter]
	public DateTime SelectedDate { get; set;  } = DateTime.Today;

	private DateTime _InitialDate {  get; set; }

	[Parameter]
	public DateTime? SelectedEndDate { get; set; } = null;

	[Parameter]
	public EventCallback<DateTime> SelectedDateChanged { get; set; }

	[Parameter]
	public EventCallback<DateTime?> SelectedEndDateChanged { get; set; }

	private List<DateTime> ShowingDates = new List<DateTime>();

	[Parameter]
	public string ErrorText { get; set; } = string.Empty;

	[Parameter]
	public string LabelText { get; set; } = "Select date";

	[Parameter]
	public string HelperText { get; set; } = string.Empty;

	[Parameter]
	public bool UseDateRange { get; set; } = false;

	[Parameter]
	public bool ShowBottomBar { get; set; } = false;

	[Parameter]
	public string Format { get; set; } = "dd.MM.yyyy";

	private int offset = 0;

	private bool _readonly = false;
	[Parameter]
	public bool Editable
	{
		get
		{
			return !_readonly;
		}
		set
		{
			_readonly = !value;
		}
	}
	private bool Shown { get; set; } = false;

	public async Task SetDate(DateTime Date)
	{
		SelectedDate = Date;
		SetStartStop();
		if (CloseOnDateSelect)
			Shown = false;
		await SelectedDateChanged.InvokeAsync(SelectedDate);
		StateHasChanged();
	}

	public async Task SetMonth(int month)
	{
		SelectedDate = new DateTime(SelectedDate.Year, month, SelectedDate.Day);
		Selecting = Selecting.Days;
		SetStartStop();
		await SelectedDateChanged.InvokeAsync(SelectedDate);
		StateHasChanged();
	}

	public async Task SetYear(int year)
	{
		SelectedDate = new DateTime(year, SelectedDate.Month, SelectedDate.Day);
		Selecting = Selecting.Months;
		SetStartStop();
		await SelectedDateChanged.InvokeAsync(SelectedDate);
		StateHasChanged();
	}

	public void SetStartStop()
	{
		CalendarStart = new DateTime(SelectedDate.Year,SelectedDate.Month,1);
		CalendarEnd = CalendarStart.AddMonths(1).AddMinutes(-1);
		offset=0;
		DateTime temp = CalendarStart;
		while (!CalendarStart.DayOfWeek.Equals(DayOfWeek.Monday))
		{
			CalendarStart = CalendarStart.AddDays(-1);
			offset++;
		}

		DateTime start = CalendarStart.AddDays(offset);
		if (ShowingDates is null) ShowingDates = new();
		ShowingDates.Clear();
		while (start.CompareTo(CalendarEnd) < 0)
		{
			ShowingDates.Add(start);
			start = start.AddDays(1);
		}
	}

	public string DateChipClass(DateTime date)
	{
		string result = "";
		if (date.Month.CompareTo(SelectedDate.Month) < 0 || date.Month.CompareTo(SelectedDate.Month) > 0) return "d-none";
		if (date.CompareTo(DateTime.Today) == 0) result = "days current";
		if (date.Month.CompareTo(SelectedDate.Month) == 0)
		{
			if (date.Date.CompareTo(SelectedDate.Date) == 0)
				return result+"days active";
			else
				return result+" ";
		}
		return result;
	}

	public string MonthChipClass(int month)
	{
		if (month.CompareTo(SelectedDate.Month) == 0)
			return "active";
		else
			return "";
	}

	public string YearChipClass(int year)
	{
		if (year.CompareTo(SelectedDate.Year) == 0)
			return "active";
		else
			return "";
	}

	protected override async Task OnInitializedAsync()
	{
		SelectedDate = DateTime.Today;
		_InitialDate=SelectedDate;
		SetStartStop();
		loaded = true;
		await base.OnInitializedAsync();
	}

	private async Task ClosePicker(bool resetDate=true)
	{
		if (resetDate)
			SelectedDate=_InitialDate;
		Shown = false;
		await SelectedDateChanged.InvokeAsync(SelectedDate);
	}

	private void TogglePicker()
	{
		_InitialDate=SelectedDate;
		Shown = !Shown;
	}

	private async Task NavBarClick(bool forward = true)
	{
		int multiplier = 1;
		if (!forward) multiplier = -1;
		switch (Selecting)
		{
			case Selecting.Years:
				{
					SelectedDate=SelectedDate.AddYears(8 * multiplier);
					break;
				}
			case Selecting.Months:
				{
					SelectedDate = SelectedDate.AddYears(1 * multiplier);
					break;
				}
			case Selecting.Days: 
				{
					SelectedDate = SelectedDate.AddMonths(1*multiplier);
					break;
				}
		}
		SetStartStop();
		await InvokeAsync(StateHasChanged);
	}

}

public enum Selecting
{
	Days,
	Months,
	Years
}
