@inherits ViewBase
@implements IDisposable


@code {
    private Guid currentEditPlaylistId;
    private string currentEditPlaylistName;

    private FnzBootstrapModal editPlaylistDialog;
    private FnzBootstrapModal deletePlaylistDialog;

    private string EditListNameClass => string.IsNullOrWhiteSpace(currentEditPlaylistName) ? "is-invalid form-control" : "form-control";

    private void PlaylistChanged() => StateHasChanged();

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender) PlaylistHelper.Changed += PlaylistChanged;
    }

    void IDisposable.Dispose() => PlaylistHelper.Changed -= PlaylistChanged;
}

<div class="container mt-3">
    @foreach (var item in PlaylistHelper.PlayListLoadSave.KeepNoEmpty().WithIndex())
    {
        if (item.item.Value != null)
        {
            <PlaylistTable 
                Playlist="@item.item.Value"
                ListItemIndex="@item.index"
                ListItemCount="@PlaylistHelper.PlayListLoadSave.Count" 
                EditPlaylist="ShowPlaylistEdit" 
                DeletePlaylist="ShowPlaylistDelete"></PlaylistTable>
        }
        else
        {
            <table class="table w-100">
                <tbody>
                    <tr>
                        <td>
                            No playlist created
                        </td>
                    </tr>
                </tbody>
            </table>
        }
    }
</div>

@code {
    void ShowPlaylistEdit(Guid playlistId)
    {
        currentEditPlaylistId = playlistId;
        currentEditPlaylistName = PlaylistHelper.PlayListLoadSave[currentEditPlaylistId]?.Name ?? "???";
        editPlaylistDialog.Show();
    }

    void ShowPlaylistDelete(Guid playlistId)
    {
        currentEditPlaylistId = playlistId;
        currentEditPlaylistName = PlaylistHelper.PlayListLoadSave[currentEditPlaylistId]?.Name ?? "???";
        deletePlaylistDialog.Show();
    }

    void EditPlaylistNameChanged(ChangeEventArgs e)
    {
        currentEditPlaylistName = e.Value?.ToString()?.Trim();
        StateHasChanged();
    }

    void UpdatePlaylist()
    {
        if (string.IsNullOrWhiteSpace(currentEditPlaylistName)) return;
        PlaylistHelper.UpdatePlaylistName(currentEditPlaylistId, currentEditPlaylistName);
        StateHasChanged();
        editPlaylistDialog.Hide();
    }

    void DeletePlaylist()
    {
        PlaylistHelper.DeletePlaylist(currentEditPlaylistId);
        StateHasChanged();
        deletePlaylistDialog.Hide();
    }
}

<FnzBootstrapModal @ref="deletePlaylistDialog">
    <Title>Delete playlist</Title>
    <Body>
        @currentEditPlaylistName
    </Body>
    <Footer>
        <button type="button" class="btn btn-danger" @onclick="DeletePlaylist">
            Delete
        </button>
    </Footer>
</FnzBootstrapModal>

<FnzBootstrapModal @ref="editPlaylistDialog" CssClass="modal-sm">
    <Title>Modify playlist</Title>
    <Body>
        <div class="row">
            <div class="w-100">
                <div class="form-floating">
                    <InputText class="@EditListNameClass" placeholder="Playlist Name" @oninput="EditPlaylistNameChanged" Value="@currentEditPlaylistName" ValueExpression="()=>currentEditPlaylistName"></InputText>
                    @if (string.IsNullOrEmpty(currentEditPlaylistName))
                    {
                        <label class="text-warning">Please enter playlist name</label>
                    }
                    else
                    {
                        <label>Playlist Name</label>
                    }
                </div>
            </div>
        </div>
    </Body>
    <Footer>
        <button type="button" class="btn btn-primary" @onclick="UpdatePlaylist">
            Save
        </button>
    </Footer>
</FnzBootstrapModal>
