{% 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>
        </ul>
        <p>Note:</p>
        <ul>
            <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>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>Current rescan schedule: <span class="settings-current">
                {% if update_subscribed %}
                    {{ update_subscribed.crontab.minute }} {{ update_subscribed.crontab.hour }} {{ update_subscribed.crontab.day_of_week }}
                    <button data-schedule="update_subscribed" onclick="deleteSchedule(this)" class="danger-button">Delete</button>
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Periodically rescan your subscriptions:</p>
            {% for error in scheduler_form.update_subscribed.errors %}
                <p class="danger-zone">{{ error }}</p>
            {% endfor %}
            {{ scheduler_form.update_subscribed }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Start Download</h2>
        <div class="settings-item">
            <p>Current Download schedule: <span class="settings-current">
                {% if download_pending %}
                    {{ download_pending.crontab.minute }} {{ download_pending.crontab.hour }} {{ download_pending.crontab.day_of_week }}
                    <button data-schedule="download_pending" onclick="deleteSchedule(this)" class="danger-button">Delete</button>
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Automatic video download schedule:</p>
            {% for error in scheduler_form.download_pending.errors %}
                <p class="danger-zone">{{ error }}</p>
            {% endfor %}
            {{ scheduler_form.download_pending }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Refresh Metadata</h2>
        <div class="settings-item">
            <p>Current Metadata refresh schedule: <span class="settings-current">
                {% if check_reindex %}
                    {{ check_reindex.crontab.minute }} {{ check_reindex.crontab.hour }} {{ check_reindex.crontab.day_of_week }}
                    <button data-schedule="check_reindex" onclick="deleteSchedule(this)" class="danger-button">Delete</button>
                {% 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">{{ check_reindex.task_config.days }}</span></p>
            <p>Refresh older than x days, recommended 90:</p>
            {% for error in scheduler_form.check_reindex.errors %}
                <p class="danger-zone">{{ error }}</p>
            {% endfor %}
            {{ scheduler_form.check_reindex_days }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Thumbnail Check</h2>
        <div class="settings-item">
            <p>Current thumbnail check schedule: <span class="settings-current">
                {% if thumbnail_check %}
                    {{ thumbnail_check.crontab.minute }} {{ thumbnail_check.crontab.hour }} {{ thumbnail_check.crontab.day_of_week }}
                    <button data-schedule="thumbnail_check" onclick="deleteSchedule(this)" class="danger-button">Delete</button>
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Periodically check and cleanup thumbnails:</p>
            {% for error in scheduler_form.thumbnail_check.errors %}
                <p class="danger-zone">{{ error }}</p>
            {% endfor %}
            {{ 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 run_backup %}
                    {{ run_backup.crontab.minute }} {{ run_backup.crontab.hour }} {{ run_backup.crontab.day_of_week }}
                    <button data-schedule="run_backup" onclick="deleteSchedule(this)" class="danger-button">Delete</button>
                {% else %}
                    False
                {% endif %}
            </span></p>
            <p>Automatically backup metadata to a zip file:</p>
            {% for error in scheduler_form.run_backup.errors %}
                <p class="danger-zone">{{ error }}</p>
            {% endfor %}
            {{ scheduler_form.run_backup }}
        </div>
        <div class="settings-item">
            <p>Current backup files to keep: <span class="settings-current">{{ run_backup.task_config.rotate }}</span></p>
            <p>Max auto backups to keep:</p>
            {{ scheduler_form.run_backup_rotate }}
        </div>
    </div>
    <div class="settings-group">
        <h2>Add Notification URL</h2>
        <div class="settings-item">
            {% if notifications %}
            <p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
            <div id="text-reveal" class="description-text">
                {% for task, notifications in notifications.items %}
                    <h3>{{ notifications.title }}</h3>
                    {% for url in notifications.urls %}
                        <p>
                            <button type="button" class="danger-button" data-url="{{ url }}" data-task="{{ task }}" onclick="deleteNotificationUrl(this)"> Delete</button>
                            <span> {{ url }}</span>
                        </p>
                    {% endfor %}
                {% endfor %}
            </div>
            {% else %}
                <p>No notifications stored</p>
            {% endif %}
        </div>
        <div class="settings-item">
            <p><i>Send notification on completed tasks with the help of the <a href="https://github.com/caronc/apprise" target="_blank">Apprise</a> library.</i></p>
            {{ notification_form.task }}
            {{ notification_form.notification_url }}
        </div>
    </div>
    <button type="submit" name="scheduler-settings">Update Scheduler Settings</button>
</form>
{% endblock settings_content %}
