package industries.doublehelix.furrynotemanager.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import industries.doublehelix.conditionalfilter.filtertypes.container.MasterFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.DateFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.FlatFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.NumberFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.StringFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.StringFilter.StringFilterMode;
import industries.doublehelix.conditionalfilter.gui.ConditionalFilter;
import layout.TableLayout;

public class FilterDialog extends JDialog implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	public static final ArrayList<FlatFilter> POSSIBLE_FILTERS = new ArrayList<>();
	static {
		POSSIBLE_FILTERS.add(new NumberFilter("Note ID", "originalnoteid"));
		POSSIBLE_FILTERS.add(new StringFilter("Sender"));
		POSSIBLE_FILTERS.add(new StringFilter("Receiver"));
		POSSIBLE_FILTERS.add(new StringFilter("Title"));
		POSSIBLE_FILTERS.add(new DateFilter("Date"));
		POSSIBLE_FILTERS.add(new StringFilter(StringFilterMode.CONTAINS, "Content", "rawtext"));
	}
	
	JButton ok = new JButton("OK");
	JButton abort = new JButton("Abort");
	ConditionalFilter filterPanel = null;
	
	MasterFilter originalFilter = null;
	MasterFilter newFilter = null;
	String filterName = null;
	boolean wasAborted = true; // default for when dialog is closed by X
	
	public FilterDialog(Component parent) {
		this(parent, null, null);
	}
	
	public FilterDialog(Component parent, String filterName, MasterFilter filterToEdit) {
		boolean newFilter = filterName == null;
		
		if (!newFilter) {
			originalFilter = filterToEdit;
			this.filterName = filterName;
		}
		
		this.setSize(new Dimension(800, 600));
		this.setTitle(newFilter ? "New Filter" : "Edit filter " + filterName);
		this.setLocationRelativeTo(parent);
		this.setModal(true);
		//this.setResizable(false);
		
		JPanel mainPanel = new JPanel();
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		double sizeSettings[][] = {{TableLayout.FILL, TableLayout.PREFERRED, 10, TableLayout.PREFERRED}, //width
				{TableLayout.FILL, 10, TableLayout.PREFERRED}}; // height
		mainPanel.setLayout(new TableLayout(sizeSettings));
		
		if (newFilter) {
			filterPanel = new ConditionalFilter(true, POSSIBLE_FILTERS);
		} else {
			filterPanel = new ConditionalFilter((MasterFilter)filterToEdit.clone(), POSSIBLE_FILTERS);
		}
		
		ok.addActionListener(this);
		abort.addActionListener(this);
		
		mainPanel.add(filterPanel, "0, 0, 3, 0");
		mainPanel.add(ok, "1, 2");
		mainPanel.add(abort, "3, 2");
		
		
		this.add(mainPanel);
		
		
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == ok) {
			if (filterName == null) {
				//new filter, ask for name
				String name = "";
				while (name != null && name.trim().isEmpty()) {
					name = JOptionPane.showInputDialog(this, "Please enter a name for this filter");
				}
				if (name == null) {
					// user canceled, keep dialog open
					return;
				}
				this.filterName = name;
			}
			wasAborted = false;
			this.newFilter = filterPanel.getFilter();
			this.dispose();
		} else if (e.getSource() == abort) {
			newFilter = originalFilter;
			this.dispose();
		}
	}
	
	public boolean wasAborted() {
		return wasAborted;
	}
	
	public MasterFilter getFilter() {
		return newFilter;
	}
	
	public String getFilterName() {
		return filterName;
	}
}
