/***************************************************
*
* cismet GmbH, Saarbruecken, Germany
*
*              ... and it just works.
*
****************************************************/
package de.cismet.cids.custom.udm2020di.search;

import Sirius.navigator.search.dynamic.SearchControlListener;
import Sirius.navigator.search.dynamic.SearchControlPanel;

import org.apache.log4j.Logger;

import org.openide.util.ImageUtilities;

import java.io.IOException;

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

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;

import de.cismet.cids.custom.udm2020di.ImageUtil;
import de.cismet.cids.custom.udm2020di.serversearch.boris.BorisSiteSearch;
import de.cismet.cids.custom.udm2020di.types.AggregationValue;

import de.cismet.cids.dynamics.CidsBean;

import de.cismet.cids.server.search.MetaObjectNodeServerSearch;

import de.cismet.cids.tools.search.clientstuff.CidsWindowSearch;

/**
 * DOCUMENT ME!
 *
 * @author   pd
 * @version  $Revision$, $Date$
 */
public class CustomMaxValuesSearchPanel extends JPanel implements CidsWindowSearch, SearchControlListener {

    //~ Instance fields --------------------------------------------------------

    protected transient Collection<AggregationValue> aggregationValues;
    protected transient Collection<CidsBean> cidsBeans;
    protected SearchControlPanel searchControlPanel;
    protected final ImageIcon icon;
    protected final Logger logger = Logger.getLogger(CustomMaxValuesSearchPanel.class);

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionPanel;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private de.cismet.cids.custom.udm2020di.widgets.MaxParameterValueSelectionPanel maxParameterValueSelectionPanel;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables

    //~ Constructors -----------------------------------------------------------

    /**
     * Creates new form CustomSearchPanel.
     */
    public CustomMaxValuesSearchPanel() {
        this.icon = ImageUtilities.loadImageIcon(
                ImageUtil.getResourcePath(CustomMaxValuesSearchPanel.class, "search_16.png"),
                false);
        initComponents();
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * DOCUMENT ME!
     *
     * @param  cidsBeans          DOCUMENT ME!
     * @param  aggregationValues  DOCUMENT ME!
     */
    public void setCollections(final Collection<CidsBean> cidsBeans,
            final Collection<AggregationValue> aggregationValues) {
        this.cidsBeans = cidsBeans;
        this.aggregationValues = aggregationValues;
        this.maxParameterValueSelectionPanel.setAggregationValues(aggregationValues);

        this.searchControlPanel = new SearchControlPanel(this);
        this.add(searchControlPanel, java.awt.BorderLayout.SOUTH);
    }
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        actionPanel = new javax.swing.JPanel();
        searchButton = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(5, 5),
                new java.awt.Dimension(5, 5),
                new java.awt.Dimension(5, 5));
        resetButton = new javax.swing.JButton();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0),
                new java.awt.Dimension(0, 0),
                new java.awt.Dimension(32767, 0));
        maxParameterValueSelectionPanel = new de.cismet.cids.custom.udm2020di.widgets.MaxParameterValueSelectionPanel();

        actionPanel.setName(org.openide.util.NbBundle.getMessage(
                CustomMaxValuesSearchPanel.class,
                "CustomMaxValuesSearchPanel.actionPanel.name")); // NOI18N
        actionPanel.setLayout(new javax.swing.BoxLayout(actionPanel, javax.swing.BoxLayout.LINE_AXIS));

        org.openide.awt.Mnemonics.setLocalizedText(
            searchButton,
            org.openide.util.NbBundle.getMessage(
                CustomMaxValuesSearchPanel.class,
                "CustomMaxValuesSearchPanel.searchButton.text"));          // NOI18N
        searchButton.setActionCommand(org.openide.util.NbBundle.getMessage(
                CustomMaxValuesSearchPanel.class,
                "CustomMaxValuesSearchPanel.searchButton.actionCommand")); // NOI18N
        searchButton.addActionListener(new java.awt.event.ActionListener() {

                @Override
                public void actionPerformed(final java.awt.event.ActionEvent evt) {
                    searchButtonActionPerformed(evt);
                }
            });
        actionPanel.add(searchButton);
        actionPanel.add(filler1);

        org.openide.awt.Mnemonics.setLocalizedText(
            resetButton,
            org.openide.util.NbBundle.getMessage(
                CustomMaxValuesSearchPanel.class,
                "CustomMaxValuesSearchPanel.resetButton.text"));          // NOI18N
        resetButton.setActionCommand(org.openide.util.NbBundle.getMessage(
                CustomMaxValuesSearchPanel.class,
                "CustomMaxValuesSearchPanel.resetButton.actionCommand")); // NOI18N
        resetButton.addActionListener(new java.awt.event.ActionListener() {

                @Override
                public void actionPerformed(final java.awt.event.ActionEvent evt) {
                    resetButtonActionPerformed(evt);
                }
            });
        actionPanel.add(resetButton);
        actionPanel.add(filler2);

        setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new java.awt.BorderLayout(5, 5));
        add(maxParameterValueSelectionPanel, java.awt.BorderLayout.CENTER);
    } // </editor-fold>//GEN-END:initComponents

    /**
     * DOCUMENT ME!
     *
     * @param  evt  DOCUMENT ME!
     */
    private void searchButtonActionPerformed(final java.awt.event.ActionEvent evt) { //GEN-FIRST:event_searchButtonActionPerformed
    }                                                                                //GEN-LAST:event_searchButtonActionPerformed

    /**
     * DOCUMENT ME!
     *
     * @param  evt  DOCUMENT ME!
     */
    private void resetButtonActionPerformed(final java.awt.event.ActionEvent evt) { //GEN-FIRST:event_resetButtonActionPerformed
        this.maxParameterValueSelectionPanel.reset();
    }                                                                               //GEN-LAST:event_resetButtonActionPerformed

    @Override
    public JComponent getSearchWindowComponent() {
        return this;
    }

    @Override
    public ImageIcon getIcon() {
        return this.icon;
    }

    @Override
    public void searchStarted() {
        if (logger.isDebugEnabled()) {
            logger.debug("search started");
        }
    }

    @Override
    public void searchDone(final int numberOfResults) {
        if (logger.isDebugEnabled()) {
            logger.debug("search done");
        }
    }

    @Override
    public void searchCanceled() {
        if (logger.isDebugEnabled()) {
            logger.debug("search canceled");
        }
    }

    @Override
    public boolean suppressEmptyResultMessage() {
        return false;
    }

    @Override
    public MetaObjectNodeServerSearch assembleSearch() {
        return getServerSearch();
    }
    @Override
    public MetaObjectNodeServerSearch getServerSearch() {
        try {
            final BorisSiteSearch customSearch = new BorisSiteSearch();

            final List<Integer> objectIds = new ArrayList<Integer>(cidsBeans.size());
            int classId = -1;
            for (final CidsBean cidsBean : cidsBeans) {
                objectIds.add(cidsBean.getPrimaryKeyValue());
                classId = (classId == -1) ? cidsBean.getMetaObject().getMetaClass().getId() : classId;
            }

            customSearch.setClassId(classId);
            customSearch.setObjectIds(objectIds);
            customSearch.setMaxValues(this.maxParameterValueSelectionPanel.getValues());
            // customSearch.setMinDate();

            return customSearch;
        } catch (IOException ex) {
            logger.error("could not perform search: " + ex.getMessage(), ex);
        }
        return null;
    }
}
