package be.iminds.ilabt.jfed.lowlevel.user;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.lowlevel.authority.AuthorityFinder;
import be.iminds.ilabt.jfed.util.GeniUrn;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * User: twalcari
 * Date: 7/25/2016
 * Time: 11:25
 */
@Singleton
public class UserLoginInfoFactory {

    private final GeniUserFactory geniUserFactory;
    private final AuthorityFinder authorityFinder;

    @Inject
    UserLoginInfoFactory(GeniUserFactory geniUserFactory, AuthorityFinder authorityFinder) {
        this.geniUserFactory = geniUserFactory;
        this.authorityFinder = authorityFinder;
    }

    public KeyCertFileUserLoginInfo createKeyCertFileUserLoginInfo(File keyCertFile) throws IOException {
        return new KeyCertFileUserLoginInfo(keyCertFile, geniUserFactory, authorityFinder);
    }

    public KeyCertFileUserLoginInfo createKeyCertFileUserLoginInfo(File keyCertFile, GeniUrn userUrn, Server selectedServer) throws IOException {
        return new KeyCertFileUserLoginInfo(keyCertFile, userUrn, selectedServer, geniUserFactory);
    }

    public KeyCertUrlUserLoginInfo createKeyCertUrlUserLoginInfo(URL keyCertUrl) throws IOException {
        return new KeyCertUrlUserLoginInfo(keyCertUrl, geniUserFactory, authorityFinder);
    }

    public KeyCertUrlUserLoginInfo createKeyCertUrlUserLoginInfo(URL keyCertUrl, GeniUrn userUrn, Server selectedServer) throws IOException {
        return new KeyCertUrlUserLoginInfo(keyCertUrl, userUrn, selectedServer, geniUserFactory);
    }

    public ManualUserLoginInfo createManualUserLoginInfo(@Nullable String keyCertContent) {
        return new ManualUserLoginInfo(keyCertContent, geniUserFactory, authorityFinder);
    }

    public ManualUserLoginInfo createManualUserLoginInfo(@Nullable String keyCertContent, @Nullable GeniUrn userUrn, Server userAuthority) {
        return new ManualUserLoginInfo(keyCertContent, userUrn, userAuthority, geniUserFactory);
    }

    public ManualUserLoginInfo createManualUserLoginInfo(UserLoginInfo userLoginInfo) {
        if (userLoginInfo.isUserAndAuthorityProvidedSeperately()) {
            return new ManualUserLoginInfo(userLoginInfo, geniUserFactory);
        } else {
            return new ManualUserLoginInfo(userLoginInfo, geniUserFactory, authorityFinder);
        }
    }



}
