using Blazorise;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Contracts.Model;
using FoodsharingSiegen.Server.BaseClasses;
using Microsoft.AspNetCore.Components;

namespace FoodsharingSiegen.Server.Dialogs
{
    public partial class ProspectFilterDialog : FsBase
    {
        [Parameter] public ProspectFilter CurrentFilter { get; set; } = new();
        [Parameter] public ProspectStateFilter StateFilter { get; set; } = ProspectStateFilter.All;
        [Parameter] public Func<ProspectFilter, Task>? OnFilterApplied { get; set; }

        public ProspectFilter ModalFilter { get; set; } = new();

        protected override void OnInitialized()
        {
            // Clone the filter so changes are not immediately mapped to the parent until "Anwenden" is pressed
            ModalFilter = new ProspectFilter
            {
                Text = CurrentFilter.Text,
                WithoutStepInBriefing = CurrentFilter.WithoutStepInBriefing,
                WithoutIdCheck = CurrentFilter.WithoutIdCheck,
                NoActivity = CurrentFilter.NoActivity,
                RecentActivity = CurrentFilter.RecentActivity,
                DeletedOnly = CurrentFilter.DeletedOnly,
                IdCheckPossible = CurrentFilter.IdCheckPossible
            };
            base.OnInitialized();
        }

        public static async Task ShowAsync(IModalService modalService, ProspectFilter currentFilter, ProspectStateFilter stateFilter, Func<ProspectFilter, Task> onFilterApplied)
        {
            await modalService.Show<ProspectFilterDialog>("Filtern", p =>
            {
                p.Add(nameof(CurrentFilter), currentFilter);
                p.Add(nameof(StateFilter), stateFilter);
                p.Add(nameof(OnFilterApplied), onFilterApplied);
            }, new ModalInstanceOptions
            {
                Size = ModalSize.Small,
            });
        }

        private async Task ApplyAsync()
        {
            if (OnFilterApplied != null) await OnFilterApplied(ModalFilter);
            await ModalService.Hide();
        }
    }
}