<div title=@Title @onmousedown:preventDefault>
    <RadzenColorPicker @bind-Value=@Value Change=@OnChange Icon=@Icon IconColor=@IconColor
        InputSize="InputSize.Small" ShowArrow="false" TabIndex=-1
        Disabled=@(Editor?.Disabled == true || !EnabledModes.HasFlag(Editor?.GetMode() ?? HtmlEditorMode.Design))
        Open=@OnOpen ShowHSV=@ShowHSV ShowRGBA=@ShowRGBA ShowColors=@ShowColors ShowButton=@ShowButton
        HexText=@HexText RedText=@RedText GreenText=@GreenText BlueText=@BlueText AlphaText=@AlphaText
        ButtonText=@ButtonText ChildContent=@ChildContent />
</div>

@code {
    [CascadingParameter]
    public RadzenHtmlEditor? Editor { get; set; }

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

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

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

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

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

    [Parameter]
    public HtmlEditorMode EnabledModes { get; set; } = HtmlEditorMode.Design;

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

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

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

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

    [Parameter]
    public string HexText { get; set; } = "Hex";

    [Parameter]
    public string RedText { get; set; } = "R";

    [Parameter]
    public string GreenText { get; set; } = "G";

    [Parameter]
    public string BlueText { get; set; } = "B";

    [Parameter]
    public string AlphaText { get; set; } = "A";

    [Parameter]
    public string ButtonText { get; set; } = "OK";

    [Parameter]
    public EventCallback<string> Change { get; set; }

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    async Task OnChange(string value)
    {
        if (Editor != null)
        {
            await Editor.RestoreSelectionAsync();
        }
        await ValueChanged.InvokeAsync(value);
        await Change.InvokeAsync(value);
    }

    async Task OnOpen()
    {
        if (Editor != null)
        {
            await Editor.SaveSelectionAsync();
        }
    }
}
