package org.ohdsi.rabbitInAHat;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

public class FilterDialog extends JDialog implements ActionListener{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7009265246652874341L;
	
	
	private JTextField		sourceSearchField; 
	private JTextField		targetSearchField; 
	private MappingPanel	filterPanel;

	SpringLayout layout = new SpringLayout();
	
	JPanel mainView = new JPanel(layout);
	
	public FilterDialog(Window parentWindow){
		
		super(parentWindow,"Filter Results",ModalityType.MODELESS);		
						
		this.setLocation(parentWindow.getX()+parentWindow.getWidth()/2, parentWindow.getY()+100);
		this.setSize(700,120);
		
		sourceSearchField = new JTextField(40);
		sourceSearchField.setName("Source");
		
		targetSearchField = new JTextField(40);
		targetSearchField.setName("Target");

		
		// Add key listener to send search string as it's being typed
		sourceSearchField.addKeyListener(new SearchListener() );
		JLabel sourceLabel = new JLabel("Filter Source:",JLabel.TRAILING);
		JButton sourceClearBtn = new JButton("Clear");
		mainView.add(sourceLabel);
		mainView.add(sourceSearchField);
		sourceClearBtn.addActionListener(this);
		sourceClearBtn.setActionCommand("Clear Source");
		mainView.add(sourceClearBtn);
		
		targetSearchField.addKeyListener(new SearchListener() );
		JLabel targetLabel = new JLabel("Filter Target:",JLabel.TRAILING);
		JButton targetClearBtn = new JButton("Clear");
		mainView.add(targetLabel);
		mainView.add(targetSearchField);		
		targetClearBtn.addActionListener(this);
		targetClearBtn.setActionCommand("Clear Target");
		mainView.add(targetClearBtn);
		
		layout.putConstraint(SpringLayout.WEST, sourceLabel, 10, SpringLayout.WEST, mainView);
		layout.putConstraint(SpringLayout.NORTH, sourceLabel, 10, SpringLayout.NORTH, mainView);
		
		layout.putConstraint(SpringLayout.WEST, sourceSearchField, 5, SpringLayout.EAST, sourceLabel);
		layout.putConstraint(SpringLayout.NORTH, sourceSearchField, 10, SpringLayout.NORTH, mainView);
		
		layout.putConstraint(SpringLayout.WEST, sourceClearBtn, 5, SpringLayout.EAST, sourceSearchField);
		layout.putConstraint(SpringLayout.NORTH, sourceClearBtn, 10, SpringLayout.NORTH, mainView);
		
		layout.putConstraint(SpringLayout.WEST, targetLabel, 10, SpringLayout.WEST, mainView);
		layout.putConstraint(SpringLayout.NORTH, targetLabel, 10, SpringLayout.SOUTH, sourceLabel);
		
		layout.putConstraint(SpringLayout.WEST, targetSearchField, 0, SpringLayout.WEST, sourceSearchField);
		layout.putConstraint(SpringLayout.NORTH, targetSearchField, 0, SpringLayout.NORTH, targetLabel);
		
		layout.putConstraint(SpringLayout.WEST, targetClearBtn, 5, SpringLayout.EAST, targetSearchField);
		layout.putConstraint(SpringLayout.NORTH, targetClearBtn, 0, SpringLayout.NORTH, targetSearchField);
		
		this.add(mainView);
	};
	
	public void setFilterPanel(MappingPanel aFilterPanel){
		filterPanel = aFilterPanel;
	}
	
	public MappingPanel getFilterPanel(){
		if(filterPanel.isMinimized()){
			return filterPanel.getSlaveMappingPanel();
		}else{
			return filterPanel;
		}
	}
	
	public void doFilterPanel(String str,String panelName){		
		getFilterPanel().filterComponents(str,panelName=="Target");
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		switch (event.getActionCommand()) {
			case "Clear Source":
				sourceSearchField.setText("");
				doFilterPanel("","Source");
				break;
			
			case "Clear Target":
				targetSearchField.setText("");
				doFilterPanel("","Target");
				break;
				
		}
		
	}
	
	class SearchListener implements KeyListener{
			private String searchStrBuffer = "";
			
			@Override
			public void keyPressed(KeyEvent event) {						
				
			}

			@Override
			public void keyReleased(KeyEvent event) {
				
				switch (event.getComponent().getName()) {
					case "Source":
						searchStrBuffer = sourceSearchField.getText();
						break;
					case "Target":
						searchStrBuffer = targetSearchField.getText();
						break;
				}
				
				doFilterPanel(searchStrBuffer,event.getComponent().getName());
			}

			@Override
			public void keyTyped(KeyEvent event) {
				
			}
		
	}
	
	
}


@@ -3648,68 +3648,90 @@
 %09%09%09%09
-sourceSearchField.setText(%22%22);%0A%09%09%09%09doFilterPanel(%22%22,%22Source%22
+clearSourceFilter();%0A%09%09%09%09break;%0A%09%09%09%0A%09%09%09case %22Clear Target%22:%0A%09%09%09%09clearTargetFilter(
 );%0A%09
@@ -3747,35 +3747,57 @@
 %0A%09%09%09
+%09
 %0A%09%09
-%09case %22C
+%7D%0A%09%09%0A%09%7D%0A%0A%09private void c
 lear
- 
 Target
-%22:%0A%09%09
+Filter() %7B%0A
 %09%09ta
@@ -3825,18 +3825,16 @@
 xt(%22%22);%0A
-%09%09
 %09%09doFilt
@@ -3856,37 +3856,118 @@
 get%22);%0A%09
-%09%09%09break;%0A%09%09%09%09%0A%09%09%7D%0A%09%09
+%7D%0A%09%0A%09private void clearSourceFilter() %7B%0A%09%09sourceSearchField.setText(%22%22);%0A%09%09doFilterPanel(%22%22,%22Source%22);
 %0A%09%7D%0A%09%0A%09c

refactor to make clearSource/TargetFilter methods