{% load waffle_tags %}
{% load i18n %}

{
    "training": {

        "id": "{{ training.id }}",
        "name": "{{ training.name|escapejs }}",
        "purpose": "{{ training.description|escapejs }}",
        "image": "{{ training.image.get_filename }}",
        "last_update": "{{ training.version }}",
        "session_id": "{{ current_session.id }}",
        "token": "{{ current_session.token }}",

        "header_image": "{{ training.get_header_image.url }}",
        "header_image_hash": "{{ training.get_header_image_hash }}",

        "home_diffusion_image": "{{ training.theme.home_diffusion_image.url }}",
        "home_diffusion_image_hash": "{{ training.theme.home_diffusion_image_hash }}",

        "url_message_broker": "{{ message_broker_settings.url }}",
        "port_message_broker": "{{ message_broker_settings.port }}",

        "items": {

            "document": {{ documents_json|safe }},

            "workshop": {{ workshops_json|safe }},

            {% if survey_json %}
            "survey": {{ survey_json|safe }},
            {% endif %}

            {% if metaplans or training.options.activate_activity_quickcreation %}
                "plan_meta": [
                    {% if training.options.activate_activity_quickcreation %}
                        {
                            "id": "nil",
                            "title": "{% trans 'New Metaplan' %}",
                            "preview": "metaplan-preview.png",
                            "module": "plan_meta",
                            "groups": [
                                "trainer"
                            ],
                            "order":101,
                            "items": [
                            ]
                        }{% if metaplans %},{% endif %}
                    {% endif %}
                    {% for metaplan in metaplans %}
                        {
                            "id": "{{ metaplan.id }}",
                            "title": "{{ metaplan.name|escapejs }}",
                            "preview": "metaplan-preview.png",
                            "module": "plan_meta",
                            "groups": [
                                "trainer"
                            ],
                            "order": {{ metaplan.sort_order }},
                            "items": [
                                {% for proposition in metaplan.propositions.all %}
                                {
                                    "msg": "{{ proposition.text|escapejs }}",
                                    "user_id": "{{ proposition.user.id }}",
                                    "type": {{ proposition.group }}
                                }{% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ]
                        }
                        {% if not forloop.last %},{% endif %}
                    {% endfor %}
                ],
            {% endif %}
            {% if training.options.activate_activity_quickcreation or brainstormers %}
                "brainstormer": [
                    {% if training.options.activate_activity_quickcreation %}
                    {
                        "id": "nil",
                        "title": "{% trans 'New Brainstormer' %}",
                        "preview": "brainstormer-preview.png",
                        "module": "brainstormer",
                        "groups": [
                            "trainer"
                        ],
                        "order":101,
                        "activity": "",
                        "activities": [
                            {% for activity in activities %}
                              {
                                  "id": "{{ activity.id }}",
                                  "title": "{{ activity.name|escapejs }}",
                                  "preview": "{% if activity.preview %}{{ MEDIA_URL }}{{ activity.preview }}{% endif %}",
                                  "image": "{% if activity.image %}{{ MEDIA_URL }}{{ activity.image }}{% endif %}",
                                  "hash_preview": "{{ activity.hash_preview }}",
                                  "hash_image": "{{ activity.hash_image }}"
                              }
                              {% if not forloop.last %},{% endif %}
                            {% endfor %}
                        ]
                    }{% if brainstormers %},{% endif %}
                    {% endif %}

                    {% for brainstormer in brainstormers %}
                        {
                            "id": "{{ brainstormer.id }}",
                            "title": "{{ brainstormer.name|escapejs }}",
                            "brainstormer_type": "{{ brainstormer.brainstormer_type }}",
                            "preview": "brainstormer-preview.png",
                            "module": "brainstormer",
                            "groups": [
                                "trainer",
                                "trainee"
                            ],
                            "order": {{ brainstormer.sort_order }},
                            "activity": "{{ brainstormer.activity.id }}",
                            "activities": [
                                {% for activity in activities %}
                                  {
                                      "id": "{{ activity.id }}",
                                      "title": "{{ activity.name|escapejs }}",
                                      "preview": "{% if activity.preview %}{{ MEDIA_URL }}{{ activity.preview }}{% endif %}",
                                      "image": "{% if activity.image %}{{ MEDIA_URL }}{{ activity.image }}{% endif %}",
                                      "hash_preview": "{{ activity.hash_preview }}",
                                      "hash_image": "{{ activity.hash_image }}"
                                  }
                                  {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "proposals": [
                                {% for proposition in brainstormer.get_propositions %}
                                  {
                                      "id": "{{ proposition.id }}",
                                      "text": "{{ proposition.text|escapejs }}",
                                      "user": "{{ proposition.user.id }}",
                                      "group": "{{ proposition.group.id }}"
                                  }
                                  {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "proposal_groups" : [
                                {% for group in brainstormer.get_groups %}
                                  {
                                      "id": "{{ group.id }}",
                                      "name": "{{ group.name|escapejs }}",
                                      "color": "{{ group.color }}",
                                      "x": {{ group.coord_x|safe }},
                                      "y": {{ group.coord_y|safe }},
                                      "order": {{ group.sort_order }}
                                  }
                                  {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ]
                        }{% if not forloop.last %},{% endif %}
                    {% endfor %}
                ],
            {% endif %}

            "background_execution": [
                {% for bg_task in bg_tasks %}
                    {
                        "id": "{{ bg_task.id }}",
                        "authorization": {{ bg_task.mgroups.all }},
                        "begin_date": {{ bg_task.get_begin_date }},
                        "end_date": {{ bg_task.get_end_date }},
                        "execution": {
                            "action": "{{ bg_task.get_action }}",
                            "params": {{ bg_task.get_param|escapejs }}
                        }
                    },
                {% endfor %}
            ],
            {% if training.options.activate_activity_quickcreation %}
                "buzzer": [
                    {
                        "id": "2cc",
                        "title": "{% trans 'New Buzzer' %}",
                        "module": "buzzer",
                        "groups": [
                            "trainer"
                        ],
                        "preview": "buzzer-preview.png",
                        "order": 101
                    }
                ],
            {% endif %}

            "who_is_who": [
                {
                    "id": "2cc",
                    "title": "{% trans "Show Who's who" %}",
                    "module": "who_is_who",
                    "groups": [
                        "trainer",
                        "trainee"
                    ],
                    "preview": "whoswho-preview.png",
                    "order": 0
                }
            ],

            {% switch enable_photostory %}
            {% if training.options.activate_activity_quickcreation %}
                "photo_story": [
                    {
                        "id": "7gg",
                        "title": "{% trans 'New Photo Story' %}",
                        "module": "photo_story",
                        "groups": [
                            "trainer",
                            "trainee"
                        ],
                        "preview": "photostory-preview.png",
                        "order": 101
                    }
                ],
            {% endif %}
            {% endswitch %}

            "action": [
                {% for action in actions %}
                    {
                        "id": "{{ action.id }}",
                        "groups": [
                            {% for group in action.mgroups.all %}
                                "{{ group.name }}"
                                {% if not forloop.last %},{% endif %}
                            {% endfor %}
                        ],
                        "action": "{{ action.action }}",
                        "params": {
                            "id": "{{ action.module_id }}",
                            "module": "{{ action.module }}"
                        },
                        "external_id": "{{ action.external_id }}"
                    },
                {% endfor %}
            ]

            {% switch enable_roleplay %}
            ,
            "roleplay": [
                {% for roleplay in roleplays %}
                    {
                        "title": "{{ roleplay.name|escapejs }}",
                        "id": "{{ roleplay.id }}",
                        "original_version": "{{ roleplay.original_version_id|default_if_none:"" }}",
                        "groups": [
                            "trainer"
                        ],
                        "preview": "roleplay-preview.png",
                        "order": {{roleplay.sort_order}},
                        "module": "roleplay"
                    }
                    {% if not forloop.last %},{% endif %}
                {% endfor %}
            ]
            {% endswitch %}

            {% switch enable_assessments %}
            {% if training.options.activate_show_results %}
            ,
            "assessment_debrief": [
                {
                    "id": "2cc",
                    "title": "{% trans 'Show results' %}",
                    "module": "assessment_debrief",
                    "groups": [
                        "trainer"
                    ],
                    "preview": "assessment-debrief-preview.png",
                    "order": 0
                }
            ]
            {% endif %}
            {% endswitch %}

            {% switch enable_evaluation %}
            ,
            "spider_graph" : [
                {% for evaluation in evaluations %}
                    {
                        "id": "{{ evaluation.id }}",
                        "original_version": "{{ evaluation.original_version_id|default_if_none:"" }}",
                        "title": "{{ evaluation.name|escapejs }}",
                        "module": "spider_graph",
                        "preview": "spidergraph-preview.png",
                        "order": {{ evaluation.sort_order }},
                        "groups": [
                            "trainer",
                            "trainee"
                        ],
                        "permissions": {
                            "candidate": [
                                {% for group_name in evaluation.candidates_group_names %}
                                    "{{ group_name }}"
                                    {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "moderator": [
                                {% for group_name in evaluation.moderators_group_names %}
                                    "{{ group_name }}"
                                    {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "evaluator": [
                                {% for group_name in evaluation.evaluators_group_names %}
                                    "{{ group_name }}"
                                    {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "auto_evaluator": [
                                {% for group_name in evaluation.auto_evaluators_group_names %}
                                    "{{ group_name }}"
                                    {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "observer": [
                                {% for group_name in evaluation.observers_group_names %}
                                    "{{ group_name }}"
                                    {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ],
                            "auto_observer": [
                                {% for group_name in evaluation.auto_observers_group_names %}
                                    "{{ group_name }}"
                                    {% if not forloop.last %},{% endif %}
                                {% endfor %}
                            ]
                        },
                        "module_settings": [
                            {
                                "id": "view1",
                                "key": "home",
                                "broadcastable": true,
                                "info": {
                                    "exercices": [
                                        {% for exercise in evaluation.exercises.non_deleted %}
                                            {
                                                "id": "{{ exercise.id }}",
                                                "label": "{{ exercise.name|escapejs }}",
                                                "url": "{% if exercise.preview %}{{ exercise.preview.url }}{% endif %}",
                                                "default_image": "default-exercice-preview.png"
                                            }{% if not forloop.last %},{% endif %}
                                        {% endfor %}
                                    ]
                                }
                            },
                            {
                                "id": "view2",
                                "key": "evaluation",
                                "broadcastable": true,
                                "info": {
                                    "exercices": [
                                        {% for exercise in evaluation.exercises.non_deleted %}
                                            {
                                                "id": "{{ exercise.id }}",
                                                "title": "{{ exercise.name|escapejs }}",
                                                "form": [

                                                    {% for criterion in exercise.criteria.non_deleted %}
                                                        {
                                                            "id": "{{ criterion.id }}",
                                                            "label": "{{ criterion.description|escapejs }}",
                                                            {% if criterion.criterion_type == criterion.CRITERION_TYPES.QUANTITATIVE %}
                                                                "type": "numeric",
                                                                "min_value": {{ exercise.start_range }},
                                                                "max_value": {{ exercise.end_range }},
                                                                "default_value": {{ exercise.end_range }},
                                                                "step": {{ exercise.step }}
                                                            {% else %}
                                                                "type": "text"
                                                            {% endif %}
                                                        }{% if not forloop.last %},{% endif %}
                                                    {% endfor %}
                                                ]
                                            }
                                            {% if not forloop.last %},{% endif %}
                                        {% endfor %}
                                    ]
                                }
                            },
                            {
                                "id": "view3",
                                "key": "result",
                                "broadcastable": true
                            }
                        ]
                    }{% if not forloop.last %},{% endif %}
                {% endfor %}
            ]
            {% endswitch %}

            {% if training.options.activate_qrcode_launcher %}
            ,
            "qr_code": [
                {
                    "id": "2ccZzzz",
                    "title": "{% trans 'Qr Code' %}",
                    "module": "qr_code",
                    "groups": [
                        "trainer",
                        "trainee"
                    ],
                    "preview": "qr-code-preview.png",
                    "order": 0
                }
            ]
            {% endif %}
        }
    }
}
