@using MongoDB.Bson;

@inject ISnackbar Snackbar
@inject IDialogService Dialog

<MudDrawer @bind-Open="_visible" Anchor="Anchor.End" Elevation="0" Variant="@DrawerVariant.Temporary" ClipMode="DrawerClipMode.Always" Width="400px" Style="max-width:auto;">
    <MudDrawerHeader>
        <MudText Typo="Typo.h6">
            Options
        </MudText>
    </MudDrawerHeader>
    <MudStack Justify="Justify.Center" Class="px-6" Spacing="3">
        @if (_content == Content.Menu)
        {
            <MudButton OnClick="()=>_browserVisible = !_browserVisible" Variant="Variant.Filled" DisableElevation Size="Size.Large" Color="Color.Success">
                 Browser
            </MudButton>
            <MudButton OnClick="()=>_content = Content.Save" Variant="Variant.Filled" DisableElevation Size="Size.Large" Color="Color.Success" Disabled>
                 Save
             </MudButton>
            <MudButton OnClick="()=>_content = Content.Load" Variant="Variant.Filled" DisableElevation Size="Size.Large" Color="Color.Error">
                 Load
            </MudButton>
        }
        else if (_content == Content.Save)
        {
            
        }
        else if (_content == Content.Load)
        {
            <MudList Clickable="true" Style="max-height: calc(100vh - 160px); overflow-y: scroll;">
                @foreach (var entity in _loadables)
                {
                    <MudListItem OnClick="@(async () => await OnSaveItemAsync(entity))">
                        <div class="d-flex flex-row mt-n1 mb-n1">
                            <div>
                                <MudText Typo="Typo.body1" Style="font-weight: normal;">
                                    @entity.Key
                                </MudText>
                            </div>
                        </div>
                    </MudListItem>
                }
            </MudList>
        }
    </MudStack>
</MudDrawer>

<style>
    .mud-dialog .mud-dialog-content {
        padding: 0;
    }
</style>

<MudDialog @bind-IsVisible="_browserVisible" Options="_browserOptions">
    <DialogContent>
        <MudToolBar Style="height: 48px; background-color:#666699;">
            <MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Dark" Edge="Edge.Start" OnClick="()=>_browserDrawer = !_browserDrawer" />
        </MudToolBar>
        <MudDrawer @bind-Open="_browserDrawer" ClipMode="DrawerClipMode.Never" Variant="DrawerVariant.Persistent" Elevation="2" Height="calc(50vh)">
            <MudNavMenu>
                <MudNavLink Match="NavLinkMatch.All">Store</MudNavLink>
                <MudNavLink Match="NavLinkMatch.All">Library</MudNavLink>
                <MudNavLink Match="NavLinkMatch.All">Community</MudNavLink>
            </MudNavMenu>
        </MudDrawer>
        <MudList Clickable="true" DisablePadding Style="width: calc(50vw); height: calc(50vh); overflow-y: auto;">
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
            <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
            <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
        </MudList>
        
        
        @* <MudStack Row="true">
            <MudPaper Width="300px">
                <MudList Clickable="true">
                    <MudListItem Text="Neutral" Value="1" />
                    <MudListItem Text="Good">
                        <NestedList>
                            <MudListItem Text="Frodo Baggins" Value="2" />
                            <MudListItem Text="Harry Potter" Value="3" />
                        </NestedList>
                    </MudListItem>
                    <MudListItem Text="Evil">
                        <NestedList>
                            <MudListItem Text="Sauron" Value="5" />
                            <MudListItem Text="Voldemort" Value="6" />
                            <MudListItem Text="Mckaragoz" Value="7" />
                            <MudListItem Text="Sauron" Value="8" />
                            <MudListItem Text="Voldemort" Value="9" />
                            <MudListItem Text="Mckaragoz" Value="10" />
                        </NestedList>
                    </MudListItem>
                </MudList>
            </MudPaper>
            <MudPaper Class="pe-4" Width="calc(100vw - 50px)">
                <MudList Clickable="true">
                    <MudListItem Text="Item 1" Icon="@Icons.Material.Filled.Inbox" />
                    <MudListItem Text="Item 2" Icon="@Icons.Material.Filled.Send" />
                    <MudListItem Text="Item 3" />
                    <MudListItem Text="Item 4" />
                    <MudListItem Text="Item 5" />
                    <MudListItem Text="Item 6" />
                    <MudListItem Text="Item 7" />
                    <MudListItem Text="Item 8" />
                </MudList>
            </MudPaper>
        </MudStack> *@
    </DialogContent>
</MudDialog>

@code {
    private DialogOptions _browserOptions = new() { FullWidth = false, MaxWidth = MaxWidth.Large, FullScreen = false, NoHeader = true };
    private bool _browserVisible;
    private bool _browserDrawer = true;

    //

    [Parameter] public EventCallback<ScriptItem> OnChanges { get; set; }

    private Content _content = Content.Menu;

    private enum Content { Menu = 0, Save = 1, Load = 3 }
    private bool _visible;
    private ScriptItem? _currentItem;

    private List<ScriptItem> _loadables = new();

    public async Task ToggleAsync(string? id)
    {
        _content = Content.Menu;
        _visible = !_visible;

        await InvokeAsync(StateHasChanged);
    }

    public async Task ToggleBrowserAsync()
    {
        _visible = !_visible;
        _browserVisible = !_browserVisible;
    }

    private async Task OnSaveItemAsync(ScriptItem item)
    {
        _currentItem = item;
        _visible = false;

        Notification.Information(Snackbar, $"Save Item ({item.Key})");

        // base64 encode
        var valueBytes = System.Text.Encoding.UTF8.GetBytes(item.Value);
        var valueBase64 = System.Convert.ToBase64String(valueBytes);

        // save to database


        await OnChanges.InvokeAsync(item);
    }

    private async Task OnLoadItemAsync(ScriptItem item)
    {
        _currentItem = item;
        _visible = false;

        Notification.Information(Snackbar, $"Load Item ({item.Key})");

        await OnChanges.InvokeAsync(item);
    }

    protected override void OnInitialized()
    {
        _loadables.Add(new ScriptItem { Key = "Date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RyaW0oKQogICAgICAgICAgICAkbW9kdWxlc1skY3VycmVudE1vZHVsZV1bJGtleV0gPSAkdmFsdWUKICAgICAgICB9CiAgICB9Cn0KCiMgQ29udmVydCB0byBKU09OCiRqc29uID0gJG1vZHVsZXMgfCBDb252ZXJ0VG8tSnNvbiAtRGVwdGggMwokanNvbg==";
        _loadables.Add(new ScriptItem { Key = "IPMI (Power)", Value = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(ipmi64)) });
    }

    public class ScriptItem
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}