package org.orienteer.core.component.property.filter;

import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.IMarkupFragment;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.orienteer.core.component.visualizer.IVisualizer;
import org.orienteer.core.service.IMarkupProvider;
import ru.ydn.wicket.wicketorientdb.utils.query.filter.FilterCriteriaType;
import ru.ydn.wicket.wicketorientdb.utils.query.filter.IFilterCriteriaManager;

import java.util.Collection;
import java.util.List;

/**
 * Panel for range filter
 * SELECT FROM aClass WHERE a BETWEEN value1 AND value2
 * @param <T> type of value
 */
public class RangeFilterPanel<T> extends AbstractFilterPanel<Collection<T>> {

    @Inject
    private IMarkupProvider markupProvider;

    private FormComponent<T> startComponent;
    private FormComponent<T> endComponent;

    @SuppressWarnings("unchecked")
    public RangeFilterPanel(String id, IModel<Collection<T>> model, String filterId, IModel<OProperty> propertyModel,
                            IVisualizer visualizer, IFilterCriteriaManager manager) {
        super(id, model, filterId, propertyModel, visualizer, manager, Model.of(true));
        startComponent = (FormComponent<T>) createFilterComponent(Model.of());
        endComponent = (FormComponent<T>) createFilterComponent(Model.of());
        startComponent.setOutputMarkupId(true);
        endComponent.setOutputMarkupId(true);
        List<Component> rangeContainers = Lists.newArrayList();
        rangeContainers.add(getRangeContainer(startComponent, getFilterId(), true));
        rangeContainers.add(getRangeContainer(endComponent, getFilterId(), false));

        ListView<Component> listView = new ListView<Component>("rangeFilters", rangeContainers) {
            @Override
            protected void populateItem(ListItem<Component> item) {
                item.add(item.getModelObject());
            }
        };
        add(listView);
    }

    @Override
    protected Collection<T> getFilterInput() {
        Collection<T> collection = Lists.newArrayList();
        collection.add(startComponent.getConvertedInput());
        collection.add(endComponent.getConvertedInput());
        return collection;
    }

    @Override
    protected void focus(AjaxRequestTarget target) {
        target.focusComponent(startComponent);
    }

    private WebMarkupContainer getRangeContainer(final Component component, final String filterId, boolean first) {
        WebMarkupContainer container = new WebMarkupContainer("container") {
            @Override
            public IMarkupFragment getMarkup(Component child) {
                if (child != null && child.getId().equals(filterId))
                    return markupProvider.provideMarkup(component);
                return super.getMarkup(child);
            }
        };
        container.add(component);
        Label label = new Label("label", new ResourceModel(first ? "widget.document.filter.range.startValue" :
                "widget.document.filter.range.endValue"));
        label.add(AttributeModifier.replace("for", component.getMarkupId()));
        container.add(label);
        return container;
    }


    @Override
    protected void setFilterCriteria(IFilterCriteriaManager manager, FilterCriteriaType type, IModel<Collection<T>> models) {
        manager.addFilterCriteria(manager.createRangeFilterCriteria(models, getJoinModel()));
    }

    @Override
    public FilterCriteriaType getFilterCriteriaType() {
        return FilterCriteriaType.RANGE;
    }

    @Override
    protected void clearInputs() {
        startComponent.setConvertedInput(null);
        endComponent.setConvertedInput(null);
        startComponent.setModelObject(null);
        endComponent.setModelObject(null);
    }
}

@@ -1028,37 +1028,8 @@
 r;%0A%0A
-import java.util.Collection;%0A
 impo
@@ -1047,16 +1047,16 @@
 l.List;%0A
+
 %0A/**%0A * 
@@ -1227,26 +1227,20 @@
 erPanel%3C
-Collection
+List
 %3CT%3E%3E %7B%0A%0A
@@ -1462,34 +1462,28 @@
  id, IModel%3C
-Collection
+List
 %3CT%3E%3E model, 
@@ -2510,26 +2510,20 @@
 otected 
-Collection
+List
 %3CT%3E getF
@@ -2545,26 +2545,20 @@
         
-Collection
+List
 %3CT%3E coll
@@ -3814,18 +3814,12 @@
 del%3C
-Collection
+List
 %3CT%3E%3E

Add min and max filtering support in range filter