# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015-2020 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Tests for the search templates with React-SearchKit/Semantic-UI."""

import json

from flask import render_template_string

from invenio_search_ui.views import format_config


def _check_template():
    """Check template."""
    extended = """
        {% extends 'semantic-ui/invenio_search_ui/search.html' %}
        {% block javascript %}{% endblock %}
        {% block css %}{% endblock %}
        {% block page_body %}{{ super() }}{% endblock %}
    """
    rendered = render_template_string(extended)
    # check if the header search bar element id exists
    assert 'id="header-search-bar"' in rendered
    # check if the search app element id exists
    assert 'id="search-ui-app"' in rendered


def test_view(app):
    """Test view."""
    with app.test_request_context():
        _check_template()


def test_format_sortoptions(app):
    """Test default sort option filter."""
    sort_options = dict(
        test1=dict(order=2, title="Test 1", default_order="desc"),
        test2=dict(order=1, title="Test 2", default_order="asc"),
    )
    default_sort = dict(query="test2", noquery="test1")
    facets = dict(aggs=dict(type=dict(terms=dict(field="type"))))

    config = dict(
        RECORDS_REST_ENDPOINTS=dict(
            recid=dict(
                list_route="/myrecords/",
                default_media_type="application/json",
                search_index="myrecords",
            )
        ),
        RECORDS_REST_SORT_OPTIONS=dict(myrecords=sort_options),
        RECORDS_REST_DEFAULT_SORT=dict(myrecords=default_sort),
        RECORDS_REST_FACETS=dict(myrecords=dict(facets)),
    )

    assert format_config(config, "recid") == dict(
        api="/api/myrecords/", mimetype="application/json",
        sort_options=[dict(
            text="Test 2",
            sortBy="test2",
            sortOrder="asc",
            default=True,
            defaultOnEmptyString=False
        ), dict(
            text="Test 1",
            sortBy="test1",
            sortOrder="desc",
            default=False,
            defaultOnEmptyString=True
        )],
        aggs=[dict(
            title="Type", aggName="type", field="type"
        )]
    )

@@ -304,21 +304,8 @@
 %22%22%0A%0A
-import json%0A%0A
 from
@@ -393,16 +393,650 @@
 config%0A%0A
+_RECORDS_REST_CONFIG = dict(%0A    RECORDS_REST_ENDPOINTS=dict(%0A        recid=dict(%0A            list_route=%22/myrecords/%22,%0A            default_media_type=%22application/json%22,%0A            search_index=%22myrecords%22,%0A        )%0A    ),%0A    RECORDS_REST_SORT_OPTIONS=dict(myrecords=dict(%0A        test1=dict(order=2, title=%22Test 1%22, default_order=%22desc%22),%0A        test2=dict(order=1, title=%22Test 2%22, default_order=%22asc%22),%0A    )),%0A    RECORDS_REST_DEFAULT_SORT=dict(%0A        myrecords=dict(query=%22test2%22, noquery=%22test1%22)),%0A    RECORDS_REST_FACETS=dict(%0A        myrecords=dict(%0A            aggs=dict(type=dict(terms=dict(field=%22type%22))))%0A    )%0A)%0A%0A
 %0Adef _ch
@@ -1491,26 +1491,32 @@
 rch app 
-element id
+config attribute
  exists%0A
@@ -1531,26 +1531,34 @@
 rt '
-id=%22search-ui-app%22
+data-invenio-search-config
 ' in
@@ -1606,24 +1606,68 @@
 st view.%22%22%22%0A
+    app.config.update(_RECORDS_REST_CONFIG)%0A
     with app
@@ -1717,16 +1717,77 @@
 plate()%0A
+    for k in _RECORDS_REST_CONFIG:%0A        del app.config%5Bk%5D%0A
 %0A%0Adef te
@@ -1865,778 +1865,92 @@
     
-sort_options = dict(%0A        test1=dict(order=2, title=%22Test 1%22, default_order=%22desc%22),%0A        test2=dict(order=1, title=%22Test 2%22, default_order=%22asc%22),%0A    )%0A    default_sort = dict(query=%22test2%22, noquery=%22test1%22)%0A    facets = dict(aggs=dict(type=dict(terms=dict(field=%22type%22))))%0A%0A    config = dict(%0A        RECORDS_REST_ENDPOINTS=dict(%0A            recid=dict(%0A                list_route=%22/myrecords/%22,%0A                default_media_type=%22application/json%22,%0A                search_index=%22myrecords%22,%0A            )%0A        ),%0A        RECORDS_REST_SORT_OPTIONS=dict(myrecords=sort_options),%0A        RECORDS_REST_DEFAULT_SORT=dict(myrecords=default_sort),%0A        RECORDS_REST_FACETS=dict(myrecords=dict(facets)),%0A    )%0A%0A    assert format_config(config, %22recid%22) == dict(
+assert format_config(_RECORDS_REST_CONFIG, %22recid%22) == dict(%0A        appId='search',
 %0A   

fix records-REST test config