package edu.sc.seis.gee.task;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import edu.sc.seis.fissuresUtil.exceptionHandler.GlobalExceptionHandler;
import edu.sc.seis.fissuresUtil.freq.NamedFilter;
import edu.sc.seis.gee.CommonAccess;
import edu.sc.seis.gee.configurator.ConfigurationException;

/**
 * CustomFilterTask.java
 *
 *
 * Created: Wed Oct 30 14:55:30 2002
 *
 * @author <a href="mailto:">Srinivasa Telukutla</a>
 * @version
 */

public class CustomFilterTask extends JPanel implements Task{
    public CustomFilterTask (){

    }

    public void invoke()  {
        if(!guiInitialized) {
            guiInitialized = true;
            createGUI();
        }
    }

    public void configure(java.util.Map params) throws ConfigurationException{
        this.configParams = params;
        if(params.containsKey("filterTargets")){
            String filterTargetIds = (String)params.get("filterTargets");
            filterTarget = FilterTask.parseFilterTargets(filterTargetIds);
        }
    }

    public void destroy(){}

    private void createGUI() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        this.setLayout(gridBagLayout);
        final JComboBox filterType = new JComboBox();
        final JComboBox filterStyle = new JComboBox();
        final JComboBox filterPasses = new JComboBox();
        JLabel filterTypeLabel = new JLabel("filterType");
        JLabel filterStyleLabel = new JLabel("filterStyle");
        JLabel filterPassesLabel = new JLabel("passes");
        populateComboBox(filterType, types);
        populateComboBox(filterStyle, styles);
        populateComboBox(filterPasses, passes);

        gridBagLayout.setConstraints(filterTypeLabel, constraints);
        this.add(filterTypeLabel);

        constraints.gridwidth = 1;
        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(filterStyleLabel, constraints);
        this.add(filterStyleLabel);
        constraints.gridwidth = 1;

        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(filterPassesLabel, constraints);
        this.add(filterPassesLabel);

        constraints.gridx = 0;
        constraints.gridy = constraints.gridy + 1;
        gridBagLayout.setConstraints(filterType, constraints);
        this.add(filterType);

        //  constraints.gridwidth = 2;
        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(filterStyle, constraints);
        this.add(filterStyle);
        constraints.gridwidth = 1;

        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(filterPasses, constraints);
        this.add(filterPasses);


        final JTextField poles = new JTextField("2", 10);
        JLabel polesLabel = new JLabel("poles");
        final JTextField minFrequency = new JTextField(".01");
        final JLabel minFrequencyLabel = new JLabel("minFreq");
        final JTextField maxFrequency = new JTextField("1");
        final JLabel maxFrequencyLabel = new JLabel("maxFreq");
        final JTextField filterName = new JTextField("noName");
        final JLabel filterNameLabel = new JLabel("Enter Name for the Filter: ");
        JButton applyButton = new JButton("Apply");

        applyVisibility(filterStyle,
                        minFrequencyLabel,
                        minFrequency,
                        maxFrequencyLabel,
                        maxFrequency);

        constraints.gridx = 0;
        constraints.gridy = constraints.gridy + 1;
        gridBagLayout.setConstraints(polesLabel, constraints);
        this.add(polesLabel);

        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(minFrequencyLabel, constraints);
        this.add(minFrequencyLabel);

        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(maxFrequencyLabel, constraints);
        this.add(maxFrequencyLabel);

        constraints.gridx = 0;
        constraints.gridy = constraints.gridy + 1;
        gridBagLayout.setConstraints(poles, constraints);
        this.add(poles);

        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(minFrequency, constraints);
        this.add(minFrequency);

        constraints.gridx = constraints.gridx + 1;
        gridBagLayout.setConstraints(maxFrequency, constraints);
        this.add(maxFrequency);

        constraints.gridx = 0;
        constraints.gridy = constraints.gridy + 1;
        constraints.gridwidth = 2;
        gridBagLayout.setConstraints(filterNameLabel, constraints);
        this.add(filterNameLabel);


        constraints.gridx = constraints.gridx + 2;
        constraints.gridwidth = 1;
        gridBagLayout.setConstraints(filterName, constraints);
        this.add(filterName);

        constraints.gridwidth = 3;
        constraints.gridy = constraints.gridy + 1;
        constraints.gridx = 0;
        gridBagLayout.setConstraints(applyButton, constraints);
        this.add(applyButton);
        constraints.gridwidth = 1;


        applyButton.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        //here build the filter.
                        int polesValue = 2;
                        try {
                            polesValue = Integer.parseInt(poles.getText());
                        } catch(NumberFormatException nfe) {
                            //TODO: this is easy, but a dialog would be better or never allowing non-numbers to be typed
                            GlobalExceptionHandler.handle("poles must be an integer: '"+poles.getText()+"'", nfe);
                        }
                        double minFrequencyValue = 0.0;
                        try {
                            minFrequencyValue = Double.parseDouble(minFrequency.getText());

                        } catch(NumberFormatException nfe) {
                            GlobalExceptionHandler.handle("min frequency must be an number: '"+poles.getText()+"'", nfe);
                        }

