@namespace Radzen.Blazor
@using Radzen.Blazor
@using Radzen.Blazor.Spreadsheet
@using Radzen.Documents.Spreadsheet
@inherits RadzenSpreadsheetToolBase

<RadzenSplitButton Click=@OnClick Icon="border_all" Disabled=@IsDisabled Variant="Variant.Outlined" ButtonStyle="ButtonStyle.Base" Size="ButtonSize.Small">
    <ChildContent>
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_AllBorders)) ?? "All Borders") Value="all" />
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_OutsideBorders)) ?? "Outside Borders") Value="outside" />
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_TopBorder)) ?? "Top Border") Value="top" />
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_BottomBorder)) ?? "Bottom Border") Value="bottom" />
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_LeftBorder)) ?? "Left Border") Value="left" />
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_RightBorder)) ?? "Right Border") Value="right" />
        <RadzenSplitButtonItem Text=@(Spreadsheet?.Localize(nameof(RadzenStrings.Spreadsheet_NoBorder)) ?? "No Border") Value="none" />
    </ChildContent>
</RadzenSplitButton>

@code {
    protected override SpreadsheetFeature? Feature => SpreadsheetFeature.CellFormatting;

    private async Task OnClick(RadzenSplitButtonItem? item)
    {
        if (Worksheet is null || Worksheet.Selection.Cell == CellRef.Invalid || Spreadsheet is null)
        {
            return;
        }

        var value = item?.Value;
        var range = Worksheet.Selection.Range;
        var border = new BorderStyle();

        ICommand? command = value switch
        {
            "all" or null => new AllBordersCommand(Worksheet, range, border),
            "outside" => new BorderCommand(Worksheet, range, border, border, border, border),
            "top" => new BorderCommand(Worksheet, range, top: border, right: null, bottom: null, left: null),
            "bottom" => new BorderCommand(Worksheet, range, top: null, right: null, bottom: border, left: null),
            "left" => new BorderCommand(Worksheet, range, top: null, right: null, bottom: null, left: border),
            "right" => new BorderCommand(Worksheet, range, top: null, right: border, bottom: null, left: null),
            "none" => new NoBordersCommand(Worksheet, range),
            _ => null,
        };

        if (command is not null)
        {
            await Spreadsheet.ExecuteAsync(command);
        }
    }
}
