// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.CustomArgs;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.Types;

namespace SelmoStudio.Hmi.Server.Shared.Components.BaseComponents
{
    public partial class SwitchComponent
    {
        private const string defaultStateText = "";

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

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

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

        [Parameter]
        public EventCallback<(ComponentEventInfo EventArgs, bool Value)> ValueChanged { get; set; }

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

        private async Task OnValueChanged(bool value)
        {
            ComponentEventInfo eventInfo = new()
            {
                ComponentId = ComponentId,
                Type = EventType.SwitchChange,
                ButtonType = HmiElementType.Switch
            };

            await ValueChanged.InvokeAsync((eventInfo, value));
        }

        private string GetSateText()
        {
            return State ? OnText ?? defaultStateText : OffText ?? defaultStateText;
        }

        [Parameter]
        public HmiElementType ButtonType { get; set; }

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

            return;
        }
    }
}