{% extends 'standingsmanager/_base.html' %}
{% load i18n %}
{% load humanize %}

{% block details %}

    <div class="panel panel-default">
        <div class="panel-heading" style="display:flex;">
            <h3 class="panel-title">{% translate 'Alliance for Sync' %}</h3>
        </div>
        <div class="panel-body">
            {% if alliance %}
                <img class="ra-avatar img-circle" src="{{ alliance.logo_url }}">
                &nbsp;&nbsp;
                {{ alliance.alliance_name }}
                &nbsp;
                (
                {% if alliance_contacts_count != None %}
                    {{ alliance_contacts_count }} contacts
                    {% if alliance_war_targets_count != None %}
                        ,
                    {% endif %}
                {% endif %}
                {% if alliance_war_targets_count != None %}
                    {{ alliance_war_targets_count }} war targets
                {% endif %}
                )
            {% else %}
                <p class="text-muted">Not configured</p>
            {% endif %}
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading" style="display:flex;">
            <h3 class="panel-title">{% translate 'Your Synced Characters' %}</h3>
        </div>
        <div class="panel-body">
            {% if has_synced_chars %}
                <table class="table table-striped" id="tab_standingsmanager">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Organization</th>
                            <th>Status</th>
                            <th>Disable</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for character in synced_characters|dictsort:"name" %}
                            {% if character.errors %}
                                <tr class="danger">
                            {% else %}
                                <tr>
                            {% endif %}
                                    <td>
                                        <img src="{{ character.portrait_url }}" class="ra-avatar img-circle">
                                        &nbsp;
                                        {{ character.name }}
                                    </td>
                                    <td>{{ character.organization }}</td>
                                    <td>
                                        {% if character.errors %}
                                            {% for error in character.errors %}
                                                <span class="text-warning">
                                                    <i class="fas fa-info-circle"></i>
                                                    {{ error }}
                                                </span><br>
                                            {% endfor %}
                                        {% else %}
                                            <i class="fas fa-check text-success"></i>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <a  class="btn btn-danger"
                                            href="{% url 'standingsmanager:remove_character' character.pk %}"
                                            role="button">
                                            <i class="far fa-trash-alt"></i>
                                        </a>
                                    </td>
                                </tr>
                        {% endfor %}
                    </tbody>
                </table>
            {% else %}
                <p class="text-muted">None</p>
            {% endif %}
        </div>
    </div>

    {% if alliance %}
        <a  id="standingsmanagerBtnAddSyncChar"
            class="btn btn-primary"
            href="{% url 'standingsmanager:add_character' %}">
            Add Character to Sync
        </a>
    {% else %}
        <button class="btn btn-primary" disabled="disabled">
            Add Character to Sync
        </button>
    {% endif %}

    <div class="bs-callout bs-callout-warning">
        <h3>Read Me First</h3>
        <p>Once activated this app will automatically update the personal contacts of your synced characters.</p>

        {% if alliance_contacts_count != None %}
            <h4>Alliance Contacts Sync Mode</h4>

            {% if sync_mode == sync_mode_replace %}
                {# Replace Mode #}
                <p>
                    <strong>Mode: REPLACE</strong> - All personal contacts will be replaced with alliance contacts, allowing you to see alliance standings on your synced characters.
                </p>
                <ul>
                    <li><i class="fas fa-sync text-info"></i> Alliance contacts are synced with their actual alliance standing values</li>
                    <li><i class="fas fa-star text-warning"></i> Your alliance is automatically added with +10.0 standing</li>
                </ul>
                <p>
                    <i class="fas fa-exclamation-triangle text-warning"></i> <strong>Warning:</strong> You will lose all personal contacts on your synced characters.
                </p>
                <p>
                    <i class="fas fa-info-circle text-info"></i> <strong>Note:</strong> You can only sync out-of-alliance alts (characters not in the alliance).
                </p>

            {% elif sync_mode == sync_mode_merge %}
                {# Merge Mode #}
                <p>
                    <strong>Mode: MERGE</strong> - Alliance contacts will be automatically synced while preserving your personal contacts.
                </p>
                <ul>
                    <li><i class="fas fa-check text-success"></i> Personal contacts are preserved</li>
                    <li><i class="fas fa-plus text-info"></i> New alliance contacts are added with +5.0 standing</li>
                    <li><i class="fas fa-sync text-info"></i> Existing alliance contacts are updated to +5.0 standing</li>
                    <li><i class="fas fa-minus text-danger"></i> Old alliance contacts are removed when entities leave the alliance</li>
                </ul>
                <p>
                    <i class="fas fa-info-circle text-info"></i> <strong>Required Setup:</strong>
                    Please create a contact label with the name <strong>"{{ alliance_contacts_label_name }}"</strong> on all your synced characters.
                    This label is used to track which contacts are managed by the alliance sync.
                </p>
                <p>
                    <i class="fas fa-lightbulb"></i> <strong>Tip:</strong> All alliance-synced contacts will be automatically marked with the "{{ alliance_contacts_label_name }}" label,
                    making it easy to distinguish them from your personal contacts.
                </p>
                <p>
                    <i class="fas fa-info-circle text-info"></i> <strong>Note:</strong> You can only sync out-of-alliance alts (characters not in the alliance).
                </p>

            {% elif sync_mode == sync_mode_preserve %}
                {# Preserve Mode #}
                <p>
                    <strong>Mode: PRESERVE</strong> - Your personal contacts are completely preserved. Alliance contacts are NOT synced.
                </p>
                <p>
                    <i class="fas fa-info-circle text-info"></i> This mode is useful when you only want to sync war targets without modifying any existing contacts. Only war targets will be synced if war target syncing is enabled.
                </p>

            {% endif %}
        {% endif %}

        {% if alliance_war_targets_count != None %}
            <h4>War Targets</h4>
            <p>Adds current alliance war targets as personal contacts with -10 standing to your synced characters. If the contact already exists it will be overwritten. War targets can be distinguished by having the war targets contact label.</p>
            <p><i class="fas fa-info-circle text-info"></i>
                Please make sure to create a contact label with the name <strong>"{{ war_targets_label_name }}"</strong> on all your synced characters.
            </p>
        {% endif %}
    </div>

{% endblock %}
