using Blazorise;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Contracts.Model;
using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Server.Dialogs;
using FoodsharingSiegen.Server.Service;
using Microsoft.AspNetCore.Components;

namespace FoodsharingSiegen.Server.Controls;

public partial class ProspectSortControl
{   
    [Parameter]
    public ProspectSortOption CurrentSort { get; set; } = ProspectSortOption.NameAscending;

    [Parameter]
    public EventCallback<ProspectSortOption> CurrentSortChanged { get; set; }

    [Parameter]
    public EventCallback OnSortChanged { get; set; }

    [Parameter]
    public string? StorageKey { get; set; }

    [Parameter] public ProspectFilter Filter { get; set; } = new();
    [Parameter] public EventCallback<ProspectFilter> FilterChanged { get; set; }
    [Parameter] public ProspectStateFilter StateFilter { get; set; } = ProspectStateFilter.All;

    protected override async Task OnInitializedAsync()
    {
        if (!string.IsNullOrEmpty(StorageKey))
        {
            var savedSort = await LocalStorageService.GetItem<ProspectSortOption?>(StorageKey);
            if (savedSort.HasValue)
            {
                CurrentSort = savedSort.Value;
                await CurrentSortChanged.InvokeAsync(CurrentSort);
            }
        }
        await base.OnInitializedAsync();
    }

    private async Task OpenSortDialogAsync()
    {
        await ProspectSortDialog.ShowAsync(ModalService, CurrentSort, async option =>
        {
            await UpdateSortAsync(option);
        });
    }

    private async Task OpenFilterDialogAsync()
    {
        await ProspectFilterDialog.ShowAsync(ModalService, Filter, StateFilter, async (f) =>
        {
            Filter = f;
            await FilterChanged.InvokeAsync(Filter);
            await InvokeAsync(StateHasChanged);
        });
    }

    private async Task TextChangedAsync(string text)
    {
        Filter.Text = text;
        await FilterChanged.InvokeAsync(Filter);
    }

    private async Task DisableFilterAsync(string filterPropName)
    {
        switch (filterPropName)
        {
            case nameof(Filter.WithoutStepInBriefing): Filter.WithoutStepInBriefing = false; break;
            case nameof(Filter.WithoutIdCheck): Filter.WithoutIdCheck = false; break;
            case nameof(Filter.RecentActivity): Filter.RecentActivity = false; break;
            case nameof(Filter.NoActivity): Filter.NoActivity = false; break;
            case nameof(Filter.DeletedOnly): Filter.DeletedOnly = false; break;
            case nameof(Filter.IdCheckPossible): Filter.IdCheckPossible = false; break;
        }
        await FilterChanged.InvokeAsync(Filter);
    }

    private bool HasCustomSort => CurrentSort != ProspectSortOption.NameAscending;

    private string CurrentSortText => CurrentSort switch
    {
        ProspectSortOption.NameDescending => "Sortierung: Name (absteigend)",
        ProspectSortOption.ModifiedAscending => "Sortierung: Zuletzt geändert (aufsteigend)",
        ProspectSortOption.ModifiedDescending => "Sortierung: Zuletzt geaendert (absteigend)",
        _ => string.Empty
    };

    private async Task ResetSortAsync()
    {
        await UpdateSortAsync(ProspectSortOption.NameAscending);
    }

    private async Task UpdateSortAsync(ProspectSortOption option)
    {
        CurrentSort = option;
        
        if (!string.IsNullOrEmpty(StorageKey))
        {
            await LocalStorageService.SetItem(StorageKey, CurrentSort);
        }

        await CurrentSortChanged.InvokeAsync(CurrentSort);
        await OnSortChanged.InvokeAsync();
    }
}