@inherits FnzComponentBase

@code {
    private FnzBoostrapModal mdl;
    private string ExportName { get; set; } = "brrr";
    private string ExportExtension { get; set; } = "m3u";
    private string ExportMime { get; set; } = "text/mpegurl";
    private Guid PlaylistId { get; set; }
}

<FnzBoostrapModal @ref="mdl">
    <Title>Export playlist</Title>
    <Body>
        <div class="input-group mb-3">
            <span class="input-group-text">File Name</span>
            <InputText class="form-control" placeholder="Filename" ValueExpression="()=>ExportName" Value="@ExportName"></InputText>
        </div>
        <div class="input-group mb-3">
            <span class="input-group-text">Extension</span>
            <InputText type="text" class="form-control" placeholder="Extension" ValueExpression="()=>ExportExtension" Value="@ExportExtension"></InputText>
            <span class="input-group-text">Mime</span>
            <InputText type="text" class="form-control" placeholder="Extension" ValueExpression="()=>ExportMime" Value="@ExportMime"></InputText>
        </div>
    </Body>
    <Footer>
        <button class="btn btn-primary" @onclick="DoExport">Save</button>
    </Footer>
</FnzBoostrapModal>

@code {
    public void Show(Guid playlistId)
    {
        var playlist = PlaylistHelper.PlayListLoadSave[playlistId];
        var uri = new Uri(NavigationManager.Uri);
        ExportName = $"{playlist}-{uri.Host}-{DateTime.Now:yyMMdd}";

        PlaylistId = playlistId;

        StateHasChanged();
        mdl.Show();
    }

    public void DoExport()
    {
        // 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", $"{ExportName}.{ExportExtension}", ExportMime, sb.ToString());
        mdl.Hide();
    }
}
