@inherits FnzComponentBase

@code {
    [Parameter, Required] public KeyValuePair<Guid, string>? Playlist { 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;
}

<table class="table w-100">
    <tbody>

        @if (Playlist != null)
        {
            var arr = PlaylistHelper[Playlist.Value.Key];

            <tr>
                <td class="w-0" 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="2">
                    <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>
                    <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 class="align-middle text-nowrap">
                    <button type="button" class="btn btn-primary 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)
                        {
                            <tr>
                                <td colspan="2" style="width:0">
                                    <img src="@track.Disc.CoverPath" style="height:50px" />
                                </td>
                                <td class="align-middle w-100">
                                    <div class="text-nowrap">
                                        <FileIcon FileName="@track.Name"></FileIcon>
                                        @((track.Tag?.Title).NullOrEmptyEscape(track.Name))
                                    </div>
                                    <div class="text-nowrap text-muted"> @track.Tag?.Artist</div>
                                </td>
                                <td class="text-center" colspan="2">
                                    <div class="text-nowrap">@track.Tag?.Duration.SecondToDur()</div>
                                    <div class="text-nowrap text-muted"> @track.Tag?.Length.BytesToFileSize()</div>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-primary p-2" @onclick="()=>Remove(Playlist.Value.Key, item.index,item.item)">
                                        <i class="bi bi-folder-minus"></i>
                                    </button>
                                </td>
                            </tr>
                        }
                        else
                        {
                            var uri = new Uri(item.item);
                            var paths = uri.AbsolutePath.Split('/').Skip(2).ToArray();
                            <tr>
                                <td></td>
                                <td><img src="blazor-192.png" style="height:50px" /></td>
                                <td class="align-middle w-100">
                                    <div class="text-nowrap">- broken reference -</div>
                                    @foreach (var p in paths)
                                    {
                                        <div class="text-nowrap text-muted">@Uri.UnescapeDataString(p)</div>
                                    }
                                </td>
                                <td></td>
                                <td></td>
                                <td>
                                    <button type="button" class="btn btn-primary p-2" @onclick="()=>Remove(Playlist.Value.Key, item.index,item.item)">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </td>
                                <td></td>
                                <td></td>
                            </tr>
                        }
                    }
                    else
                    {
                        <tr>
                            <td></td>
                            <td></td>
                            <td class="align-middle w-100">
                                <div class="text-nowrap">- empty -</div>
                            </td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    }
                }
            }
        }
    </tbody>
</table>

@code {
    private void DownloadPlaylist(Guid playlistId)
    {
        // generate m3u content
        var sb = new StringBuilder();
        sb.WriteM3U8Header();

        foreach (var item in PlaylistHelper[playlistId])
        {
            var track = DataSet.AllTracks.FirstOrDefault(p => p.Path == item);
            if (track == null) continue;

            sb.AppendLine($"#EXTINF:{track.Tag?.Duration ?? 0} logo=\"{track.Disc.CoverPath.UrlEscape()}\",{track.Tag?.Artist} - {track.Tag?.Title ?? track.Name}");
            sb.AppendLine(track.Path.UrlEscape());
        }

        // export
        JSRuntime.InvokeVoidAsync("fnz.downloadFile", $"{PlaylistHelper.PlayListLoadSave[playlistId]}.m3u8", "text/mpegurl", sb.ToString());
    }

    private void Remove(Guid playlistId, int index, string path)
    {
        PlaylistHelper.DeleteTrackInPlaylist(playlistId, index, path);
        StateHasChanged();
    }
}
