{# Learn how to use Turbo Streams: https://github.com/symfony/ux-turbo#broadcast-doctrine-entities-update #}
{% block create %}
    <turbo-stream action="remove" target="active_downloads_no_downloads">
    </turbo-stream>

    <turbo-stream action="append" target="active_downloads">
        <template>
            <twig:DownloadListRow download="{{ entity }}" />
        </template>
    </turbo-stream>
{% endblock %}

{% block update %}
    {% if entity.status != "Complete" %}
        <turbo-stream action="update" target="download_progress_{{ id }}">
            <template>
                {% if entity.progress >= 50 %}
                    {% set text_color = "text-black dark:text-black" %}
                {% else %}
                    {% set text_color = "text-black dark:text-white" %}
                {% endif %}
                <div class="background {{ text_color }} text-center rounded-sm text-bold bg-green-300 h-5 relative z-10"
                     style="width: {{ entity.progress }}%">
                </div>
                <div class="number {{ text_color }} font-bold text-center z-40"
                >{{ entity.progress }}%</div>
            </template>
        </turbo-stream>
        <turbo-stream action="update" target="action_buttons_{{ id }}">
            <template>
                {% if entity.status == "In Progress" and entity.progress < 100 %}
                    <button id="pause_{{ entity.id }}" class="text-orange-500 mr-1 self-start" {{ stimulus_action('download_list', 'pauseDownload', 'click', {id: entity.id}) }}>
                        <twig:ux:icon name="icon-park-twotone:pause-one" width="16.75px" height="16.75px" class="rounded-full" />
                    </button>
                {% elseif entity.status == "Paused" %}
                    <button id="resume_{{ entity.id }}" class="text-orange-500 mr-1 self-start" {{ stimulus_action('download_list', 'resumeDownload', 'click', {id: entity.id}) }}>
                        <twig:ux:icon name="icon-park-twotone:play" width="16.75px" height="16.75px" class="rounded-full" />
                    </button>
                {% endif %}
                {% set delete_button = component('ux:icon', {name: 'ic:twotone-cancel', width: '17.5px', class: 'rounded-full align-middle text-red-600' }) %}
                <twig:Modal heading="But wait!" button_text="{{ delete_button }}" submit_action="{{ stimulus_action('download_list', 'deleteDownload', 'click', {id: entity.id}) }}" show_cancel show_submit>
                    Are you sure you want to delete <span class="font-bold">{{ entity.filename }}</span>?
                </twig:Modal>
            </template>
        </turbo-stream>
    {% else %}
        <turbo-stream action="remove" target="complete_downloads_no_downloads">
        </turbo-stream>

        <turbo-stream action="remove" target="ad_download_{{ id }}">
        </turbo-stream>

        <turbo-stream action="remove" target="action_buttons_{{ id }}">
        </turbo-stream>

        <turbo-stream action="prepend" target="alert_list">
            <template>
                {% if entity.mediaType == "tvshows" %}
                <twig:Alert title="Success" message="{{ entity.title }} - ({{ entity.episodeId }}) has finished downloading." alert_id="{{ entity.id }}" data-controller="alert" />
                {% else %}
                <twig:Alert title="Success" message="{{ entity.title }} has finished downloading." alert_id="{{ entity.id }}" data-controller="alert" />
                {% endif %}
            </template>
        </turbo-stream>

        <turbo-stream action="prepend" target="complete_downloads">
            <template>
                <twig:DownloadListRow download="{{ entity }}" />
            </template>
        </turbo-stream>
    {% endif %}
{% endblock %}

{% block remove %}
    <turbo-stream action="remove" target="ad_download_{{ id }}"></turbo-stream>
{% endblock %}
