@using RobotNet.WebApp.Scripts.Components.Dialogs
@implements IDisposable

@inject IJSRuntime JsRuntime
@inject ISnackbar Snackbar
@inject IDialogService DialogService

<MudMenu Class="w-100 h-100" Dense ActivationEvent="@MouseEvent.RightClick" Size="@Size.Small" PositionAtCursor>
    <ActivatorContent>
        <div class="editor-hierachy" @onclick="ResetSelect">
            <div>
                <CascadingValue Value="ItemRadioName" Name="HierachyItemRadioName">
                    @foreach (var model in Workspace.Folders)
                    {
                        <FolderItem Model="model" />
                    }
                    @foreach (var model in Workspace.Files)
                    {
                        <FileItem Model="model" />
                    }
                </CascadingValue>
            </div>
        </div>
    </ActivatorContent>
    <ChildContent>
        <MudMenuItem Label="Save" OnClick="SaveSelect" />
        <MudMenuItem Label="Rename" OnClick="RenameSelect" />
        <MudDivider />
        <MudMenuItem Label="Delete" Class="text-danger" OnClick="DeleteSelect" />
    </ChildContent>
</MudMenu>

@code {
    [CascadingParameter]
    private ScriptWorkspace Workspace { get; set; } = null!;

    readonly string ItemRadioName = $"script-{Guid.NewGuid()}";

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Workspace.RootChanged += OnRootChanged;
    }

    private void OnRootChanged()
    {
        this.InvokeAsync(() =>
        {
            StateHasChanged();
        });
    }

    private async Task ResetSelect()
    {
        Workspace.SelectFolder(null);
        await JsRuntime.InvokeVoidAsync("UncheckRadioAll", ItemRadioName);
    }

    private async Task SaveSelect()
    {
        if (Workspace.IsReadOnly)
        {
            Snackbar.Add($"Hệ thống đang ở trạng thái {Workspace.ProcessorState}, Không thể thay đổi script", Severity.Warning);
            return;
        }
        var results = await Workspace.SaveSelectAsync();
        foreach (var result in results)
        {
            if (!result.IsSuccess)
            {
                Snackbar.Add(result.Message, Severity.Error);
            }
        }
    }

    private async Task DeleteSelect()
    {
        if (Workspace.IsReadOnly)
        {
            Snackbar.Add($"Hệ thống đang ở trạng thái {Workspace.ProcessorState}, Không thể thay đổi script", Severity.Warning);
            return;
        }
        // Truyền Workspace vào DeleteFileOrFolderDialog qua parameters
        var parameters = new DialogParameters
        {
            { "Workspace", Workspace }
        };

        await DialogService.ShowAsync<DeleteFileOrFolderDialog>("", parameters: parameters);
    }

    private async Task RenameSelect()
    {
        if (Workspace.IsReadOnly)
        {
            Snackbar.Add($"Hệ thống đang ở trạng thái {Workspace.ProcessorState}, Không thể thay đổi script", Severity.Warning);
            return;
        }
        var parameters = new DialogParameters
        {
            { "Workspace", Workspace }
        };

        await DialogService.ShowAsync<RenameFileOrFolderDialog>("", parameters: parameters);
    }
    
    public void Dispose()
    {
        Workspace.RootChanged -= OnRootChanged;
        GC.SuppressFinalize(this);
    }
}

