package rapanui.ui;

import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import java.util.Map;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import rapanui.core.ProofEnvironment;

class MainWindow extends JFrame implements PropertyChangeListener, ApplicationObserver {
	private static final long serialVersionUID = 1L;

	private final Application app;
	private final SymbolKeyboard keyboard = new SymbolKeyboard();

	private final JPanel proofContainer = new JPanel(new CardLayout());
	private final JComboBox<String> proofList = new JComboBox<String>();

	// use a counter instead of counting existing ones so there are no duplicates after a deletion
	private int environmentCounter = 1;
	private final Map<ProofEnvironment, JComponent> environmentViewMap = new HashMap<ProofEnvironment, JComponent>();

	public MainWindow(Application app) {
		assert app != null;
		this.app = app;

		initializeContent();
		setTitle("RAPA nui – Relational Algebra Proof Assistant");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(MAXIMIZED_BOTH);

		for (ProofEnvironment environment : app.getEnvironments())
			createEnvironmentView(environment);
		app.addObserver(this);

		pack();
		setVisible(true);
	}

	private void initializeContent() {
		rootPane.getContentPane().setBackground(Color.WHITE);
		rootPane.getContentPane().setLayout(new BorderLayout());	

		JLabel title = new JLabel("RAPA nui – Relational Algebra Proof Assistant");
		title.setFont(new Font(title.getFont().getFamily(), Font.BOLD, 26));
		title.setBorder(new EmptyBorder(15,15,15,15));
		rootPane.getContentPane().add(title, BorderLayout.NORTH);

		JPanel leftPanel = new JPanel(new BorderLayout());
		leftPanel.setBackground(Color.WHITE);

		JPanel proofSelectionPanel = new JPanel();
		proofSelectionPanel.setOpaque(false);
		proofSelectionPanel.setLayout(new BoxLayout(proofSelectionPanel, BoxLayout.X_AXIS));

		proofSelectionPanel.add(new JLabel("Aktueller Beweis:"));
		proofSelectionPanel.add(Box.createHorizontalStrut(5));
		proofSelectionPanel.add(proofList);
		proofSelectionPanel.add(new SimpleLink("\u2A01", "Neuen Beweis starten", UICommand.createProofEnvironment(app)));
		proofSelectionPanel.add(new SimpleLink("\u2718", "Aktuellen Beweis löschen"));

		proofContainer.setOpaque(false);

		proofList.addItemListener((e) -> activateEnvironmentView(e.getItem().toString()));

		leftPanel.setBorder(new EmptyBorder(10,10,10,10));
		leftPanel.add(proofSelectionPanel, BorderLayout.NORTH);
		leftPanel.add(proofContainer, BorderLayout.CENTER);
		leftPanel.add(keyboard, BorderLayout.SOUTH);

		JPanel suggestionPanel = new JPanel();
		suggestionPanel.setBackground(Color.WHITE);
		suggestionPanel.add(new JLabel("Vorschläge"), BorderLayout.NORTH);

		JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, suggestionPanel);
		rootPane.getContentPane().add(splitter, BorderLayout.CENTER);

		KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(this);
	}

	private void createEnvironmentView(ProofEnvironment environment) {
		JScrollPane tab = new JScrollPane(new ProofEnvironmentPanel(app, environment));
		tab.setBorder(null);
		tab.setOpaque(false);
		tab.getViewport().setOpaque(false);

		String name = "Beweis " + environmentCounter++;
		proofContainer.add(tab, name);
		proofList.addItem(name);
		environmentViewMap.put(environment, tab);

		activateEnvironmentView(name);
	}

	private void activateEnvironmentView(String name) {
		((CardLayout)proofContainer.getLayout()).show(proofContainer, name);
		proofList.setSelectedItem(name);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if ("focusOwner".equals(evt.getPropertyName()))
			keyboard.setVisible(evt.getNewValue() instanceof JTextField);
	}

	@Override
	public void environmentAdded(ProofEnvironment environment) {
		createEnvironmentView(environment);
	}

	@Override
	public void environmentRemoved(ProofEnvironment environment) {
		// TODO
	}
}

@@ -2454,16 +2454,203 @@
 g()));%0A%0A
+%09%09JScrollPane scrollContainer = new JScrollPane(proofContainer);%0A%09%09scrollContainer.setBorder(null);%0A%09%09scrollContainer.setOpaque(false);%0A%09%09scrollContainer.getViewport().setOpaque(false);%0A%0A
 %09%09leftPa
@@ -2690,24 +2690,24 @@
 10,10,10));%0A
-
 %09%09leftPanel.
@@ -2768,21 +2768,22 @@
 nel.add(
-proof
+scroll
 Containe
@@ -3341,42 +3341,37 @@
 %7B%0A%09%09
-JScrollPane tab = new JScrollPane(
+ProofEnvironmentPanel view = 
 new 
@@ -3413,96 +3413,9 @@
 ent)
-);%0A%09%09tab.setBorder(null);%0A%09%09tab.setOpaque(false);%0A%09%09tab.getViewport().setOpaque(false);%0A
+;
 %0A%09%09S

create only one JScrollPane around all environment views