package industries.doublehelix.conditionalfilter.gui.filtersettings;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

import industries.doublehelix.conditionalfilter.filtertypes.Filter;
import industries.doublehelix.conditionalfilter.filtertypes.container.ContainerFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.DateFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.DateTimeFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.FixedValuesStringFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.NumberFilter;
import industries.doublehelix.conditionalfilter.filtertypes.flat.StringFilter;

public class FilterSettingsPanel extends JPanel {
	private static final long serialVersionUID = -1734955078070677686L;
	
	protected Filter filter;
	protected int preferredContentHeight = 0;
	
	protected FilterSettingsPanel(Filter filter, int preferredContentHeight) {
		this.filter = filter;
		this.preferredContentHeight = preferredContentHeight;
		
		this.setLayout(new GridBagLayout());
		//this.setBackground(Color.red);
		//this.setMaximumSize(new Dimension(this.getMaximumSize().width, new JButton().getPreferredSize().height));
		this.setAlignmentX(JPanel.LEFT_ALIGNMENT);
	}
	
	public static FilterSettingsPanel getFilterSettingsPanelForFilter(Filter filter, int preferredContentHeight) {
		if (filter instanceof ContainerFilter) {
			return new ContainerFilterSettingsPanel((ContainerFilter)filter, preferredContentHeight);
		} else if (filter instanceof FixedValuesStringFilter) { // check before stringfilter as it extends from it
			return new FixedValuesStringFilterSettingsPanel((FixedValuesStringFilter)filter, preferredContentHeight);
		} else if (filter instanceof StringFilter) {
			return new StringFilterSettingsPanel((StringFilter)filter, preferredContentHeight);
		} else if (filter instanceof NumberFilter) {
			return new NumberFilterSettingsPanel((NumberFilter)filter, preferredContentHeight);
		} else if (filter instanceof DateTimeFilter) {
			return new DateTimeFilterSettingsPanel((DateTimeFilter)filter, preferredContentHeight);
		} else if (filter instanceof DateFilter) {
			return new DateFilterSettingsPanel((DateFilter)filter, preferredContentHeight);
		}
		return new FilterSettingsPanel(filter, preferredContentHeight); // just an empty panel
	}
	
	protected void setPreferredHeightToComponents(Component... components) {
		for (Component comp : components) {
			comp.setPreferredSize(new Dimension(comp.getPreferredSize().width, preferredContentHeight));
		}
	}
}
