@inherits LocalizableComponentBase

<section class="flex flex-col space-y-4">

	@if (isPosting)
	{
		<div class="block p-3 md:p-4 neomorph rounded-xl is-nxsmall">
			<MessageUpsertForm OnMessageSubmit="OnMessageSubmit" />
		</div>
	}

	<div class="flex justify-between space-x-3">

		<EditForm Model="Filters">
			<div class="field flex flex-row space-x-3">
				@if (OnTimelineChanged.HasDelegate)
				{
					<div class="control has-icons-left">
						<span class="icon is-left">
							<i class="@Filters.TimelineType.GetTimelineTypeIcon()"></i>
						</span>
						<span class="select is-rounded neoSelect">
							<InputSelect TValue="TimelineType" Value="Filters.TimelineType"
												 ValueChanged="async v => await OnTimelineChange(v)" ValueExpression="() => Filters.TimelineType">
								@foreach (var timelineType in Enum.GetValues<TimelineType>())
								{
									<option value="@timelineType">@Localizer[timelineType.ToString()]</option>
								}
							</InputSelect>
						</span>
					</div>
				}
				@if (OnTimeSortingChanged.HasDelegate)
				{
					<div class="control has-icons-left">
						<span class="icon is-left">
							<i class="ion-md-funnel"></i>
						</span>
						<span class="select is-rounded neoSelect">
							<InputSelect TValue="TimeSortingType" Value="Filters.TimeSortingType"
												 ValueChanged="async v => await OnTimeSortChange(v)" ValueExpression="() => Filters.TimeSortingType">
								@foreach (var timeSortingType in Enum.GetValues<TimeSortingType>())
								{
									<option value="@timeSortingType">@Localizer[timeSortingType.ToString()]</option>
								}
							</InputSelect>
						</span>
					</div>
				}
			</div>
		</EditForm>

		@if (OnMessageSubmit.HasDelegate)
		{
			<div>
				<button class="button is-rounded @SUtility.IfTrueThen(isPosting, "neoBtnInsetPlain", "neoBtn")"
							@onclick="OpenCloseMessageForm">
					<span class="icon is-left">
						<i class="ion-md-create"></i>
					</span>
					<span>@Localizer["Post"]</span>
				</button>
			</div>
		}
		else
		{
			<div></div>
		}

	</div>

</section>

@code {
	[CascadingParameter] CascadingState CascadingState { get; set; }
	[Parameter] public EventCallback<MessageForm> OnMessageSubmit { get; set; }
	[Parameter] public EventCallback<TimelineType> OnTimelineChanged { get; set; }
	[Parameter] public EventCallback<TimeSortingType> OnTimeSortingChanged { get; set; }
	[Inject] ILocalStorageService LocalStorage { get; set; }
	bool isPosting { get; set; } = false;
	ActionBarFilter Filters { get; set; } = new();

	protected override async Task OnInitializedAsync()
	{
		var filters = await LocalStorage.GetItemAsync<ActionBarFilter>(nameof(ActionBarFilter));
		if (filters == default)
			await LocalStorage.SetItemAsync(nameof(ActionBarFilter), Filters);
		else
			Filters = filters;
	}

	void OpenCloseMessageForm()
	{
		isPosting = !isPosting;
	}
	async Task OnTimelineChange(TimelineType timelineType)
	{
		Filters.TimelineType = timelineType;
		await LocalStorage.SetItemAsync(nameof(ActionBarFilter), Filters);
		await OnTimelineChanged.InvokeAsync(Filters.TimelineType);
	}
	async Task OnTimeSortChange(TimeSortingType timeSorting)
	{
		Filters.TimeSortingType = timeSorting;
		await LocalStorage.SetItemAsync(nameof(ActionBarFilter), Filters);
		await OnTimeSortingChanged.InvokeAsync(Filters.TimeSortingType);
	}
}