package be.iminds.ilabt.jfed;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.lowlevel.GeniUser;
import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.lowlevel.TestCertHelper;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.util.GeniUrn;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Named;

import javax.annotation.Nonnull;
import javax.inject.Singleton;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.List;

/**
 * TestUserModule
 */
public class TestUserModule extends AbstractModule {

    public TestUserModule() {
    }

    @Override
    protected void configure() {
    }

    @Provides
    @Singleton
    public GeniUser provideGeniUser(final TestbedInfoSource testbedInfoSource, final TestCertHelper testCertHelper) {
        return new GeniUser() {
            @Nonnull
            @Override
            public PrivateKey getPrivateKey() {
                assert testCertHelper != null;
                return testCertHelper.userKeys.getSshPrivateKey();
            }

            @Nonnull
            @Override
            public PublicKey getPublicKey() {
                return testCertHelper.userKeys.getSshPublicKey();
            }

            @Nonnull
            @Override
            public List<X509Certificate> getClientCertificateChain() {
                return Collections.singletonList(testCertHelper.userCert);
            }

            @Override
            public Server getUserAuthorityServer() {
                return testbedInfoSource.getFromAnyUrnExact(TestCertHelper.authority1Urn);
            }

            @Nonnull
            @Override
            public String getUserUrnString() {
                return TestCertHelper.userUrn;
            }

            @Nonnull
            @Override
            public GeniUrn getUserUrn() {
                try {
                    return new GeniUrn(getUserUrnString());
                } catch (GeniUrn.GeniUrnParseException ex) {
                    throw new RuntimeException(ex);
                }
            }

            @Override
            public File getPrivateKeyFile() {
                return null;
            }

            @Override
            public File getCertificateFile() {
                return null;
            }
        };
    }

    @Provides
    @Named("userServer")
    public Server provideUserServer(final GeniUser geniUser) {
        return geniUser.getUserAuthorityServer();
    }

    @Provides
    public GeniUserProvider provideGeniUserProvider(final GeniUser geniUser) {
        return new GeniUserProvider() {
            @Override
            public GeniUser getLoggedInGeniUser() {
                return geniUser;
            }

            @Override
            public boolean isUserLoggedIn() {
                return geniUser != null;
            }
        };
    }
}
