package industries.doublehelix.conditionalfilter.gui.filtersettings;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import industries.doublehelix.conditionalfilter.filtertypes.flat.StringFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.StringFilter.StringFilterMode;

public class StringFilterSettingsPanel extends FilterSettingsPanel implements ActionListener, DocumentListener {
	private static final long serialVersionUID = -6614004532630323774L;
	
	JComboBox<StringFilterMode> filterMode = new JComboBox<>();
	JTextField text = new JTextField();
	
	public StringFilterSettingsPanel(StringFilter filter, int preferredContentHeight) {
		super(filter, preferredContentHeight);
		
		filterMode.setModel(new DefaultComboBoxModel<>(StringFilterMode.values()));
	//	filterMode.setMaximumSize(filterMode.getPreferredSize());
		filterMode.setSelectedItem(filter.getFilterMode());
		filterMode.addActionListener(this);
		text.getDocument().addDocumentListener(this);
		text.setHorizontalAlignment(SwingConstants.RIGHT);
		text.setText(filter.getString());
		
		setPreferredHeightToComponents(text, filterMode);
		
		
		this.add(filterMode);
		this.add(Box.createHorizontalStrut(5));
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		this.add(text, c);
		this.add(Box.createHorizontalStrut(5));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == filterMode) {
			StringFilter castedFilter = (StringFilter)this.filter;
			StringFilterMode selectedFilterMode = (StringFilterMode)filterMode.getSelectedItem();
			if (selectedFilterMode != castedFilter.getFilterMode()) {
				castedFilter.setFilterMode(selectedFilterMode);
			}
		}
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		((StringFilter)this.filter).setString(text.getText());
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		((StringFilter)this.filter).setString(text.getText());
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
	}

}
