package Division.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import Division.Claimer;

public class DisplayDivision extends JFrame{
	
	/**	 */
	private static final long serialVersionUID = -4542342378814217899L;

	List<Claimer> claimers;
	
	JPanel currentView;
	
	/** the title of the algorithm */
	private String title;
	
	/**
	 * This frame shows the divisions graphically
	 * @param claimers the list of claimers
	 * @param showTalmudDisplayOption if the talmud was used set this to true, so that the connected vessels are shown to
	 */
	public DisplayDivision(List<Claimer> claimers, boolean showTalmudDisplayOption, String title){
		this.claimers = claimers;
		this.title = title;
		
		setLayout(new BorderLayout());
		add(buttonPanel(showTalmudDisplayOption), BorderLayout.NORTH);
		currentView = new GeneralDivisionDisplayPanel(claimers);
		add(currentView);
		
		setTitle("Wega division display (Right click to save)");
		pack();
        setVisible(true);
	}
	
	private JPanel buttonPanel(boolean showTalmudDisplayOption){
		JPanel buttonPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints(); 
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(10, 10, 10 , 10);
		
		JButton showGeneralDisplay = new JButton("Show general division display");
		JButton showPieChartDisplay = new JButton("Show pie chart");
		
		showGeneralDisplay.addActionListener(new ShowGeneralDisplayListener());
		showPieChartDisplay.addActionListener(new ShowPieDisplayListener());
		
		buttonPanel.add(showGeneralDisplay,c);
			
			c.gridx = 1;
		buttonPanel.add(showPieChartDisplay);
		
		if(showTalmudDisplayOption){
			JButton showTalmudDisplay = new JButton("Show connected vessels (for Talmud)");
			showTalmudDisplay.addActionListener(new ShowTalmudDisplayListener());
			c.gridx = 2;
			buttonPanel.add(showTalmudDisplay,c);
		}
		
		
		return buttonPanel;
	}
	
	class ShowPieDisplayListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			remove(currentView);
			currentView = new PieChartDisplay(claimers, title);
			add(currentView);
			repaint();
			pack();
		}
	}
	
	class ShowGeneralDisplayListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			remove(currentView);
			currentView = new GeneralDivisionDisplayPanel(claimers);
			add(currentView);
			repaint();
			pack();
		}
	}
	
	class ShowTalmudDisplayListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			remove(currentView);
			currentView = new TalmudConnectedVesselsDisplay(claimers);
			add(currentView);
			repaint();
			pack();
		}
	}
}
