@inherits FnzComponentBase

@code {
    [Parameter, Required] public KeyValuePair<Guid, string>? Playlist { get; set; }
    [Parameter, Required] public int ListItemIndex { get; set; }
    [Parameter, Required] public int ListItemCount { get; set; }

    [Parameter] public EventCallback<Guid> EditPlaylist { get; set; } = EventCallback<Guid>.Empty;
    [Parameter] public EventCallback<Guid> DeletePlaylist { get; set; } = EventCallback<Guid>.Empty;

    private bool IsExpand;
    private PlaylistExportDialog dlgExport;
}

<div class="table-responsive">
    <table class="table w-100">
        <tbody>
            @if (Playlist != null)
            {
                var arr = PlaylistHelper[Playlist.Value.Key];

                <tr class="table-primary">
                    <td style="width:0" @onclick="()=>{IsExpand=!IsExpand;}">
                        <button type="button" class="btn btn-primary p-2">
                            @if (IsExpand)
                            {
                                <i class="bi bi-dash-square"></i>
                            }
                            else
                            {
                                <i class="bi bi-plus-square"></i>
                            }
                        </button>
                    </td>
                    <td class="text-left w-100 align-middle" @onclick="()=>{IsExpand=!IsExpand;}" colspan="3">
                        <div class="text-nowrap">@Playlist.Value.Value</div>
                        <div class="text-nowrap text-muted">
                            (@arr.Count)
                            @arr.Sum(p=>DataSet.AllTracks.Where(q=>q.Path==p).Select(q=>q.Tag?.Duration??0).FirstOrDefault()).SecondToDur()
                            @arr.Sum(p=>DataSet.AllTracks.Where(q=>q.Path==p).Select(q=>q.Tag?.Length??0).FirstOrDefault()).BytesToFileSize()
                        </div>
                    </td>
                    <td>
                        @if (ListItemCount > 1)
                        {
                            var isFirst = ListItemIndex == 0;
                            var isLast = ListItemIndex == ListItemCount - 1;

                            <div class="dropstart">
                                <button type="button" class="btn btn-primary p-2" data-bs-toggle="dropdown">
                                    <i class="bi bi-arrow-down-up"></i>
                                </button>
                                <div class="dropdown-menu text-end p-1 text-nowrap" style="width: initial;min-width: initial;">
                                    <button class="btn btn-primary p-2" disabled="@isFirst" @onclick="()=>PlaylistHelper.ListMoveUp(ListItemIndex)">
                                        <i class="bi bi-arrow-bar-up"></i>
                                        Up
                                    </button>
                                    <button class="btn btn-primary p-2" disabled="@isLast" @onclick="()=>PlaylistHelper.ListMoveDown(ListItemIndex)">
                                        <i class="bi bi-arrow-bar-down"></i>
                                        Down
                                    </button>
                                </div>
                            </div>
                        }
                    </td>
                    <td>
                        <button type="button" class="btn btn-primary p-2" @onclick="()=>DownloadPlaylist(Playlist.Value.Key)">
                            <i class="bi bi-download"></i>
                        </button>
                    </td>
                    <td>
                        <button type="button" class="btn btn-primary p-2" @onclick="()=>EditPlaylist.InvokeAsync(Playlist.Value.Key)">
                            <i class="bi bi-pencil-square"></i>
                        </button>
                    </td>
                    <td>
                        <button type="button" class="btn btn-danger p-2" @onclick="()=>DeletePlaylist.InvokeAsync(Playlist.Value.Key)">
                            <i class="bi bi-trash"></i>
                        </button>
                    </td>
                </tr>

                @if (IsExpand)
                {
                    @foreach (var item in arr.KeepNoEmpty().WithIndex())
                    {
                        if (item.item != null)
                        {
                            var track = DataSet.AllTracks.FirstOrDefault(p => p.Path == item.item);
                            if (track != null)
                            {
                                <PlaylistTableItem PlaylistId="Playlist.Value.Key" TrackIndex="item.index" TrackCount="arr.Count" Track="track"  RemoveClick="()=>Remove(Playlist.Value.Key, item.index,item.item)"></PlaylistTableItem>
                            }
                            else
                            {
                                var uri = new Uri(item.item);
                                var paths = uri.AbsolutePath.Split('/').Skip(2).ToArray();

                                <tr>
                                    <td><img src="blazor-192.png" style="height:50px" /></td>
                                    <td colspan="6" class="align-middle w-100">
                                        <div class="text-nowrap">- broken reference -</div>
                                        @foreach (var p in paths)
                                        {
                                            <span style="display:inline-block" class="text-nowrap text-muted border border-1 m-1 p-1 rounded">@Uri.UnescapeDataString(p)</span>
                                        }
                                    </td>
                                    <td>
                                        <div class="dropstart">
                                            <button type="button" class="btn btn-danger p-2" data-bs-toggle="dropdown">
                                                <i class="bi bi-folder-minus"></i>
                                            </button>
                                            <ul class="dropdown-menu mx-2">
                                                <li><button class="dropdown-item text-danger" @onclick="()=>PlaylistHelper.RemoveTrackInPlaylist(Playlist.Value.Key, item.index,item.item)">Confirm Remove</button></li>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                            }
                        }
                        else
                        {
                            <tr>
                                <td></td>
                                <td colspan="7" class="align-middle w-100">
                                    <div class="text-nowrap">- empty -</div>
                                </td>
                            </tr>
                        }
                    }
                }
            }
        </tbody>
    </table>
</div>

<PlaylistExportDialog @ref="dlgExport">
</PlaylistExportDialog>

@code {
    private void DownloadPlaylist(Guid playlistId)
    {
        dlgExport.Show(playlistId);
    }        
}
