{% extends 'layout/base.html.twig' %}

{% set i18n_prefix = _self|i18n_prefix %}

{% set vacation_mode_link = app.user.vacationMode ? (i18n_prefix ~ '.vacation_mode_desactivate')|trans : (i18n_prefix ~ '.vacation_mode_activate')|trans %}
{% set vacation_mode_icon = app.user.vacationMode ? 'fa-solid fa-pause' : 'fa-solid fa-play' %}
{% set has_admin_access = is_granted('ROLE_ADMIN') or is_granted('ROLE_GROUP_ADMIN') %}
{% set show_my_groups = has_admin_access %}

{% if canCreateGroup %}
    {% set create_group_label = (i18n_prefix ~ '.create_group')|trans %}
    {% set create_group_icon = null %}
{% else %}
    {% set create_group_label = (i18n_prefix ~ '.creation_request')|trans %}
    {% set create_group_icon = 'fa-regular fa-envelope' %}
{% endif %}

{% set my_account_links = [
    {
        section: 'Messagerie',
        enable: true,
        links: [
        {
            name: 'Mes emprunts',
            link: 'app_user_my_loans',
            icon: null,
            notification: userHasNewLoanMessage
        },
        {
            name: 'Mes prêts',
            link: 'app_user_my_lendings',
            icon: null,
            notification: userHasNewLendingMessage
        },
    ],
        icon: 'bi bi-chat-left-text'
    },
    {
        section: 'Mes objets',
        enable: true,
        links: [
        {
            name: 'Voir mes objets',
            link: 'app_user_objects',
            icon: null
        },
        {
            name: 'Créer un objet',
            link: 'app_object_new',
            icon: null,
            needAddress: app.user.address is null ? true : false
        },
    ],
        icon: 'fa-solid fa-shop'
    },
    {
        section: 'Mes services',
        enable: servicesConfig,
        links: [
        {
            name: 'Voir mes services',
            link: 'app_user_services',
            icon: null
        },
        {
            name: 'Créer un service',
            link: 'app_service_new',
            icon: null,
            needAddress: app.user.address is null ? true : false
        },
    ],
        icon: 'fa-solid fa-shop'
    },
    {
        section: 'Mes groupes',
        enable: true,
        links: [
        {
            name: 'Voir mes groupes',
            link: 'app_user_groups',
            icon: null
        },
        {
            name: 'Administrer mes groupes',
            link: 'admin',
            show: show_my_groups,
            icon: 'bi bi-box-arrow-up-right'
        },
        {
            name: create_group_label,
            link: 'app_group_create',
            icon: create_group_icon,
            canCreateGroup: canCreateGroup,
        },
    ],
        icon: 'fa-solid fa-user-group'
    },
    {
        section: 'Compte',
        enable: true,
        links: [
        {
            name: 'Mon adresse',
            link: 'user_address_step1',
            icon: null
        },
        {
            name: 'Modifier mon profil',
            link: 'app_user_edit_profile',
            icon: null
        },
        {
            name: 'Changer mon adresse e-mail',
            link: 'app_user_change_login',
            icon: null
        },
        {
            name: 'Changer mon mot de passe',
            link: 'app_user_change_password',
            icon: null
        },
        {
            name: vacation_mode_link,
            link: 'user_toggle_vacation_mode',
            icon: vacation_mode_icon
        },
    ],
        icon: 'fa-solid fa-user fa-xl text-white'
    },
] %}

{% block app_name %}{% endblock %}

{% block body %}
    <div class="px-3">
        {% include 'components/layout/_title_3.html.twig' with {
            name: (i18n_prefix ~ '.my_account')|trans
        } %}
        <div class="row flex-wrap">
            {% for my_account_link in my_account_links %}
                {% if my_account_link.enable %}
                    <div class="col-12 col-md-4">
                        <nav class="myAccount d-flex flex-column mt-3 mt-md-5">
                            <div class="myAccount-header w-100 rounded-2">
                                <i class="{{ my_account_link.icon }}"></i>
                                <h5 class="mb-1 ms-3 fw-bolder fs-5 text-black">{{ my_account_link.section }}</h5>
                            </div>
                            <div class="myAccount-body pb-4">
                                {% for link in my_account_link.links %}
                                    {% if link.show ?? true %}
                                        <div class="mt-3 d-flex align-items-center">
                                            {% if link.canCreateGroup is defined and not link.canCreateGroup %}
                                                <a
                                                    href="mailto:{{ contactEmail ~ "?Subject=" ~ (i18n_prefix ~ '.mail.subject')|trans ~ "&body=" ~ (i18n_prefix ~ '.mail.mail_adress')|trans ~ "%0A" ~ (i18n_prefix ~ '.mail.group')|trans ~ "%0A" ~ (i18n_prefix ~ '.mail.type')|trans ~ "%0A" ~ (i18n_prefix ~ '.mail.membership')|trans ~ "%0A" ~ (i18n_prefix ~ '.mail.info')|trans }}"
                                                    class="text-decoration-none text-primary ms-2">
                                                    {% if link.icon is not empty %}
                                                        <i class="{{ link.icon }} ~ me-1"></i>
                                                    {% endif %}
                                                    {{ link.name }}
                                                </a>
                                            {% else %}
                                                {% if link.needAddress is defined and link.needAddress == true %}
                                                    {% include 'components/product/_modal.html.twig' with {
                                                        menu_action: true,
                                                        button: link.name,
                                                        title: (i18n_prefix ~ '.no-address-title')|trans,
                                                        message: (i18n_prefix ~ '.no-address-message')|trans({
                                                            '%product%': link.link == 'app_object_new' ? 'objet' : 'service'
                                                        }),
                                                        action: (i18n_prefix ~ '.no-address-add')|trans
                                                    } %}
                                                {% else %}
                                                    <a href="{{ path(link.link) }}"
                                                       class="text-decoration-none text-primary ms-2 position-relative pe-1">
                                                        {% if link.icon is not empty %}
                                                            <i class="{{ link.icon }} ~ me-1"></i>
                                                        {% endif %}
                                                        {{ link.name }}
                                                        {% if link.notification is defined and link.notification %}
                                                            <span
                                                                class="position-absolute top-0 start-100 translate-middle p-1 rounded-circle bg-danger"></span>
                                                        {% endif %}
                                                    </a>
                                                {% endif %}
                                            {% endif %}
                                        </div>
                                    {% endif %}
                                {% endfor %}
                            </div>
                        </nav>
                    </div>
                {% endif %}
            {% endfor %}
        </div>
        <div class="d-grid col-12 col-md-6 mx-auto mt-5">
            <a href="{{ path('app_logout') }}" class="btn btn-outline-primary btn-sm">
                <i class="fa-solid fa-right-from-bracket"></i>
                {{ (i18n_prefix ~ '.disconnect')|trans }}
            </a>
        </div>
    </div>
{% endblock %}

{% block link %}{% endblock %}

{# TODO: Traduction + Link + icons #}
