# Path: memopol/search/forms.py
# class MEPSearchForm(HaystackForm):
#     options_form_class = MEPSearchOptionsForm
# 
#     FILTER_NAME = FILTER_NAME
# 
#     _FILTERS_BY_FIELD = {
#         FILTER_NAME.FULLTEXT: {
#             'type': 'fulltext',
#             'receptacle': 'fulltext'
#         },
#         FILTER_NAME.IS_ACTIVE: {
#             'type': 'yes_no',
#             'receptacle': 'yes_no',
#         },
#         FILTER_NAME.COUNTRY: {
#             'type': 'str',
#             'receptacle': 'country',
#         },
#         FILTER_NAME.GROUP: {
#             'type': 'str',
#             'receptacle': 'group',
#         },
#         FILTER_NAME.COMMITTEE: {
#             'type': 'str',
#             'receptacle': 'committees',
#         },
#         FILTER_NAME.DELEGATION: {
#             'type': 'id',
#             'receptacle': 'delegations',
#         },
#         FILTER_NAME.TOTAL_SCORE: {
#             'type': 'int',
#             'receptacle': 'int',
#         },
#         FILTER_NAME.ACHIEVEMENT: {
#             'type': 'id',
#             'receptacle': 'autocomplete',
#             'autocomplete_lookup': 'mep_achievements',
#         },
#         FILTER_NAME.BXL_BUILDING: {
#             'type': 'str',
#             'receptacle': 'bxl_building',
#         },
#         FILTER_NAME.BXL_FLOOR: {
#             'type': 'str',
#             'receptacle': 'str',
#         },
#         FILTER_NAME.STG_BUILDING: {
#             'type': 'str',
#             'receptacle': 'stg_building',
#         },
#         FILTER_NAME.STG_FLOOR: {
#             'type': 'str',
#             'receptacle': 'str',
#         },
#     }
# 
#     filter_value_country = StrChoiceField(COUNTRY)
#     filter_value_group = StrChoiceField(GROUP)
#     filter_value_committees = StrChoiceField(COMMITTEE)
#     filter_value_delegations = IntChoiceField(DELEGATION)
#     filter_value_bxl_building = StrChoiceField(BXL_BUILDING)
#     filter_value_stg_building = StrChoiceField(STG_BUILDING)
# 
#     JS_FILTERS_BUILDERS = (
#         (u'reset', {
#             'replace': True,
#             'filter_name': FILTER_NAME.FULLTEXT
#         }),
#         (u'active', {
#             'filter_name': FILTER_NAME.IS_ACTIVE,
#             'filter_lookup': HaystackForm.FILTER_LOOKUPS_INT.EXACT,
#             'filter_value': YES_NO.YES,
#         }),
#         (u'name', {
#             'filter_name': FILTER_NAME.FULLTEXT,
#             'previous_right_op': HaystackForm.FILTER_RIGHT_OP.AND,
#         }),
#     )
# 
# class MEPSearchAdvancedFormset(AdvancedFormset):
#     options_form_class = MEPSearchOptionsForm
#     form = MEPSearchForm
# 
#     def get_initial_data(self):
# 
#         initial_options = {
#             'model': "MEP",
#             'limit': 15,
#         }
#         initial = [
#                 {
#                     'filter_name': FILTER_NAME.IS_ACTIVE,
#                     'yes_no_lookup': HaystackForm.FILTER_LOOKUPS_YES_NO.EXACT,
#                     'filter_value_yes_no': YES_NO.YES,
#                     'filter_right_op': HaystackForm.FILTER_RIGHT_OP.AND,
#                 },
#         ]
#         return initial, initial_options


from django.core.urlresolvers import reverse
from django.template.defaultfilters import urlencode
from django import template
from dynamiq.utils import get_advanced_search_formset_class
from ..forms import MEPSearchForm, MEPSearchAdvancedFormset



register = template.Library()


@register.simple_tag
def simple_search_shortcut(search_string, sort=None):
    """
    Return a simple search URL from a search string, like "daniel OR country:CZ".
    """
    base_url = reverse("search")
    query_string = "q=%s" % urlencode(search_string)
    if sort:
        query_string = "%s&sort=%s" % (query_string, sort)
    return "%s?%s" % (base_url, query_string)


@register.inclusion_tag('blocks/search_form.html', takes_context=True)
def render_search_form(context):
    """
    Display the search form, if a `dynamiq` key is on the context, it will
    used, otherwise, it create an empty form
    """
    if 'dynamiq' in context:
        dynamiq = context['dynamiq']
    else:
        request = context['request']

