using System.Text;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Server.BaseClasses;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Primitives;

namespace FoodsharingSiegen.Server.Controls
{
    /// <summary>
    ///     The interaction row class (a. beging, 31.05.2022)
    /// </summary>
    public partial class InteractionRow : FsBase
    {
        #region Parameters

        /// <summary>
        ///     Gets or sets the value of the add click (ab)
        /// </summary>
        [Parameter]
        public Func<InteractionType, Task>? AddClick { get; set; }

        /// <summary>
        ///     Gets or sets the value of the allow add interaction (ab)
        /// </summary>
        [Parameter]
        public bool AllowInteraction { get; set; }

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

        /// <summary>
        ///     Gets or sets the value of the icon class (ab)
        /// </summary>
        [Parameter]
        public string? IconClass { get; set; }

        /// <summary>
        ///     Gets or sets the value of the minimum (ab)
        /// </summary>
        [Parameter]
        public int Minimum { get; set; } = 1;

        /// <summary>
        ///     Gets or sets the value of the multiple (ab)
        /// </summary>
        [Parameter]
        public bool Multiple { get; set; }

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

        /// <summary>
        ///     Gets or sets the value of the remove click (ab)
        /// </summary>
        [Parameter]
        public Func<Guid, Task>? RemoveClick { get; set; }

        /// <summary>
        ///     Gets or sets the value of the type (ab)
        /// </summary>
        [Parameter]
        public InteractionType Type { get; set; }

        #endregion

        #region Private Properties

        /// <summary>
        ///     Gets the value of the alert (ab)
        /// </summary>
        private bool Alert => Interactions.Any(x => x.Alert);

        /// <summary>
        ///     Gets the value of the done (ab)
        /// </summary>
        private bool Done => Interactions.Count >= Minimum;

        /// <summary>
        ///     Gets the value of the interactions (ab)
        /// </summary>
        private List<Interaction> Interactions => Prospect?.Interactions.Where(x => x.Type == Type).ToList() ?? [];

        /// <summary>
        ///     Gets the value of the not needed (ab)
        /// </summary>
        private bool NotNeeded => Interactions.Any(x => x.NotNeeded);

        #endregion

        private async Task RemoveFirstAsync(InteractionType type)
        {
            if (Prospect != null && RemoveClick != null)
            {
                var interaction = Interactions.FirstOrDefault(x => x.Type == type);
                if (interaction != null) await RemoveClick(interaction.Id);
            }
        }

        private MarkupString FeedbackBuilder(Interaction interaction)
        {
            var infoList = new List<string>();
            if(!string.IsNullOrWhiteSpace(interaction.Info1)) infoList.Add(interaction.Info1);
            if(!string.IsNullOrWhiteSpace(interaction.Info2)) infoList.Add(interaction.Info2);
            var infos = string.Join(" / ", infoList);
            if (string.IsNullOrWhiteSpace(infos)) return new();
            
            var sb = new StringBuilder();
            sb.Append("(");

            if (interaction.Feedback == InteractionFeedback.Positive) 
                sb.Append("<i class=\"fa-solid fa-thumbs-up\" style=\"margin-right: 0.3rem;\"></i>");
            
            if (interaction.Feedback == InteractionFeedback.Negative) 
                sb.Append("<i class=\"fa-solid fa-thumbs-down\" style=\"margin-right: 0.3rem;\"></i>");

            sb.Append($"<i>{infos}</i>");
            sb.Append(")");

            return new(sb.ToString());
        }
    }
}