{% extends "base.jinja" %}
{% block main %}
    {% block setup %}
    {% endblock %}
{% endblock %}

{% block scripts %}
    <script>
        let currentStep = parseInt('{{step}}');
        let torSetup = false;

        function toggleHelper() {
            let helper = document.getElementById("helper");
            let helperText = document.getElementById("helper-text");
            let support = document.getElementById("support");
            if (helperText.classList.contains("hidden")) {
                helper.innerText = "Got it!";
                helperText.classList.remove("hidden");
                support.classList.remove("hidden");
                helper.classList.add("helper-btn");
                support.classList.add("helper-btn");
            } else {
                helper.innerText = '{{ _("Unsure? Click here for more info!") }}';
                helperText.classList.add("hidden");
                support.classList.add("hidden");
                helper.classList.remove("helper-btn");
                support.classList.remove("helper-btn");
            }
        }

        function toggleAdvanced() {
            let advancedButton = document.getElementById(`toggle_advanced`);
            let advancedSettings = document.getElementById(`advanced_settings`);
            if (advancedSettings.classList.contains("hidden")) {
                advancedSettings.classList.remove("hidden");
                advancedButton.innerHTML = `{{ _("Advanced") }} &#9660;`;
            } else {
                advancedSettings.classList.add("hidden");
                advancedButton.innerHTML = `{{ _("Advanced") }} &#9654;`;
            }
        }

        function startProgressCheck(software, nextURL) {
            async function fetchProgress() {
                try {
                    let url = "{{ url_for('setup_endpoint.get_software_setup_status', software='SOFTWARE_PLACEHOLDER') }}".replace('SOFTWARE_PLACEHOLDER', software);

                    const response = await fetch(
                        url,
                        {
                            method: 'GET'
                        }
                    );
                    let result = await response.json();
                    if (result.error) {
                        showError(`{{ _("Encountered an error:") }}` + result.error);
                        document.getElementById('progress-details').classList.add('hidden');
                        hidePageOverlay('progress-details');
                        return;
                    }
                    let stage = result.stage;
                    let progress = parseFloat(result.stage_progress);
                    if (progress == -1) {
                        localStorage.setItem("torSetupCompleted", true)
                        window.location.href = nextURL;
                    }
                    setTimeout(fetchProgress, 1000);
                } catch(e) {
                    console.log('Caught error:', e);
                    return { success: false, error: e };
                }
            }
            setTimeout(fetchProgress, 1000);
        }
    </script>
    {% block setup_scripts %}
    {% endblock %}
{% endblock %}
