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

import be.iminds.ilabt.jfed.ApplicationParametersModule;
import be.iminds.ilabt.jfed.experimenter_gui.events.OpenRequestRspecFromUrlEvent;
import be.iminds.ilabt.jfed.experimenter_gui.events.RecoverSliceEvent;
import com.google.common.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * User: twalcari
 * Date: 6/26/2015
 * Time: 12:24
 */
public class GeneralParameterHandler implements ParameterHandler<Void> {
    private static final Logger LOG = LoggerFactory.getLogger(GeneralParameterHandler.class);

    private final EventBus eventBus;
    private final ApplicationParametersModule.ApplicationParameters applicationParameters;

    @Inject
    public GeneralParameterHandler(EventBus eventBus,
                                   ApplicationParametersModule.ApplicationParameters applicationParameters) {
        this.eventBus = eventBus;
        this.applicationParameters = applicationParameters;
    }


    @Override
    public Void handleParameters() {
        Map<String, String> parameters = applicationParameters.getParameters();
        //start with  the recovery of a slice if a slice_urn was provided as a startup parameter
        if (parameters.containsKey(StartupParameters.PARAM_SLICE_URN)) {
            String sliceUrnParam = parameters.get(StartupParameters.PARAM_SLICE_URN);
            sliceUrnParam = sliceUrnParam.trim();
            if (!sliceUrnParam.isEmpty()) {
                eventBus.post(new RecoverSliceEvent(sliceUrnParam));
            } else {
                LOG.warn("Got an empty slice-urn");
            }
        }

        //open an rspec if a rspec-parameter was provided as a startup parameter
        if (parameters.containsKey(StartupParameters.PARAM_RSPEC)) {
            String rspecLocation = parameters.get(StartupParameters.PARAM_RSPEC);
            rspecLocation = rspecLocation.trim();
            if (!rspecLocation.isEmpty()) {
                eventBus.post(new OpenRequestRspecFromUrlEvent(rspecLocation));
            } else {
                LOG.warn("Got an empty rspec location");
            }
        }
        return null;
    }
}
