<?xml version="1.0" encoding="utf-8"?>
<?py
from cocktail.stringutils import normalize
from cocktail.controllers import get_state, view_state, serialize_parameter
from cocktail.controllers.userfilter import user_filters_registry
?>

<form
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:py="http://www.whads.com/ns/cocktail/templates">

    <?py-class
    content_type = None
    filters = ()
    available_filters = ()

    def create_filter_ui(self, filter):
        ui = filter.create_ui()
        ui.embeded = True
        ui.table_layout = False
        ui.add_class("filter_ui")
        return ui

    def get_discard_filters_state(self):
        state = {"page": 0, "filter": ""}

        for i, f in enumerate(self.filters):
            for member in f.schema.members().itervalues():
                state["filter_" + member.name + str(i)] = ""

        return state

    def get_add_filter_state(self, filter):
        return user_filters_registry.get_new_filter_view_state(
            self.content_type,
            [filter.id for filter in self.filters],
            filter
        )
        return state
    ?>

    <?py
    self.add_resource("/cocktail/scripts/xregexp.js")
    self.add_resource("/cocktail/scripts/searchable.js")
    self.add_resource("/cocktail/scripts/FilterBox.js")
    ?>

    <div py:id="filter_list">
        <py:ready>
            <py:new
                py:element="self.create_filter_entry(index, filter)"
                py:for="index, filter in enumerate(self.filters)"/>
        </py:ready>
    </div>

    <div py:def="filter_entry" py:args="index, filter" py:filter="${filter}">
        <?py
        element.add_class("filter-" + filter.id)
        ?>
        <input type="hidden" name="filter" value="${filter.id}"/>
        <span class="filter_desc">${translations(filter)}</span>
        <py:new
            py:element="self.create_filter_ui(filter)"
            py:name_suffix="${str(index)}"/>
        <a
            class="delete_filter_button"
            title="${translations('cocktail.html.FilterBox.delete_button')}"
            py:if="not filter.promoted_search">
            <?py
            filter_param = []
            params = {"page": "0"}

            for i, f in enumerate(self.filters):
                if i != index:
                    filter_param.append(f.id)
                for member in f.schema.members().itervalues():
                    key = "filter_" + member.name + str(i if i <= index else i - 1)
                    value = "" if i == index else serialize_parameter(
                        member,
                        getattr(f, member.name)
                    )
                    params[key] = value

            params["filter"] = filter_param or ""
            element["href"] = "?" + view_state(**params)
            ?>
            ✖
        </a>
    </div>

    <div py:id="buttons">

        <py:cocktail.html.DropdownPanel py:id="new_filter_selector">
            <py:with py:element="element.label">
                ${translations("cocktail.html.FilterBox add filter")}
            </py:with>
            <py:with py:element="element.panel">
                <py:ready>
                    <?py
                    chosen = set(filter.id for filter in self.filters)
                    filter_items = [(translations(filter), filter)
                                    for filter in self.available_filters
                                    if (filter.repeatable and not filter.promoted_search)
                                    or filter.id not in chosen]

                    filter_items.sort(key = lambda item: normalize(item[0]))
                    ?>
                    <a py:for="desc, filter in filter_items"
                        href="${'?' + view_state(**self.get_add_filter_state(filter))}">
                        ${desc}
                        <py:new
                            py:element="self.create_filter_entry('$index', filter)"
                            py:client_model="cocktail.html.FilterBox-entry-${filter.id}"/>
                        <?py
                        element.set_client_param("filterId", filter.id)
                        element.add_class("new_filter-" + filter.id)
                        ?>
                    </a>
                </py:ready>
            </py:with>
        </py:cocktail.html.DropdownPanel>

        <a py:id="discard_button" href="@{'?' + view_state(**self.get_discard_filters_state())}">
            ${translations("cocktail.html.FilterBox remove filters")}
        </a>

        <button py:id="search_button" type="submit">
            ${translations("cocktail.html.FilterBox apply filters")}
        </button>
    </div>

</form>
