package be.iminds.jfed.gts_highlevel;

import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnectionProvider;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.JFedTrustStore;
import be.iminds.jfed.gts_highlevel.controller.GtsConnectionProvider;
import be.iminds.jfed.gts_highlevel.controller.GtsConnectionProviderImpl;
import be.iminds.jfed.gts_highlevel.controller.GtsLoginConnectionProvider;
import be.iminds.jfed.gts_highlevel.controller.GtsReservationManagerConnectionProvider;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * User: twalcari
 * Date: 9/23/2015
 * Time: 11:12
 */
public class GtsModule extends AbstractModule {
    public static final String LAB2_GTS = "http://195.113.161.166:8191";
    public static final String LAB1_GTS = "http://195.113.161.164:8171";
    public static final String PRODUCTION_GTS = "http://gts.geant.net:8171";

    @Provides
    @GtsLoginConnectionProvider
    GtsConnectionProvider provideLoginConnectionProvider(JFedTrustStore jFedTrustStore,
                                                         GeniUserProvider geniUserProvider,
                                                         JFedConnectionProvider connectionProvider, JFedPreferences jFedPreferences) {
        String loginBaseUrl = jFedPreferences.hasKey(GtsPreferenceKey.PREF_GTS_ENDPOINT) ?
                jFedPreferences.getString(GtsPreferenceKey.PREF_GTS_ENDPOINT) : PRODUCTION_GTS;


        try {
            return new GtsConnectionProviderImpl(new URL(loginBaseUrl), jFedTrustStore, geniUserProvider, connectionProvider);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Could not parse loginBaseUrl", e);
        }
    }

    @Provides
    @GtsReservationManagerConnectionProvider
    GtsConnectionProvider provideReservationManagerConnectionProvider(JFedTrustStore jFedTrustStore,
                                                                      GeniUserProvider geniUserProvider,
                                                                      JFedConnectionProvider connectionProvider, JFedPreferences jFedPreferences) {
        String reservationManagerBaseUrl = jFedPreferences.hasKey(GtsPreferenceKey.PREF_GTS_ENDPOINT) ?
                jFedPreferences.getString(GtsPreferenceKey.PREF_GTS_ENDPOINT) : PRODUCTION_GTS;
        reservationManagerBaseUrl += "/rm";


        try {
            return new GtsConnectionProviderImpl(new URL(reservationManagerBaseUrl), jFedTrustStore, geniUserProvider, connectionProvider);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Could not parse reservationManagerBaseUrl", e);
        }
    }


    @Override
    protected void configure() {
    }
}
