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

import be.iminds.ilabt.jfed.ApplicationParametersModule;
import be.iminds.ilabt.jfed.experimenter_gui.util.ConnectivityDetector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * User: twalcari
 * Date: 6/26/2015
 * Time: 14:42
 */
@Singleton
public class EnvironmentParameterHandler implements ParameterHandler<Void> {
    private static final Logger LOG = LoggerFactory.getLogger(EnvironmentParameterHandler.class);
    private final JFedEnvironment environment;
    private final ConnectivityDetector connectivityDetector;
    private final ApplicationParametersModule.ApplicationParameters applicationParameters;

    @Inject
    public EnvironmentParameterHandler(JFedEnvironment environment,
                                       ConnectivityDetector connectivityDetector,
                                       ApplicationParametersModule.ApplicationParameters applicationParameters) {
        this.environment = environment;
        this.connectivityDetector = connectivityDetector;
        this.applicationParameters = applicationParameters;
    }


    @Override
    public Void handleParameters() {
        Map<String, String> params = applicationParameters.getParameters();
        LOG.debug("Handling environment parameters: "+params);

        if(params.containsKey(StartupParameters.PARAM_SLICE_DURATION)){
            try {
                environment.setDefaultSliceDuration(Integer.parseInt(params.get(StartupParameters.PARAM_SLICE_DURATION)));
            }catch(NumberFormatException ex){
                LOG.warn("Could not parse given default slice duration '{}'!",
                        params.get(StartupParameters.PARAM_SLICE_DURATION), ex);
            }

        }

        if(params.containsKey(StartupParameters.PARAM_LOGIN_URL)){
            environment.setLoginUrl(params.get(StartupParameters.PARAM_LOGIN_URL));
        }

        if(params.containsKey(StartupParameters.PARAM_CUSTOM_CONFIG)){
            environment.setCustomConfigurationFile(params.get(StartupParameters.PARAM_CUSTOM_CONFIG));
            LOG.debug("Using custom config file: " + params.get(StartupParameters.PARAM_CUSTOM_CONFIG));
        }

        return null;
    }
}
