package info.knacki.pass.git;

import android.support.annotation.NonNull;

import java.util.logging.Level;
import java.util.logging.Logger;

import info.knacki.pass.settings.SettingsManager;

public class GitInterfaceFactory {
    private final static Logger log = Logger.getLogger(GitInterfaceFactory.class.getName());

    public static class GitInterfaceException extends Throwable {
        GitInterfaceException(String msg) {
            super(msg);
        }

        GitInterfaceException(Throwable e) {
            super(e.getMessage(), e);
        }
    }

    public static @NonNull  GitInterface factory(SettingsManager.Git config) throws GitInterfaceException {
        if (config == null)
            throw new GitInterfaceException("No configuration found");
        try {
            if (config.GetUrl().startsWith("http://") || config.GetUrl().startsWith("https://"))
                return new HttpGitProtocol(config);
            return new SSHGitProtocol(config);
        }
        catch (Throwable e) {
            log.log(Level.SEVERE, "Cannot create git interface: " +e.getMessage(), e);
            throw new GitInterfaceException(e);
        }
    }
}
