{#
/**
 * Copyright (C) 2022 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}

{% extends "connector-form-edit.twig" %}
{% import "forms.twig" as forms %}
{% import "inline.twig" as inline %}

{% block connectorFormFields %}
    {% set services = interface.getAvailableServices() %}
    <h3>Xibo Dashboards<small> - Securely capture and show your data</small></h3>
    <p>The Xibo dashboard service securely stores your credentials, connects to your dashboard provider and
        records your dashboard. It makes that dashboard available to your player via a secure link on a short lease.</p>

    {% if services is not iterable %}
        <p class="alert alert-danger">{{ services }}</p>
    {% endif %}

    {% if not interface.isProviderSetting("apiKey") %}
        <h4>Settings</h4>
        <p>Your API key allows for secure communication between the CMS and the Xibo dashboard service. It is used
            to register your credentials and retrieve dashboards. It is never possible to retrieve credentials.</p>

        {% set title %}{% trans "API Key" %}{% endset %}
        {% set helpText %}{% trans "Enter your API Key from Xibo." %}{% endset %}
        {{ forms.input("apiKey", title, interface.getSetting("apiKey"), helpText) }}
    {% endif %}

    <h4>Credentials</h4>
    {% if services is iterable and services|length > 0 %}
        <p>Select the type of dashboard you want to connect with and enter your credentials below. Credentials are stored
            in our secure dashboard service and not in the CMS or Players. Once you have entered the credentials and this
            form has been accepted, you cannot retrieve them from the CMS.</p>

        <p class="alert alert-info">Please note: changing credentials can take a few minutes after pressing save.</p>

        <table class="table table-striped">
            <thead>
            <tr>
                <th>Type</th>
                <th>Status</th>
                <th>User</th>
                <th></th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            {% for service in interface.getAvailableServices() %}
                {% set credential = interface.getCredentialForType(service.type) %}
                <tr>
                    <input type="hidden" name="{{ service.type }}_id" value="{{ credential.id }}">
                    <td>{{ service.name }}</td>
                    {% if credential %}
                        <td>{% if interface.isCredentialInErrorState(service.type) %}Error{% else %}Connected{% endif %}</td>
                        <td>{{ credential.userName }}</td>
                        <td>
                            {% set title %}{% trans "Change Password" %}{% endset %}
                            {% set helpText %}{{ "Please enter the password you use to connect to %service%"|trans({'%service%': service.name}) }}{% endset %}
                            {{ inline.password((service.type ~ "_password"), title, null, helpText) }}

                            {% set title %}{% trans "Change second factor secret" %}{% endset %}
                            {% set helpText %}{% trans "We support Google Authenticator or similar two factor codes. You get this secret by scanning the QR code." %}{% endset %}
                            {{ inline.input((service.type ~ "_twoFactorSecret"), title, null, helpText) }}
                        </td>
                        <td>{{ inline.checkbox((service.type ~ "_remove"), "Check to remove"|trans)}}</td>
                    {% else %}
                        <td colspan="4">
                            {% set title %}{% trans "Username" %}{% endset %}
                            {% set helpText %}{% trans "Please enter the username you use to connect to your dashboard service." %}{% endset %}
                            {{ inline.input((service.type ~ "_userName"), title, null, helpText) }}

                            {% set title %}{% trans "Password" %}{% endset %}
                            {% set helpText %}{{ "Please enter the password you use to connect to %service%"|trans({'%service%': service.name}) }}{% endset %}
                            {{ inline.password((service.type ~ "_password"), title, null, helpText) }}

                            {% set title %}{% trans "Second factor secret" %}{% endset %}
                            {% set helpText %}{% trans "We support Google Authenticator or similar two factor codes. You get this secret by scanning the QR code." %}{% endset %}
                            {{ inline.input((service.type ~ "_twoFactorSecret"), title, null, helpText) }}

                            {% if service.isUrl %}
                                {% set title %}{% trans "URL" %}{% endset %}
                                {% set helpText %}{{ "The URL where the %service% is hosted"|trans({'%service%':service.name}) }}{% endset %}
                                {{ inline.input((service.type ~ "_url"), title, null, helpText) }}
                            {% endif %}
                        </td>
                    {% endif %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>To see a list of available services please enter your API key, save this form and then come back here.</p>
    {% endif %}

    <h4>Enable/Disable</h4>
    <p>Disabling this connector will stop new dashboards being captured for any existing credentials. If the connector
        is disabled for longer than 30 days, you will need to enter your credentials again.</p>

{% endblock %}