@using Rms.Risk.Mango.Pivot.Core

@*
 *                                dbMango
 *
 * Copyright 2025 Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *@
<div class="@Class">
    @if (ShowCollection)
    {
        <div class="mr-3">
            <div class="form-group">

                <FormItemSelect T="GroupedCollection"
                                Name="Collection"
                                Icon="icon-folder-outline-sm"
                                Values="@Collections"
                                Placeholder="Select collection..."
                                IsSelectable="@IsSelectableGroup"
                                GetText="@(x => x.CollectionNameWithoutPrefix)"
                                @bind-Value="SelectedCollectionNode" />
            </div>
        </div>
    }
    @if (ShowPivot)
    {
        <div class="mr-3">
            <div class="form-group">

                <FormItemSelect T="GroupedPivot"
                                Name="Pivot"
                                Icon="icon-tiles"
                                Values="@(SelectedCollectionNode?.Pivots ?? [])"
                                Placeholder="Select pivot..."
                                IsSelectable="@IsSelectablePivot"
                                @bind-Value="SelectedPivotNode" />
            </div>
        </div>
    }
</div>

@code {
    [CascadingParameter] public IModalService          Modal          { get; set; } = null!;
    [Parameter]          public bool                   ShowCollection { get; set; } = true;
    [Parameter]          public bool                   ShowPivot      { get; set; } = true;
    [Parameter]          public string                 Class          { get; set; } = "form-row";

    [Parameter]
    public string? Collection
    {
        get;
        set
        {
            if (field == value || value == null)
                return;

            field = value;

            SyncSelectedNodes();

            CollectionChanged.InvokeAsync(field);
            InvokeAsync(StateHasChanged);
        }
    }

    private void SyncSelectedNodes()
    {
        if (Collections.Count == 0 || string.IsNullOrWhiteSpace(Collection))
            return;

        SelectedCollectionNode = Collections.FirstOrDefault(x => x.CollectionNameWithPrefix == Collection);

        if (string.IsNullOrWhiteSpace(Pivot))
            return;

        SelectedPivotNode      = SelectedCollectionNode?.Pivots.FirstOrDefault(x => x.Pivot?.Name == Pivot);
    }

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

    [Parameter]
    public string? Pivot
    {
        get;
        set
        {
            if (field == value || value == null)
                return;

            field = value;
            SyncSelectedNodes();

            PivotChanged.InvokeAsync(field);
        }
    }

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

    [Parameter]
    public List<GroupedCollection> Collections
    {
        get;
        set
        {
            if (field == value)
                return;

            field = value;

            if ( value.Count > 0 )
            {
                SyncSelectedNodes();
                InvokeAsync(StateHasChanged);
            }
        }
    } = [];

    private GroupedCollection? SelectedCollectionNode
    {
        get;
        set
        {
            if (field == value || value == null)
                return;

            field = value;
            Collection = field?.CollectionNameWithPrefix;
        }
    }

    private GroupedPivot? SelectedPivotNode
    {
        get;
        set
        {
            if (field == value || value == null)
                return;

            field      = value;
            Pivot      = field?.Pivot?.Name;
        }
    }


    private static bool IsSelectableGroup(GroupedCollection? arg) => arg?.IsGroup == false;
    private static bool IsSelectablePivot(GroupedPivot? arg)      => arg?.IsGroup == false;


    protected override void OnParametersSet()
    {
        SyncSelectedNodes();
    }
}
