@inherits FnzComponentBase

@code {
    private FeTrack PendingAddTrack { get; set; }

    private FnzBoostrapModal dlgMain { get; set; }
    private bool CreatingNewList { get; set; }
    private string NewListName { get; set; }

    private string NewListNameClass => string.IsNullOrWhiteSpace(NewListName) ? "is-invalid form-control" : "form-control";
}

<FnzBoostrapModal @ref="dlgMain" CssClass="modal-md" Keyboard="true">
    <Title>Add to playlist</Title>
    <Body>
        <div class="row w-100 text-center p-2">
            <span>
                <div>
                    @(PendingAddTrack?.GetTitleOrFilename())
                </div>
                <small>
                    @PendingAddTrack?.Tag?.Artist
                    (@PendingAddTrack?.Tag?.Duration.SecondToDur())
                </small>
            </span>
        </div>
        <table class="table w-100">
            <tbody>
                @foreach (var item in PlaylistHelper.PlayListLoadSave.KeepNoEmpty())
                {
                    if (item.Value != null)
                    {
                        var arr = PlaylistHelper[item.Key];
                        var existX = arr.Count(p => p == PendingAddTrack?.Path);

                        <tr class="col">
                            <td class="w-min">
                                <button type="button" class="btn btn-primary p-2" @onclick="()=>AddToPlaylist(item.Key)">
                                    <i class="bi bi-folder-plus"></i>
                                </button>
                            </td>
                            <td class="text-left w-100 align-middle">@item.Value</td>
                            <td class="align-middle text-center">
                                (@arr.Count)
                                <div class="text-nowrap text-muted">@arr.Sum(p=>DataSet.AllTracks?.Where(q=>q?.Path==p).Select(q=>q?.Tag?.Duration??0).FirstOrDefault()).SecondToDur()</div>
                            </td>
                            <td class="align-middle text-nowrap">
                                @if (existX == 1)
                                {
                                    <span>Exist</span>
                                }
                                else if (existX > 1)
                                {
                                    <span>Dup x @existX</span>
                                }
                            </td>
                        </tr>
                    }
                    else
                    {
                        <tr>
                            <td>😒No playlist created</td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </Body>
    <Footer>
        @if (CreatingNewList)
        {
            <div class="form-floating row w-100">
                <InputText class="@NewListNameClass" placeholder="Input new playlist name" @oninput="NewPlaylistNameChanged" Value="@NewListName" ValueExpression="()=>NewListName"></InputText>
                @if (string.IsNullOrEmpty(NewListName))
                {
                    <label class="text-warning">Please enter new playlist name</label>
                }
                else
                {
                    <label>New playlist name</label>
                }
            </div>
            <button type="button" class="btn btn-link text-decoration-none" @onclick="CreateNewPlaylist">
                Create
            </button>
        }
        else
        {
            <div class="w-100">
                <button type="button" class="btn btn-link text-decoration-none" @onclick="ShowCreateNewPlaylist">
                    <i class="bi bi-plus"></i>
                    Create New playlist
                </button>
            </div>
        }
    </Footer>
</FnzBoostrapModal>

@code {
    public void Show(FeTrack track)
    {
        PendingAddTrack = track;
        CreatingNewList = false;
        StateHasChanged();
        dlgMain.Show();
    }

    private void ShowCreateNewPlaylist()
    {
        CreatingNewList = true;
        NewListName = "New Playlist";
        StateHasChanged();
    }

    void NewPlaylistNameChanged(ChangeEventArgs e)
    {
        NewListName = e.Value.ToString().Trim();
        StateHasChanged();
    }

    private void CreateNewPlaylist()
    {
        if (string.IsNullOrWhiteSpace(NewListName) == false)
        {
            PlaylistHelper.CreateNewPlaylist(NewListName);
            CreatingNewList = false;
            StateHasChanged();
        }
    }

    private void AddToPlaylist(Guid playlistId)
    {
        PlaylistHelper.AddTrackToPlaylist(PendingAddTrack.Path, playlistId);
        StateHasChanged();
    }
}
