package de.prob2.ui.persistence;

import java.util.HashSet;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

import de.prob2.ui.MainController;
import de.prob2.ui.ProB2;
import de.prob2.ui.menu.DetachViewStageController;
import de.prob2.ui.states.StatesView;

import javafx.geometry.BoundingBox;
import javafx.stage.Stage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public final class UIPersistence {
	private static final Logger LOGGER = LoggerFactory.getLogger(UIPersistence.class);
	
	private final UIState uiState;
	private final Injector injector;
	
	@Inject
	private UIPersistence(UIState uiState, Injector injector) {
		this.uiState = uiState;
		this.injector = injector;
	}
	
	private void restoreStage(final String id, BoundingBox box) {
		LOGGER.info("Restoring stage with ID {} and bounding box {}", id, box);
		if (id == null) {
			LOGGER.warn("Stage identifier is null, cannot restore window");
			return;
		}

		if (id.startsWith(DetachViewStageController.class.getName() + " detached ".toUpperCase())) {
			// Remove the prefix before the name of the detached class
			final String toDetach = id.substring((DetachViewStageController.class.getName() + " detached ").length());
			injector.getInstance(DetachViewStageController.class).selectForDetach(toDetach);
			return;
		}
		
		Class<?> clazz;
		try {
			clazz = Class.forName(id);
		} catch (ClassNotFoundException e) {
			LOGGER.warn("Class not found, cannot restore window", e);
			return;
		}
		
		if (ProB2.class.equals(clazz)) {
			// The main stage's size is restored in the application start method.
			return;
		}
		
		Class<? extends Stage> stageClazz;
		try {
			stageClazz = clazz.asSubclass(Stage.class);
		} catch (ClassCastException e) {
			LOGGER.warn("Class is not a subclass of javafx.stage.Stage, cannot restore window", e);
			return;
		}
		
		try {
			final Stage stage = injector.getInstance(stageClazz);
			stage.show();
		} catch (RuntimeException e) {
			LOGGER.warn("Failed to restore window", e);
		}
	}
	
	public void open() {
		for (final String id : new HashSet<>(uiState.getSavedVisibleStages())) {
			this.restoreStage(id, uiState.getSavedStageBoxes().get(id));
		}

		if (uiState.getGuiState().contains("detached")) {
			injector.getInstance(DetachViewStageController.class).apply();
		} else {
			injector.getInstance(MainController.class).loadMainView(uiState.getGuiState());
		}
		
		injector.getInstance(StatesView.class).restoreColumnWidths();
	}
}

@@ -163,43 +163,8 @@
 n;%0A%0A
-import de.prob2.ui.MainController;%0A
 impo
@@ -2330,102 +2330,8 @@
 ();%0A
-%09%09%7D else %7B%0A%09%09%09injector.getInstance(MainController.class).loadMainView(uiState.getGuiState());%0A
 %09%09%7D%0A

Remove redundant MainController.loadMainView call from UIPersistence