# Path: memopol/search/forms.py
# class MEPSearchForm(HaystackForm):
#     options_form_class = MEPSearchOptionsForm
# 
#     FILTER_NAME = FILTER_NAME
# 
#     _FILTERS_BY_FIELD = {
#         FILTER_NAME.FULLTEXT: {
#             'type': 'fulltext',
#             'receptacle': 'fulltext'
#         },
#         FILTER_NAME.IS_ACTIVE: {
#             'type': 'yes_no',
#             'receptacle': 'yes_no',
#         },
#         FILTER_NAME.COUNTRY: {
#             'type': 'str',
#             'receptacle': 'country',
#         },
#         FILTER_NAME.GROUP: {
#             'type': 'str',
#             'receptacle': 'group',
#         },
#         FILTER_NAME.COMMITTEE: {
#             'type': 'str',
#             'receptacle': 'committees',
#         },
#         FILTER_NAME.DELEGATION: {
#             'type': 'id',
#             'receptacle': 'delegations',
#         },
#         FILTER_NAME.TOTAL_SCORE: {
#             'type': 'int',
#             'receptacle': 'int',
#         },
#         FILTER_NAME.ACHIEVEMENT: {
#             'type': 'id',
#             'receptacle': 'autocomplete',
#             'autocomplete_lookup': 'mep_achievements',
#         },
#         FILTER_NAME.BXL_BUILDING: {
#             'type': 'str',
#             'receptacle': 'bxl_building',
#         },
#         FILTER_NAME.BXL_FLOOR: {
#             'type': 'str',
#             'receptacle': 'str',
#         },
#         FILTER_NAME.STG_BUILDING: {
#             'type': 'str',
#             'receptacle': 'stg_building',
#         },
#         FILTER_NAME.STG_FLOOR: {
#             'type': 'str',
#             'receptacle': 'str',
#         },
#     }
# 
#     filter_value_country = StrChoiceField(COUNTRY)
#     filter_value_group = StrChoiceField(GROUP)
#     filter_value_committees = StrChoiceField(COMMITTEE)
#     filter_value_delegations = IntChoiceField(DELEGATION)
#     filter_value_bxl_building = StrChoiceField(BXL_BUILDING)
#     filter_value_stg_building = StrChoiceField(STG_BUILDING)
# 
#     JS_FILTERS_BUILDERS = (
#         (u'reset', {
#             'replace': True,
#             'filter_name': FILTER_NAME.FULLTEXT
#         }),
#         (u'active', {
#             'filter_name': FILTER_NAME.IS_ACTIVE,
#             'filter_lookup': HaystackForm.FILTER_LOOKUPS_INT.EXACT,
#             'filter_value': YES_NO.YES,
#         }),
#         (u'name', {
#             'filter_name': FILTER_NAME.FULLTEXT,
#             'previous_right_op': HaystackForm.FILTER_RIGHT_OP.AND,
#         }),
#     )
# 
# class MEPSearchAdvancedFormset(AdvancedFormset):
#     options_form_class = MEPSearchOptionsForm
#     form = MEPSearchForm
# 
#     def get_initial_data(self):
# 
#         initial_options = {
#             'model': "MEP",
#             'limit': 15,
#         }
#         initial = [
#                 {
#                     'filter_name': FILTER_NAME.IS_ACTIVE,
#                     'yes_no_lookup': HaystackForm.FILTER_LOOKUPS_YES_NO.EXACT,
#                     'filter_value_yes_no': YES_NO.YES,
#                     'filter_right_op': HaystackForm.FILTER_RIGHT_OP.AND,
#                 },
#         ]
#         return initial, initial_options


# Path: memopol/search/templatetags/search_tags.py
from django.core.urlresolvers import reverse
from django.template.defaultfilters import urlencode
from django import template
from dynamiq.utils import get_advanced_search_formset_class
from ..forms import MEPSearchForm, MEPSearchAdvancedFormset




register = template.Library()


@register.simple_tag
def simple_search_shortcut(search_string, sort=None):
    """
    Return a simple search URL from a search string, like "daniel OR country:CZ".
    """
    base_url = reverse("search")
    query_string = "q=%s" % urlencode(search_string)
    if sort:
        query_string = "%s&sort=%s" % (query_string, sort)
    return "%s?%s" % (base_url, query_string)


@register.inclusion_tag('blocks/search_form.html', takes_context=True)
def render_search_form(context):
    """
    Display the search form, if a `dynamiq` key is on the context, it will
    used, otherwise, it create an empty form
    """
    if 'dynamiq' in context:
        dynamiq = context['dynamiq']
    else:
        request = context['request']

        formset_class = get_advanced_search_formset_class(request.user, MEPSearchAdvancedFormset, MEPSearchForm)