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

namespace FoodsharingSiegen.Server.Dialogs
{
    public record InteractionDialogParameter(InteractionType Type, Guid ProspectId, string HeaderText, Func<Task> OnSuccess);

    public partial class InteractionDialog : 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

        [Parameter]
        public bool AllowFeedback { get; set; }

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

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

        [Parameter]
        public Interaction Interaction { get; set; } = new();

        [Parameter]
        public Func<Task>? OnSuccess { get; set; }

        [Parameter]
        public bool ShowAlert { get; set; }

        [Parameter]
        public bool ShowNotNeeded { get; set; }

        #endregion

        #region Public Method ShowAsync

        /// <summary>
        ///     Displays the InteractionDialog modal with the specified parameters.
        /// </summary>
        /// <param name="modalService">The modal service used to display the dialog.</param>
        /// <param name="parameter">The parameters required for the dialog, including type, prospect ID, header text, and success callback.</param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        public static async Task ShowAsync(IModalService modalService, InteractionDialogParameter parameter)
        {
            var allowFeedback = parameter.Type switch
            {
                InteractionType.EinAb => true,
                _ => false
            };

            var info1Name = parameter.Type switch
            {
                InteractionType.EinAb => "Welcher Betrieb?",
                InteractionType.Complete => "Kommentar",
                InteractionType.IdCheck => "Kommentar",
                InteractionType.PrintPass => "Kommentar",
                InteractionType.ReleasedForVerification => "Hinweis",
                _ => null
            };
            
            var info2Name = parameter.Type switch
            {
                InteractionType.EinAb => "Mit wem?",
                _ => null
            };

            var showAlert = parameter.Type switch
            {
                InteractionType.EinAb => true,
                InteractionType.IdCheck => true,
                _ => false
            };

            var showNotNeeded = parameter.Type switch
            {
                InteractionType.PrintPass => true,
                _ => false
            };

            var interaction = new Interaction
            {
                Type = parameter.Type,
                Date = DateTime.UtcNow,
                ProspectID = parameter.ProspectId
            };

            await modalService.Show<InteractionDialog>(parameter.HeaderText, p =>
            {
                p.Add(nameof(Interaction), interaction);
                p.Add(nameof(AllowFeedback), allowFeedback);
                p.Add(nameof(Info1Name), info1Name);
                p.Add(nameof(Info2Name), info2Name);
                p.Add(nameof(ShowAlert), showAlert);
                p.Add(nameof(ShowNotNeeded), showNotNeeded);
                p.Add(nameof(OnSuccess), parameter.OnSuccess);
            });
        }

        #endregion

        #region Private Method AddInteractionAsync

        /// <summary>
        ///     Adds a new interaction for the current user using the ProspectService and hides the modal.
        /// </summary>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        private async Task AddInteractionAsync()
        {
            Interaction.UserID = CurrentUser.Id;

            var addR = await ProspectService.AddInteraction(Interaction);

            await ModalService.Hide();

            if (addR.Success && OnSuccess != null) await OnSuccess.Invoke();
        }

        #endregion

        #region Private Method SetFeedbackAsync

        /// <summary>
        ///     Sets the feedback type for the interaction and updates the feedback property.
        /// </summary>
        /// <param name="feedback">The feedback type to be set for the interaction.</param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        private async Task SetFeedbackAsync(InteractionFeedback feedback)
        {
            Interaction.Feedback = feedback;
            await Task.CompletedTask;
        }

        #endregion
    }
}