package rapanui.ui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

import rapanui.core.ConclusionProcess;

class ConclusionProcessView extends JPanel {
	private static final long serialVersionUID = 1L;

	private final ConclusionProcess model;

	private final JPanel longForm = new JPanel(new GridBagLayout());
	private Color borderColor = Color.BLACK;
	private int borderWidth = 1;

	public ConclusionProcessView(ConclusionProcess model) {
		assert model != null;
		this.model = model;

		initializeContent();
	}

	public void activate() {
		borderColor = Color.ORANGE;
		borderWidth = 2;
		updateBorder();
	}

	public void deactivate() {
		borderColor = Color.BLACK;
		borderWidth = 1;
		updateBorder();
	}

	private void updateBorder() {
		setBorder(new CompoundBorder(new EmptyBorder(5,0,5,0),
				new CompoundBorder(new LineBorder(borderColor, borderWidth), new EmptyBorder(5,5,5,5))));
	}

	private void initializeContent() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setOpaque(false);
		updateBorder();

		String shortForm = "R = S;T"; // TODO: remove dummy data

		JPanel header = new JPanel();
		header.setBackground(Color.WHITE);
		header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));

		longForm.setOpaque(false);

		JSeparator separator = new JSeparator();

		header.add(new CollapseButton(longForm, separator));
		header.add(ProofEnvironmentPanel.createMathematicalLabel(shortForm));
		header.add(Box.createHorizontalGlue());

		/*
		 * Implementation of modification features has been postponed.
		 *
		 * header.add(new SimpleLink("\u2B06", "Nach oben"));
		 * header.add(new SimpleLink("\u2B07", "Nach unten"));
		 * header.add(new SimpleLink("\u27F2", "Letzter Schritt rückgängig"));
		 * header.add(new SimpleLink("\u2718", "Folgerung entfernen"));
		 */

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.ipadx = 5;
		c.anchor = GridBagConstraints.EAST;
		c.weightx = 0.3;
		longForm.add(ProofEnvironmentPanel.createMathematicalLabel("R "), c);

		// TODO: remove dummy data
		String[] steps = new String[] { "= R;I", "= R;(S;T)", "= (R;S);T", "= S;T"};
		String[] reasons = new String[] { "Neutralität von I", "nach Voraussetzung", "Assoziativität", "nach Voraussetzung" };

		for (int i = 0; i < steps.length; ++i)
			addTransformation(steps[i], reasons[i]);

		add(header);
		add(separator);
		add(longForm);
	}

	private int line = 0; // TODO: remove and use model data
	private void addTransformation(String transformation, String reason) { // TODO: Transformation as parameter
		GridBagConstraints c = new GridBagConstraints();
		c.ipadx = 5;
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;
		c.gridy = line;
		c.weightx = 0.35;
		longForm.add(ProofEnvironmentPanel.createMathematicalLabel(transformation), c);

		c.gridx = 2;
		c.weightx = 0;
		longForm.add(new JLabel("(" + reason + ")"), c);
		line++;
	}
}

@@ -123,16 +123,163 @@
 Process;
+%0Aimport rapanui.core.ConclusionProcessObserver;%0Aimport rapanui.core.FormulaType;%0Aimport rapanui.core.Transformation;%0A%0Aimport rapanui.dsl.DslHelper;
 %0A%0Aclass 
@@ -315,16 +315,53 @@
  JPanel 
+implements ConclusionProcessObserver 
 %7B%0A%09priva
@@ -512,16 +512,45 @@
 out());%0A
+%09private JLabel titleLabel;%0A%0A
 %09private
@@ -740,16 +740,44 @@
 ntent();
+%0A%0A%09%09model.addObserver(this);
 %0A%09%7D%0A%0A%09pu
@@ -1306,45 +1306,19 @@
 m = 
-%22R = S;T%22; // TODO: remove dummy data
+getTitle();
 %0A%0A%09%09
@@ -1584,24 +1584,37 @@
 %09header.add(
+titleLabel = 
 ProofEnviron
@@ -3135,11 +3135,447 @@
 ne++;%0A%09%7D
+%0A%0A%09private String getTitle() %7B%0A%09%09return DslHelper.serialize(model.getStartTerm())%0A%09%09+ %22 %22%0A%09%09+ (model.getType() == FormulaType.Equality ? %22=%22 : %22%E2%8A%86%22)%0A%09%09+ %22 %22%0A%09%09+ DslHelper.serialize(model.getLastTerm());%0A%09%7D%0A%0A%09@Override%0A%09public void transformationAdded(Transformation transformation) %7B%0A%09%09titleLabel.setText(getTitle());%0A%09%7D%0A%0A%09@Override%0A%09public void transformationRemoved(Transformation transformation) %7B%0A%09%09titleLabel.setText(getTitle());%0A%09%7D
 %0A%7D%0A

replace dummy data for conclusion "short form" with model data