using Blazorise;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.BaseClasses;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;

namespace FoodsharingSiegen.Server.Dialogs
{
    /// <summary>
    ///     The add prospect modal class (a. beging, 31.05.2022)
    /// </summary>
    public partial class EditProspectDialog : FsBase
    {
        #region Dependencies

        /// <summary>
        ///     Gets or sets the value of the prospect service (ab)
        /// </summary>
        [Inject]
        public ProspectService ProspectService { get; set; } = null!;

        #endregion

        #region Parameters

        /// <summary>
        ///     Gets or sets the value of the is update mode (ab)
        /// </summary>
        [Parameter]
        public bool IsUpdateMode { get; set; }
        
        [Parameter]
        public Func<Task>? OnSuccess { get; set; }

        /// <summary>
        ///     Gets or sets the value of the on add (ab)
        /// </summary>
        [Parameter]
        public EventCallback<Prospect> OnAdd { get; set; }

        /// <summary>
        ///     Gets or sets the value of the on update (ab)
        /// </summary>
        [Parameter]
        public EventCallback<Prospect> OnUpdate { get; set; }

        /// <summary>
        ///     Gets or sets the value of the prospect (ab)
        /// </summary>
        [Parameter]
        public Prospect Prospect { get; set; } = new();

        /// <summary>
        ///     Gets or sets the value of the save button text (ab)
        /// </summary>
        [Parameter]
        public string? SaveButtonText { get; set; }

        #endregion

        #region Public Method ShowAsync

        /// <summary>
        ///     Displays the AddProspectModal dialog asynchronously, allowing for the creation or editing of a prospect.
        /// </summary>
        /// <param name="modalService">The modal service used to display the dialog.</param>
        /// <param name="onSuccess">Callback to be invoked upon successful addition or update of a prospect.</param>
        /// <param name="prospect">The prospect to be edited, or null for creating a new prospect.</param>
        /// <returns>A task representing the asynchronous operation.</returns>
        public static async Task ShowAsync(IModalService modalService, Func<Task> onSuccess, Prospect? prospect = null)
        {
            await modalService.Show<EditProspectDialog>(prospect == null ? "Neuling hinzufügen" : $"{prospect.Name} bearbeiten", p =>
            {
                p.Add(nameof(Prospect), prospect ?? new Prospect());
                p.Add(nameof(IsUpdateMode), prospect != null);
                p.Add(nameof(SaveButtonText), prospect == null ? "Hinzufügen" : "Speichern");
                p.Add(nameof(OnSuccess), onSuccess);
            });
        }

        #endregion

        #region Private Method SaveClick

        private bool _isSaving;

        /// <summary>
        ///     Saves the click (a. beging, 31.05.2022)
        /// </summary>
        private async Task SaveClick()
        {
            if (_isSaving) return;
            _isSaving = true;
            try
            {
                if (IsUpdateMode)
                {
                    var updateR = await ProspectService.UpdateAsync(Prospect);
                    if (updateR.Success)
                    {
                        if (OnSuccess != null) await OnSuccess.Invoke();
                        await ModalService.Hide();
                    }
                    else
                    {
                        await Notification.Error(updateR.Exception?.Message ?? "Unbekannter Fehler beim Speichern.", "Fehler");
                    }
                }
                else
                {
                    var addR = await ProspectService.AddProspectAsync(Prospect);
                    if (addR.Success)
                    {
                        if (OnSuccess != null) await OnSuccess.Invoke();
                        await ModalService.Hide();
                    }
                    else
                    {
                        await Notification.Error(addR.Exception?.Message ?? "Unbekannter Fehler beim Hinzufügen.", "Fehler");
                    }
                }
            }
            catch (Exception ex)
            {
                await Notification.Error(ex.Message, "Systemfehler");
            }
            finally
            {
                _isSaving = false;
            }
        }

        #endregion
    }
}