@namespace Connected.Components
@using Connected.Annotations;
@using Connected.Utilities;

<div class="treeview-item-arrow">
    @if (Visible)
    {
        <IconButton OnClick="@ToggleAsync" Icon="@(Loading ? LoadingIcon : ExpandedIcon)" Color="@(Loading ? LoadingIconColor : ExpandedIconColor)" Class="@Classname"></IconButton>
    }
</div>
 
@code {
    protected string Classname =>
    new CssBuilder()
        .AddClass("treeview-item-arrow-expand mud-transform", Expanded && !Loading)
        .AddClass("treeview-item-arrow-load", Loading)
    .Build();

    /// <summary>
    /// If true, displays the button.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Behavior)] public bool Visible { get; set; }

    /// <summary>
    /// Determens when to flip the expanded icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Behavior)] public bool Expanded { get; set; }

    /// <summary>
    /// If true, displays the loading icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Behavior)] public bool Loading { get; set; }

    /// <summary>
    /// Called whenever expanded changed.
    /// </summary>
    [Parameter] public EventCallback<bool> ExpandedChanged { get; set; }

    /// <summary>
    /// The loading icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public string LoadingIcon { get; set; } = Icons.Material.Filled.Loop;

    /// <summary>
    /// The color of the loading. It supports the theme colors.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public ThemeColor LoadingIconColor { get; set; } = ThemeColor.Default;

    /// <summary>
    /// The expand/collapse icon.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public string ExpandedIcon { get; set; } = Icons.Material.Filled.ChevronRight;

    /// <summary>
    /// The color of the expand/collapse. It supports the theme colors.
    /// </summary>
    [Parameter] [Category(CategoryTypes.TreeView.Appearance)] public ThemeColor ExpandedIconColor { get; set; } = ThemeColor.Default;


    private Task ToggleAsync()
    {
        Expanded = !Expanded;
        return ExpandedChanged.InvokeAsync(Expanded);
    }
}
