@namespace SufiChain.SufiBlazor.Components.Forms

<label class="@CssClass" style="@Style">
    <span class="sb-switch__input-wrapper">
        <input type="checkbox"
               class="sb-switch__input"
               id="@Id"
               checked="@Value"
               disabled="@Disabled"
               role="switch"
               aria-checked="@(Value ? "true" : "false")"
               @onchange="HandleChange"
               @attributes="InputAttributes" />
    </span>
    <span class="sb-switch__track">
        <span class="sb-switch__thumb"></span>
    </span>
    @if (!string.IsNullOrEmpty(Label) || ChildContent != null || !string.IsNullOrEmpty(EffectiveHelperText))
    {
        <span class="sb-switch__text">
            @if (!string.IsNullOrEmpty(Label) || ChildContent != null)
            {
                <span class="sb-switch__label">
                    @if (ChildContent != null)
                    {
                        @ChildContent
                    }
                    else
                    {
                        @Label
                    }
                </span>
            }
            @if (!string.IsNullOrEmpty(EffectiveHelperText))
            {
                <span class="sb-switch__helper">@EffectiveHelperText</span>
            }
        </span>
    }
</label>

@code {
    /// <summary>
    /// Switch value.
    /// </summary>
    [Parameter]
    public bool Value { get; set; }

    /// <summary>
    /// Value changed callback.
    /// </summary>
    [Parameter]
    public EventCallback<bool> ValueChanged { get; set; }

    /// <summary>
    /// Switch label.
    /// </summary>
    [Parameter]
    public string? Label { get; set; }

    /// <summary>
    /// Whether the switch is disabled.
    /// </summary>
    [Parameter]
    public bool Disabled { get; set; }

    /// <summary>
    /// Switch color.
    /// </summary>
    [Parameter]
    public SbColor Color { get; set; } = SbColor.Primary;

    /// <summary>
    /// Switch size.
    /// </summary>
    [Parameter]
    public SbSize Size { get; set; } = SbSize.Md;

    /// <summary>
    /// Helper text shown below the label.
    /// </summary>
    [Parameter]
    public string? HelperText { get; set; }

    /// <summary>
    /// Custom label content.
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Element ID for the input.
    /// </summary>
    [Parameter]
    public string? Id { get; set; }

    /// <summary>
    /// Additional CSS classes.
    /// </summary>
    [Parameter]
    public string? Class { get; set; }

    /// <summary>
    /// Inline styles.
    /// </summary>
    [Parameter]
    public string? Style { get; set; }

    /// <summary>
    /// Additional HTML attributes.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private string CssClass
    {
        get
        {
            var classes = new List<string> { "sb-switch" };

            classes.Add($"sb-switch--{Color.ToString().ToLowerInvariant()}");
            classes.Add($"sb-switch--{Size.ToString().ToLowerInvariant()}");

            if (Value)
            {
                classes.Add("sb-switch--checked");
            }

            if (Disabled)
            {
                classes.Add("sb-switch--disabled");
            }

            if (!string.IsNullOrWhiteSpace(Class))
            {
                classes.Add(Class);
            }

            return string.Join(" ", classes);
        }
    }

    private string? EffectiveHelperText => !string.IsNullOrEmpty(HelperText)
        ? HelperText
        : GetAdditionalAttribute("helpertext") ?? GetAdditionalAttribute("HelperText");

    private IReadOnlyDictionary<string, object>? InputAttributes
    {
        get
        {
            if (AdditionalAttributes == null || AdditionalAttributes.Count == 0)
                return null;
            var filtered = new Dictionary<string, object>(AdditionalAttributes);
            filtered.Remove("helpertext");
            filtered.Remove("HelperText");
            return filtered;
        }
    }

    private string? GetAdditionalAttribute(string key)
    {
        if (AdditionalAttributes == null) return null;
        if (AdditionalAttributes.TryGetValue(key, out var value) && value is string s)
            return s;
        foreach (var kv in AdditionalAttributes)
        {
            if (string.Equals(kv.Key, key, StringComparison.OrdinalIgnoreCase) && kv.Value is string s2)
                return s2;
        }
        return null;
    }

    private async Task HandleChange(ChangeEventArgs e)
    {
        if (!Disabled)
        {
            Value = (bool)(e.Value ?? false);
            await ValueChanged.InvokeAsync(Value);
        }
    }
}
