package de.adesso.azubis.iliasdownloader.v3;

import de.adesso.azubis.iliasdownloader.v3.prefs.UserPreferenceService;
import de.adesso.azubis.iliasdownloader.v3.prefs.UserPreferenceServiceImpl;
import de.adesso.azubis.iliasdownloader.v3.prefs.UserPreferences;
import de.adesso.azubis.iliasdownloader.v3.ui.intro.setup.WebIliasSetupController;
import de.adesso.azubis.iliasdownloader.v3.ui.main.MainUi;
import de.adesso.azubis.iliasdownloader.v3.ui.util.DialogHelper;
import de.adesso.iliasdownloader3.service.IliasService;
import de.adesso.iliasdownloader3.service.model.LoginCredentials;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.util.Pair;
import lombok.extern.log4j.Log4j2;
import org.controlsfx.dialog.LoginDialog;

import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Optional;

/**
 * Startklasse der Anwendung.
 *
 * @author Dominik Broj
 * @since 30.01.2016
 */
@Log4j2
public final class Main extends Application {
    private static final String ILIAS_DOWNLOADER_SETTINGS = "iliasdownloader.xml";
    private final UserPreferenceService userPreferenceService;

    public Main() {
        this.userPreferenceService = new UserPreferenceServiceImpl(ILIAS_DOWNLOADER_SETTINGS);
    }

    public static void main(String[] args) {
        Main.launch(args);
    }

    @Override
    public void start(Stage stage) throws IOException {
        String iliasServerBaseUrl = "";
        String username = "";
        try {
            final UserPreferences userPreferences = userPreferenceService.loadUserPreferences();
            iliasServerBaseUrl = userPreferences.getIliasServerURL();
            username = userPreferences.getUserName();
        } catch (NoSuchFileException noSettingsEx) {
            log.info("Keine Benutzereinstellungen gefunden, zeige Einrichtungsdialog.\n Exception: {}",
                     noSettingsEx.getLocalizedMessage());

        }
        createIliasService(iliasServerBaseUrl, username);
    }

    private void createIliasService(String iliasServerBaseUrl, String username) {
        Optional<IliasService> iliasServiceOptional = new WebIliasSetupController().getIliasService(iliasServerBaseUrl);
        if (iliasServiceOptional.isPresent()) {
            showLogin(iliasServiceOptional.get(), username);
        } else {
            Platform.exit();
        }
    }

    private void showLogin(IliasService iliasService, String username) {
        new LoginDialog(new Pair<>(username, ""), usernamePasswordPair -> {
            LoginCredentials credentials = new LoginCredentials(usernamePasswordPair.getKey(),
                                                                usernamePasswordPair.getValue());
            iliasService.login(credentials);
            return null;
        }).showAndWait().ifPresent(e -> showMainUi());
    }

    private void showMainUi() {
        log.info("Öffne Main UI");
        try {
            new MainUi();
        } catch (IOException e) {
            log.error("Could not create the main ui", e);
            DialogHelper.showExceptionDialog("Fehler beim Laden der Main UI", e)
                        .ifPresent(res -> Platform.exit());
        }
    }
}

@@ -1970,24 +1970,42 @@
 %0A%0A        %7D%0A
+        try %7B%0A    
         crea
@@ -2046,24 +2046,231 @@
  username);%0A
+        %7D catch (Exception e) %7B%0A            log.error(%22Fehler beim Erstellen des Ilias Connector%22, e);%0A            DialogHelper.showExceptionDialog(%22Fehler beim Erstellen des Ilias Connector%22, e);%0A        %7D%0A
     %7D%0A%0A    p

handle error when ilias srvc creation failed