{% load humanize %}

{% if search_query %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">Playlists <span class="badge bg-primary rounded-pill">{{ playlists.count|default:"0" }}</span></h1>
</div>

<div>
    {% include 'intercooler/playlists.html' %}
</div>

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">Videos <span class="badge bg-primary rounded-pill">{{ videos_count }}</span></h1>
</div>

        <div>
    <div class="row row-cols-1 row-cols-md-3 g-4">
        {% if playlist_items %}
        {% for playlist_item in playlist_items %}
        <div class="col">
            <div class="card" style="background-color: #1A4464;">

            <div class="card-body">
                <h5 class="card-title text-light">
                    {{ playlist_item.video.name|truncatewords:"15" }}<br>

                    <small>
                        <a class="badge bg-white text-black-50" href="{% url 'playlist' playlist_item.playlist.playlist_id %}">{{ playlist_item.playlist.name }}</a>

                        <span class="badge bg-dark text-white-50">{{ playlist_item.video.duration }}</span>
                    </small>
                    {% if playlist_item.video.is_unavailable_on_yt %}<small><span class="badge bg-light text-dark">Private</span></small> {% endif %}
                    {% if playlist_item.video.has_cc %}<small><span class="badge bg-danger text-dark">CC</span></small> {% endif %}
                </h5>
                                                <br>

                <span class="d-flex justify-content-center">
                    <a href="https://www.youtube.com/watch?v={{ playlist_item.video.video_id }}" class="btn btn-info me-1" target="_blank" id="share_link" style=""><i class="fas fa-external-link-alt" aria-hidden="true"></i></a>
                    <input class="form-control me-1 visually-hidden" id="video-{{ video.video_id }}" value="https://www.youtube.com/watch?v={{ playlist_item.video.video_id }}">
                    <button class="copy-btn btn btn-success  me-1" data-clipboard-target="#video-{{ playlist_item.video.video_id }}">
                        <i class="far fa-copy" aria-hidden="true"></i>
                    </button>
                    <button class="btn btn-dark" type="button" hx-get="{% url 'mark_video_favorite' playlist_item.playlist.playlist_id playlist_item.video.video_id %}" hx-target="#video-{{ forloop.counter }}-fav">
                        <div id="video-{{ forloop.counter }}-fav">
                            {% if playlist_item.video.is_favorite %}
                                <i class="fas fa-heart" style="color: #fafa06"></i>
                            {% else %}
                                <i class="far fa-heart"></i>
                            {% endif %}
                        </div>
                    </button>
                </span>
            </div>
            </div>
        </div>
        {% endfor %}

        {% else %}
        <h5 class="text-dark align-content-center">Nothing found :(</h5>
        {% endif %}
    </div>
</div>
{% else %}
{% if all_playlists %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">All Playlists <span class="badge bg-primary rounded-pill">{{ all_playlists.count }}</span></h1>
</div>

<div>
    <div class="row row-cols-1 row-cols-md-3 g-4">
        {% for playlist in all_playlists %}
        <div class="col">
            <div class="card" style="background-color: #515355;">
                <a style="background-color: #1A4464;" href="{% url 'playlist' playlist.playlist_id %}" class="list-group-item list-group-item-action" aria-current="true">

                    <div class="card-body">

                        <h5 class="card-title text-white">
                            {{ playlist.name }}

                        </h5>

                        <p class="card-text">
                            {% if playlist.is_user_owned %}<small><span class="badge bg-light text-black-50">OWNED</span></small>{% else %}<small><span class="badge bg-light text-black-50">IMPORTED</span></small>{% endif %}
                            {% if playlist.is_private_on_yt %}<small><span class="badge bg-secondary text-white">Private</span></small> {% endif %}
                            {% if playlist.is_from_yt %}<small><span class="badge bg-danger text-black-50">YT</span></small> {% endif %}
                            {% if playlist.marked_as == "watching" %}<small><span class="badge bg-primary text-white">WATCHING</span></small>{% endif %}
                        </p>

                        {% if playlist.tags.all %}
                            <p class="card-text">
                            <span class="d-flex justify-content-start flex-wrap">
                            <small>
                            <span style="color: #eed868;" class="me-lg-1 mb-lg-1">
                                <i class="fas fa-tags"></i>
                            </span>
                            </small>
                            {% for tag in playlist.tags.all %}
                                <span class="badge rounded-pill bg-info mb-lg-1 me-lg-1 text-black-50">
                                    {{ tag.name }}
                                </span>
                            {% endfor %}
                            </span>
                            </p>
                        {% endif %}
                        <small>
                            <span class="badge bg-primary rounded-pill">{{ playlist.video_count }} videos</span>
                            <span class="badge bg-primary rounded-pill">{{ playlist.playlist_duration }} </span>
                        </small>
                    </div>
                </a>
            </div>
        </div>
        {% endfor %}
    </div>
</div>
{% endif %}
{% endif %}
<br>
