@using ZB.MOM.WW.MxGateway.Contracts.Proto.Galaxy

@*
    Recursive Browse hierarchy node. Renders one Galaxy object, its child
    objects (recursively), and its attributes as right-clickable tag rows.
    Expansion state is local; children render only while expanded.
*@

<div class="tree-node">
    <div class="tree-row @(Node.IsArea ? "tree-row-area" : "tree-row-object")">
        @if (Node.HasChildren)
        {
            <button type="button" class="tree-toggle" @onclick="Toggle" aria-label="Toggle">
                @(_expanded ? "▾" : "▸")
            </button>
        }
        else
        {
            <span class="tree-toggle tree-toggle-empty"></span>
        }
        <span class="tree-label" @onclick="Toggle">
            <span class="tree-icon">@(Node.IsArea ? "▣" : "◇")</span>
            <span class="tree-name">@Node.DisplayName</span>
            @if (!string.IsNullOrWhiteSpace(Node.Object.TagName)
                && !string.Equals(Node.Object.TagName, Node.DisplayName, StringComparison.Ordinal))
            {
                <code class="tree-tag">@Node.Object.TagName</code>
            }
        </span>
    </div>
    @if (_expanded)
    {
        <div class="tree-children">
            @foreach (DashboardBrowseNode child in Node.Children)
            {
                <BrowseTreeNodeView Node="child" OnAddTag="OnAddTag" OnTagContextMenu="OnTagContextMenu" />
            }
            @foreach (GalaxyAttribute attr in Node.Attributes)
            {
                GalaxyAttribute row = attr;
                <div class="tree-attr"
                     title="@row.FullTagReference"
                     @oncontextmenu:preventDefault="true"
                     @oncontextmenu="@(args => OnTagContextMenu.InvokeAsync((args, row)))"
                     @ondblclick="@(() => OnAddTag.InvokeAsync(row.FullTagReference))">
                    <span class="tree-toggle tree-toggle-empty"></span>
                    <span class="attr-icon">·</span>
                    <span class="attr-name">@row.AttributeName</span>
                    <span class="attr-type">@DisplayType(row)</span>
                    @if (row.IsAlarm)
                    {
                        <span class="attr-flag attr-flag-alarm">alarm</span>
                    }
                    @if (row.IsHistorized)
                    {
                        <span class="attr-flag attr-flag-hist">hist</span>
                    }
                </div>
            }
        </div>
    }
</div>

@code {
    /// <summary>The hierarchy node this view renders.</summary>
    [Parameter]
    [EditorRequired]
    public DashboardBrowseNode Node { get; set; } = null!;

    /// <summary>Raised with a tag's full reference when the operator double-clicks it.</summary>
    [Parameter]
    public EventCallback<string> OnAddTag { get; set; }

    /// <summary>Raised when an attribute row is right-clicked, for the context menu.</summary>
    [Parameter]
    public EventCallback<(MouseEventArgs Event, GalaxyAttribute Attribute)> OnTagContextMenu { get; set; }

    private bool _expanded;

    private void Toggle()
    {
        if (Node.HasChildren)
        {
            _expanded = !_expanded;
        }
    }

    private static string DisplayType(GalaxyAttribute attribute)
    {
        string baseType = string.IsNullOrWhiteSpace(attribute.DataTypeName)
            ? "type?"
            : attribute.DataTypeName;
        if (attribute.IsArray)
        {
            return attribute.ArrayDimensionPresent
                ? $"{baseType}[{attribute.ArrayDimension}]"
                : $"{baseType}[]";
        }

        return baseType;
    }
}
