package be.iminds.ilabt.jfed.experimenter_gui.util;

import be.iminds.ilabt.jfed.ApplicationParametersModule;
import be.iminds.ilabt.jfed.experimenter_gui.ext_requests.EnvironmentParameterHandler;
import be.iminds.ilabt.jfed.experimenter_gui.ext_requests.ExternalUserRequestHandler;
import be.iminds.ilabt.jfed.experimenter_gui.ext_requests.FirstLoginParametersHandler;
import be.iminds.ilabt.jfed.experimenter_gui.ext_requests.LoginValidationParameterHandler;
import be.iminds.ilabt.jfed.util.OSDetector;
import com.apple.eawt.AppEvent;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Map;
import java.util.Objects;

/**
 * User: twalcari
 * Date: 6/25/2015
 * Time: 16:55
 */
@Singleton
public class MacOpenURIHandler implements com.apple.eawt.OpenURIHandler {
    private static final Logger LOG = LoggerFactory.getLogger(MacOpenURIHandler.class);

    private boolean firstUriReceived = true;

    private final FirstLoginParametersHandler firstLoginParametersHandler;
    private final LoginValidationParameterHandler loginValidationParameterHandler;
    private final ExternalUserRequestHandler externalUserRequestHandler;
    private final EnvironmentParameterHandler environmentParameterHandler;

    private final ApplicationParametersModule.ApplicationParameters applicationParameters;

    @Inject
    public MacOpenURIHandler(FirstLoginParametersHandler firstLoginParametersHandler,
                             LoginValidationParameterHandler loginValidationParameterHandler,
                             ExternalUserRequestHandler externalUserRequestHandler,
                             EnvironmentParameterHandler environmentParameterHandler,
                             ApplicationParametersModule.ApplicationParameters applicationParameters) {
        this.firstLoginParametersHandler = firstLoginParametersHandler;
        this.loginValidationParameterHandler = loginValidationParameterHandler;
        this.externalUserRequestHandler = externalUserRequestHandler;
        this.environmentParameterHandler = environmentParameterHandler;
        this.applicationParameters = applicationParameters;

        assert (OSDetector.findOS() == OSDetector.OS.MAC);

        //try to catch Apple-events
        final com.apple.eawt.Application app = com.apple.eawt.Application.getApplication();
        app.setOpenURIHandler(this);
        LOG.debug("Registered Mac OpenURIHandler");

    }


    @Override
    public void openURI(AppEvent.OpenURIEvent openURIEvent) {
        Map<String, String> parameters = URIUtils.processJFedUri(openURIEvent.getURI().toString());
        applicationParameters.setParameters(parameters);

        if (firstUriReceived) {
            firstLoginParametersHandler.handleParameters();

            firstUriReceived = false;
        } else if (Objects.equals(Boolean.FALSE, loginValidationParameterHandler.handleParameters())) {
            LOG.info("Skipping processing of URI as it is for a different user");

            Platform.runLater(() -> new Alert(Alert.AlertType.WARNING, "jFed was launched via an URL. " +
                    "However, the URL specifies another user than the one currently logged in. " +
                    "Please quit this jFed session and try again.").showAndWait());

            return;
        }

        externalUserRequestHandler.handleParameters();
        environmentParameterHandler.handleParameters();
    }


}