                        double maxFrequencyValue = 0.0;
                        try {
                            maxFrequencyValue = Double.parseDouble(maxFrequency.getText());
                        } catch(NumberFormatException nfe) {
                            GlobalExceptionHandler.handle("max frequency must be an number: '"+poles.getText()+"'", nfe);
                        }
                        String filterTypeValue = (String)filterType.getSelectedItem();
                        String filterStyleValue = (String)filterStyle.getSelectedItem();
                        String filterPassesValue = (String)filterPasses.getSelectedItem();
                        minFrequencyValue = getMinFrequencyValue(minFrequencyValue, filterStyleValue);
                        maxFrequencyValue = getMaxFrequencyValue(maxFrequencyValue, filterStyleValue);
                        String fName = filterName.getText();
                        if (customFilters.containsKey(fName)) {
                            // alreadya filter of this name, toggle off and remove it
                            NamedFilter filter = (NamedFilter)customFilters.get(filterName.getText());
                            if (filter.getVisibility()) {
                                // filter is visible, so apply again, which will make invisible, then remove
                                applyFilter(fName);
                                customFilters.remove(fName);
                            }
                        }
                        createFilter(filterTypeValue,
                                    filterStyleValue,
                                    filterPassesValue,
                                    polesValue,
                                    minFrequencyValue,
                                    maxFrequencyValue,
                                    filterName.getText());
                    }
                });

        filterStyle.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        applyVisibility(filterStyle,
                                        minFrequencyLabel,
                                        minFrequency,
                                        maxFrequencyLabel,
                                        maxFrequency);
                        //          addFilter(filterName.getText());
                    }

                });




    }

    private void applyFilter(final String name) {
        NamedFilter filter = (NamedFilter)customFilters.get(name);
        filter.toggleVisibility();
        for (int i = 0; i < filterTarget.length; i++){
            filterTarget[i].applyFilter(filter, filter.getVisibility());
        }
    }

    private double getMinFrequencyValue(double minFreq,
                                        String style) {
        if(style.equals("LP")) return 0.0;
        return minFreq;
    }

    private double getMaxFrequencyValue(double maxFreq,
                                        String style) {
        if(style.equals("HP")) return 10000.0;
        return maxFreq;
    }


    private void applyVisibility(JComboBox filterStyleBox,
                                 JLabel minFrequencyLabel,
                                 JTextField minFrequency,
                                 JLabel maxFrequencyLabel,
                                 JTextField maxFrequency) {
        String filterStyle = (String) filterStyleBox.getSelectedItem();
        if(filterStyle.equals("LP")) {
            minFrequencyLabel.setVisible(false);
            minFrequency.setVisible(false);
            maxFrequencyLabel.setVisible(true);
            maxFrequency.setVisible(true);
        } else if(filterStyle.equals("HP")) {
            minFrequencyLabel.setVisible(true);
            minFrequency.setVisible(true);
            maxFrequencyLabel.setVisible(false);
            maxFrequency.setVisible(false);
        } else if(filterStyle.equals("BP")) {
            minFrequencyLabel.setVisible(true);
            minFrequency.setVisible(true);
            maxFrequencyLabel.setVisible(true);
            maxFrequency.setVisible(true);
        } else if(filterStyle.equals("BS")) {
            minFrequencyLabel.setVisible(true);
            minFrequency.setVisible(true);
            maxFrequencyLabel.setVisible(true);
            maxFrequency.setVisible(true);
        }
    }

    private void createFilter(String filterType,
                             String filterStyle,
                             String passes,
                             int poles,
                             double minFrequency,
                             double maxFrequency,
                             String filterName) {

        int filterPassType = 0;
        if(passes.equals("CAUSAL")) filterPassType = 0;
        else filterPassType = 1;
        System.out.println("Create filter: "+localeText+" "+
                                                 minFrequency+" "+
                                                 maxFrequency+" "+
                                                 poles+" "+
                                                 filterPassType);
        NamedFilter filter = new NamedFilter(minFrequency,
                                                 maxFrequency,
                                                 poles,
                                                 filterPassType,
                                                 localeText);

        filter.setVisibility(false);
        //lterTarget.applyFilter(filter);
        customFilters.put(filterName, filter);
        applyFilter(filterName);
    }


    private void populateComboBox(JComboBox comboBox, String[] values) {
        for(int counter = 0; counter < values.length; counter++) {
            comboBox.addItem(values[counter]);
        }

    }

    private boolean guiInitialized = false;

    Map configParams;

    private String[] types = {"ButterWorth"};

    private String[] styles = {"LP","BP","HP","BS"};

    private String[] passes = {"CAUSAL", "NONCAUSAL"};

    protected String localeText = "";

    protected FilterTarget[] filterTarget;

    private ToolPaletteTask paletteTask;

    private TaskAction taskAction;

    HashMap customFilters = new HashMap();

}// CustomFilterTask
