using Connected.Utilities;
using Microsoft.AspNetCore.Components;

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

	private Selecting Selecting = Selecting.Days;
	public DateTime calendarStart { get; set; }
	public DateTime calendarEnd { get; set; }

	//private DateTime Today = DateTime.Today;

	private DateTime SelectedDate = DateTime.Today;

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

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

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

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

	public void SetMonthNavigation()
	{

	}

	public void SetDate(DateTime Date)
	{
		SelectedDate = Date;
		SetStartStop();
		StateHasChanged();
	}

	public void SetMonth(int month)
	{
		SelectedDate = new DateTime(SelectedDate.Year, month, SelectedDate.Day);
		SetStartStop();
		StateHasChanged();
	}

	private string ChipFirstRowClass
	{
		get
		{
			return new CssBuilder()
				.AddClass("chip-leading-icon d-inline-block p-1 bg-danger text-small text-light")
				.Build();
		}
	}
	private string ChipFirstRowStyle
	{
		get
		{
			return new StyleBuilder()
				.AddStyle("width", "30px")
				.AddStyle("height", "30px")
				.AddStyle("text-align", "center")
				.Build();
		}
	}
	/*
	 @for (int i=0;i<7; i++)
            {
                <div class="chip-leading-icon d-inline-block p-1 bg-danger text-small text-light" style="width:30px; height:30px; text-align:center">@calendarStart.AddDays(i).ToString("ddd").Substring(0,1)</div>
            }
            @while (calendarStart.CompareTo(calendarEnd) < 0)
            {
                <a href="#" @onclick="@(()=>SetDate(calendarStart))"><div class="chip-leading-icon d-inline-block p-1 @ChipStyle(calendarStart)" style="width:30px; height:30px; text-align:center">@calendarStart.Day.ToString()</div></a>
                @if (calendarStart.DayOfWeek.Equals(DayOfWeek.Sunday))
                {
                    <br />
                }
                calendarStart = calendarStart.AddDays(1);
            } 
	 
	 */

	public void SetStartStop()
	{
		calendarStart = SelectedDate;
		while (calendarStart.Day != 1)
		{
			calendarStart = calendarStart.AddDays(-1);
		}
		while (!calendarStart.DayOfWeek.Equals(DayOfWeek.Monday))
		{
			calendarStart = calendarStart.AddDays(-1);
		}
		calendarEnd = SelectedDate;
		while (calendarEnd.Month == SelectedDate.Month)
		{
			calendarEnd = calendarEnd.AddDays(1);
		}
		while (!calendarEnd.DayOfWeek.Equals(DayOfWeek.Monday))
		{
			calendarEnd = calendarEnd.AddDays(1);
		}
		DateTime start = calendarStart;
		if (Dates is null) Dates = new();
		Dates.Clear();
		while (start.CompareTo(calendarEnd) < 0)
		{
			Dates.Add(start);
			start = start.AddDays(1);
		}
	}

	public string DateChipStyle(DateTime date)
	{
		string result = "";
		if (date.Month.CompareTo(SelectedDate.Month) < 0 || date.Month.CompareTo(SelectedDate.Month) > 0) return "bg-core text-light";
		if (date.Month.CompareTo(SelectedDate.Month) == 0)
		{
			if (date.Date.CompareTo(SelectedDate.Date) == 0)
				return "bg-info text-light";
			else
				return "bg-core text-dark";
		}
		return result;
	}

	public string MonthChipClass(int month)
	{
		if (month.CompareTo(SelectedDate.Month) == 0)
			return "bg-info text-light";
		else
			return "bg-core text-dark";
	}

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

}

public enum Selecting
{
	Days,
	Months,
	Years
}
