{% extends "home/base_settings.html" %}
{% load static %}
{% block settings_content %}
<div class="title-bar">
    <h1>Scheduler Setup</h1>
    <div class="settings-group">
        <p>Schedule settings expect a cron like format, where the first value is minute, second is hour and third is day of the week.</p>
        <p>Examples:</p>
        <ul>
            <li><span class="settings-current">0 15 *</span>: Run task every day at 15:00 in the afternoon.</li>
            <li><span class="settings-current">30 8 */2</span>: Run task every second day of the week (Sun, Tue, Thu, Sat) at 08:30 in the morning.</li>
            <li><span class="settings-current">auto</span>: Sensible default.</li>
            <li><span class="settings-current">0</span>: (zero), deactivate that task.</li>
        </ul>
        <p>Note:</p>
        <ul>
            <li>Changes in the scheduler settings require a container restart to take effect.</li>
            <li>Avoid an unnecessary frequent schedule to not get blocked by YouTube. For that reason, the scheduler doesn't support schedules that trigger more than once per hour.</li>
        </ul>
    </div>
</div>
<form action="{% url 'settings_scheduling' %}" method="POST" name="scheduler-update">
    {% csrf_token %}
    <div class="settings-group">
        <h2>Rescan Subscriptions</h2>
        <div class="settings-item">
            <p>Current rescan schedule: <span class="settings-current">
                {% if config.scheduler.update_subscribed %}
                    {% for key, value in config.scheduler.update_subscribed.items %}
                        {{ value }}
                    {% endfor %}
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Become a sponsor and join <a href="https://members.tubearchivist.com/" target="_blank">members.tubearchivist.com</a> to get access to <span class="settings-current">real time</span> notifications for new videos uploaded by your favorite channels.</p>
            <p>Periodically rescan your subscriptions:</p>
            {{ scheduler_form.update_subscribed }}
        </div>
        <div class="settings-item">
            <p>Send notification on task completed:</p>
            {% if config.scheduler.update_subscribed_notify %}
                <p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
                <div id="text-reveal" class="description-text">
                    <p>{{ config.scheduler.update_subscribed_notify|linebreaks }}</p>
                </div>
            {% else %}
                <p>Current notification urls: <span class="settings-current">{{ config.scheduler.update_subscribed_notify }}</span></p>
            {% endif %}
            {{ scheduler_form.update_subscribed_notify }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Start download</h2>
        <div class="settings-item">
            <p>Current Download schedule: <span class="settings-current">
                {% if config.scheduler.download_pending %}
                    {% for key, value in config.scheduler.download_pending.items %}
                        {{ value }}
                    {% endfor %}
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Automatic video download schedule:</p>
            {{ scheduler_form.download_pending }}
        </div>
        <div class="settings-item">
            <p>Send notification on task completed:</p>
            {% if config.scheduler.download_pending_notify %}
                <p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
                <div id="text-reveal" class="description-text">
                    <p>{{ config.scheduler.download_pending_notify|linebreaks }}</p>
                </div>
            {% else %}
                <p>Current notification urls: <span class="settings-current">{{ config.scheduler.download_pending_notify }}</span></p>
            {% endif %}
            {{ scheduler_form.download_pending_notify }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Refresh Metadata</h2>
        <div class="settings-item">
            <p>Current Metadata refresh schedule: <span class="settings-current">
                {% if config.scheduler.check_reindex %}
                    {% for key, value in config.scheduler.check_reindex.items %}
                        {{ value }}
                    {% endfor %}
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Daily schedule to refresh metadata from YouTube:</p>
            {{ scheduler_form.check_reindex }}
        </div>
        <div class="settings-item">
            <p>Current refresh for metadata older than x days: <span class="settings-current">{{ config.scheduler.check_reindex_days }}</span></p>
            <p>Refresh older than x days, recommended 90:</p>
            {{ scheduler_form.check_reindex_days }}
        </div>
        <div class="settings-item">
            <p>Send notification on task completed:</p>
            {% if config.scheduler.check_reindex_notify %}
                <p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
                <div id="text-reveal" class="description-text">
                    <p>{{ config.scheduler.check_reindex_notify|linebreaks }}</p>
                </div>
            {% else %}
                <p>Current notification urls: <span class="settings-current">{{ config.scheduler.check_reindex_notify }}</span></p>
            {% endif %}
            {{ scheduler_form.check_reindex_notify }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Thumbnail check</h2>
        <div class="settings-item">
            <p>Current thumbnail check schedule: <span class="settings-current">
                {% if config.scheduler.thumbnail_check %}
                    {% for key, value in config.scheduler.thumbnail_check.items %}
                        {{ value }}
                    {% endfor %}
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Periodically check and cleanup thumbnails:</p>
            {{ scheduler_form.thumbnail_check }}
        </div>
    </div>
    <div class="settings-group">
        <h2>ZIP file index backup</h2>
        <div class="settings-item">
            <p><i>Zip file backups are very slow for large archives and consistency is not guaranteed, use snapshots instead. Make sure no other tasks are running when creating a Zip file backup.</i></p>
            <p>Current index backup schedule: <span class="settings-current">
                {% if config.scheduler.run_backup %}
                    {% for key, value in config.scheduler.run_backup.items %}
                        {{ value }}
                    {% endfor %}
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Automatically backup metadata to a zip file:</p>
            {{ scheduler_form.run_backup }}
        </div>
        <div class="settings-item">
            <p>Current backup files to keep: <span class="settings-current">{{ config.scheduler.run_backup_rotate }}</span></p>
            <p>Max auto backups to keep:</p>
            {{ scheduler_form.run_backup_rotate }}
        </div>
    </div>
    <button type="submit" name="scheduler-settings">Update Scheduler Settings</button>
</form>
{% endblock settings_content %}
