// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Radzen;
using SelmoStudio.Hmi.Server.SelmoHmi.Helpers;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.CustomArgs;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.Ids;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.Types;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;

namespace SelmoStudio.Hmi.Server.Shared.Components.BaseComponents
{
    public abstract partial class ButtonBaseComponent : ComponentBase
    {
        private bool _isToolTipApplicable => _tooltipService != null && !string.IsNullOrEmpty(Tooltip);

        [Parameter]
        public bool Visible { get; set; } = true;

        [Parameter]
        public string Content { get; set; }

        [Parameter]
        public string IconClass { get; set; }

        [Parameter]
        public WellKnownContentFlexDirection ContentFlexDirection { get; set; }

        [Parameter]
        public string ContentStyleString { get; set; }

        [Parameter]
        public string ContentClassString { get; set; }

        [Parameter]
        public string Tooltip { get; set; }

        [Parameter]
        public WellKnownTooltipPosition TooltipPosition { get; set; } = WellKnownTooltipPosition.Right;

        [Parameter]
        public WellKnownIconPosition IconPosition { get; set; } = WellKnownIconPosition.Left;

        [Parameter]
        public WellKnownIconType IconType { get; set; } = WellKnownIconType.Class;

        [Parameter]
        public string IconSvg { get; set; }

        [Parameter]
        public int IconMargin { get; set; } = 2;

        [Parameter]
        public int ViewBoxWidth { get; set; }

        [Parameter]
        public int ViewBoxHeight { get; set; }

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

        [Parameter]
        public RenderFragment AdditionalContent { get; set; }

        [Parameter]
        public EventCallback<ButtonEventArgs> MouseDown { get; set; }

        [Parameter]
        public EventCallback<ButtonEventArgs> MouseUp { get; set; }

        [Parameter]
        public SelmoHmi.Shared.Component.Types.HmiElementType ButtonType { get; set; }

        public ComponentId ComponentId { get; set; } = new(Guid.NewGuid());

        protected abstract Task OnClick();

        private void MouseEnter(ElementReference elementReference)
        {
            if (_isToolTipApplicable)
            {
                ShowTooltip(elementReference, new TooltipOptions() { Position = @TooltipPosition.MapToRadzen(), Duration = null });
            }
        }

        private void ShowTooltip(ElementReference elementReference, TooltipOptions options)
        {
            _tooltipService.Open(elementReference, @Tooltip, options);
        }

        private void CloseTooltip()
        {
            _tooltipService.Close();
        }

        private string ButtonContainerStyle => $"{@HtmlStyleHelper.GetButtonContainerStyle(ContentFlexDirection)}";

        private string IconStyle => @HtmlStyleHelper.GetButtonIconTextMargin(ContentFlexDirection, IconPosition, IconMargin);

        private async Task MouseDownClick(MouseEventArgs e)
        {
            if (MouseDown.HasDelegate)
                await MouseDown.InvokeAsync(new(e, ComponentId, EventType.MouseDown, ButtonType));

            return;
        }

        private async Task MouseUpClick(MouseEventArgs e)
        {
            if (MouseUp.HasDelegate)
                await MouseUp.InvokeAsync(new(e, ComponentId, EventType.MouseUp, ButtonType));

            return;
        }

        private void MouseLeave(ElementReference elementReference)
        {
            if (_isToolTipApplicable)
            {
                CloseTooltip();
            }
        }

        private void MouseLeave()
        {
            if (MouseUp.HasDelegate)
                MouseUp.InvokeAsync(new(new(), ComponentId, EventType.MouseLeave, ButtonType));
        }
    }
}