<% title = HTML.escape(playlist.title) %>
<% author = HTML.escape(playlist.author) %>

<% content_for "header" do %>
<title><%= title %> - Invidious</title>
<link rel="alternate" type="application/rss+xml" title="RSS" href="/feed/playlist/<%= plid %>" />
<% end %>

<div class="h-box flexible title">
    <div class="flex-left"><h3><%= title %></h3></div>

    <div class="flex-right button-container">
        <%- if playlist.is_a?(InvidiousPlaylist) && playlist.author == user.try &.email -%>
            <div class="pure-u">
                <a class="pure-button pure-button-secondary low-profile" dir="auto" href="/add_playlist_items?list=<%= plid %>">
                    <i class="icon ion-md-add"></i>&nbsp;<%= translate(locale, "playlist_button_add_items") %>
                </a>
            </div>
            <div class="pure-u">
                <a class="pure-button pure-button-secondary low-profile" dir="auto" href="/edit_playlist?list=<%= plid %>">
                    <i class="icon ion-md-create"></i>&nbsp;<%= translate(locale, "generic_button_edit") %>
                </a>
            </div>
            <div class="pure-u">
                <a class="pure-button pure-button-secondary low-profile" dir="auto" href="/delete_playlist?list=<%= plid %>">
                    <i class="icon ion-md-trash"></i>&nbsp;<%= translate(locale, "generic_button_delete") %>
                </a>
            </div>
        <%- else -%>
            <div class="pure-u">
                <%- if IV::Database::Playlists.exists?(playlist.id) -%>
                    <a class="pure-button pure-button-secondary low-profile" dir="auto" href="/subscribe_playlist?list=<%= plid %>">
                        <i class="icon ion-md-add"></i>&nbsp;<%= translate(locale, "Subscribe") %>
                    </a>
                <%- else -%>
                    <a class="pure-button pure-button-secondary low-profile" dir="auto" href="/delete_playlist?list=<%= plid %>">
                        <i class="icon ion-md-trash"></i>&nbsp;<%= translate(locale, "Unsubscribe") %>
                    </a>
                <%- end -%>
            </div>
        <%- end -%>

        <div class="pure-u">
            <a class="pure-button pure-button-secondary low-profile" dir="auto" href="/feed/playlist/<%= plid %>">
                <i class="icon ion-logo-rss"></i>&nbsp;<%= translate(locale, "generic_button_rss") %>
            </a>
        </div>
    </div>
</div>

<div class="h-box">
    <div class="pure-u-1-1">
        <% if playlist.is_a? InvidiousPlaylist %>
            <b>
                <% if playlist.author == user.try &.email %>
                <a href="/feed/playlists"><%= author %></a> |
                <% else %>
                <%= author %> |
                <% end %>
                <%= translate_count(locale, "generic_videos_count", playlist.video_count) %> |
                <%= translate(locale, "Updated `x` ago", recode_date(playlist.updated, locale)) %> |
                <% case playlist.as(InvidiousPlaylist).privacy when %>
                <% when PlaylistPrivacy::Public %>
                    <i class="icon ion-md-globe"></i> <%= translate(locale, "Public") %>
                <% when PlaylistPrivacy::Unlisted %>
                    <i class="icon ion-ios-unlock"></i> <%= translate(locale, "Unlisted") %>
                <% when PlaylistPrivacy::Private %>
                    <i class="icon ion-ios-lock"></i> <%= translate(locale, "Private") %>
                <% end %>
            </b>
        <% else %>
            <b>
                <% if !author.empty? %>
                    <a href="/channel/<%= playlist.ucid %>"><%= author %></a> |
                <% elsif !playlist.subtitle.nil? %>
                    <% subtitle = playlist.subtitle || "" %>
                    <span><%= HTML.escape(subtitle[0..subtitle.rindex(" â€¢ ") || subtitle.size]) %></span> |
                <% end %>
                <%= translate_count(locale, "generic_videos_count", playlist.video_count) %> |
                <%= translate(locale, "Updated `x` ago", recode_date(playlist.updated, locale)) %>
            </b>
        <% end %>

        <% if !playlist.is_a? InvidiousPlaylist %>
            <div class="pure-u-2-3">
                    <a rel="noreferrer noopener" href="https://www.youtube.com/playlist?list=<%= playlist.id %>">
                        <%= translate(locale, "View playlist on YouTube") %>
                    </a>
                    <span> | </span>

                    <% if env.get("preferences").as(Preferences).automatic_instance_redirect%>
                        <a href="/redirect?referer=<%= env.get?("current_page") %>">
                            <%= translate(locale, "Switch Invidious Instance") %>
                        </a>
                    <% else %>
                        <a href="https://redirect.invidious.io/playlist?list=<%= playlist.id %>">
                            <%= translate(locale, "Switch Invidious Instance") %>
                        </a>
                    <% end %>
            </div>
        <% end %>
    </div>
</div>

<div class="h-box">
    <div id="descriptionWrapper"><%= playlist.description_html %></div>
</div>

<div class="h-box">
    <hr>
</div>

<% if playlist.is_a?(InvidiousPlaylist) && playlist.author == user.try &.email %>
<script id="playlist_data" type="application/json">
<%=
{
    "csrf_token" => URI.encode_www_form(env.get?("csrf_token").try &.as(String) || "")
}.to_pretty_json
%>
</script>
<script src="/js/playlist_widget.js?v=<%= ASSET_COMMIT %>"></script>
<% end %>


<%= rendered "components/items_paginated" %>
