package rapanui.ui.models;

import rapanui.core.ConclusionProcess;
import rapanui.core.ConclusionProcessObserver;
import rapanui.core.FormulaType;
import rapanui.core.Transformation;
import rapanui.dsl.Term;

import java.util.List;
import java.util.LinkedList;

public class ConclusionProcessModel implements ConclusionProcessObserver {
	private final ProofEnvironmentModel container;
	private final ConclusionProcess conclusion;

	private final List<Observer> observers = new LinkedList<Observer>();

	ConclusionProcessModel(ProofEnvironmentModel container, ConclusionProcess conclusion) {
		this.container = container;
		this.conclusion = conclusion;

		conclusion.addObserver(this);
	}

	public String getTitle() {
		return String.format("%s %s %s",
				conclusion.getStartTerm().serialize(),
				(conclusion.getType() == FormulaType.Equality ? "=" : "⊆"),
				conclusion.getLastTerm().serialize());
	}

	public Transformation[] getTransformations() {
		return conclusion.getTransformations();
	}

	public Term getStartTerm() {
		return conclusion.getStartTerm();
	}

	public boolean isActive() {
		return false; // container.getActiveConclusion() == this
	}

	void onActivate() {
		for (Observer observer : observers)
			observer.activated();
		loadSuggestions();
	}

	void onDeactivate() {
		for (Observer observer : observers)
			observer.deactivated();
		clearSuggestions();
	}

	protected void loadSuggestions() {
		container.loadSuggestions(conclusion);
	}

	void clearSuggestions() {
		container.clearSuggestions();
	}

	/* ****************************************** *
	 * Observer proxy                             *
	 * ****************************************** */

	public void addObserver(Observer observer) {
		assert observer != null;
		observers.add(observer);
	}

	public void removeObserver(Observer observer) {
		observers.remove(observer);
	}

	public static interface Observer {
		void activated();
		void deactivated();
		void titleChanged(String newTitle);
		void transformationAdded(Transformation transformation);
	}

	@Override
	public void transformationAdded(Transformation transformation) {
		for (Observer observer : observers)
			observer.transformationAdded(transformation);
		for (Observer observer : observers)
			observer.titleChanged(getTitle());
	}

	@Override
	public void transformationRemoved(Transformation transformation) { /* currently unused */ }
}

package rapanui.ui.models;

import rapanui.core.ConclusionProcess;
import rapanui.core.ConclusionProcessObserver;
import rapanui.core.FormulaType;
import rapanui.core.Transformation;
import rapanui.dsl.Term;

import java.util.List;
import java.util.LinkedList;

public class ConclusionProcessModel implements ConclusionProcessObserver {
	private final ProofEnvironmentModel container;
	private final ConclusionProcess conclusion;

	private final List<Observer> observers = new LinkedList<Observer>();

	ConclusionProcessModel(ProofEnvironmentModel container, ConclusionProcess conclusion) {
		this.container = container;
		this.conclusion = conclusion;

		conclusion.addObserver(this);
	}

	public String getTitle() {
		return String.format("%s %s %s",
				conclusion.getStartTerm().serialize(),
				(conclusion.getType() == FormulaType.Equality ? "=" : "⊆"),
				conclusion.getLastTerm().serialize());
	}

	public Transformation[] getTransformations() {
		return conclusion.getTransformations();
	}

	public Term getStartTerm() {
		return conclusion.getStartTerm();
	}

	public boolean isActive() {
		return false; // container.getActiveConclusion() == this
	}

	void onActivate() {
		for (Observer observer : observers)
			observer.activated();
		loadSuggestions();
	}

	void onDeactivate() {
		for (Observer observer : observers)
			observer.deactivated();
		clearSuggestions();
	}

	void loadSuggestions() {
		container.loadSuggestions(conclusion);
	}

	void clearSuggestions() {
		container.clearSuggestions();
	}

	/* ****************************************** *
	 * Observer proxy                             *
	 * ****************************************** */

	public void addObserver(Observer observer) {
		assert observer != null;
		observers.add(observer);
	}

	public void removeObserver(Observer observer) {
		observers.remove(observer);
	}

	public static interface Observer {
		void activated();
		void deactivated();
		void titleChanged(String newTitle);
		void transformationAdded(Transformation transformation);
	}

	@Override
	public void transformationAdded(Transformation transformation) {
		for (Observer observer : observers)
			observer.transformationAdded(transformation);
		for (Observer observer : observers)
			observer.titleChanged(getTitle());
	}

	@Override
	public void transformationRemoved(Transformation transformation) { /* currently unused */ }
}

fix loadSuggestions() visibility