package leigh.tesseract;

import leigh.mecha.cred.Credential;
import leigh.mecha.cred.CredentialBinder;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * The HostGroup class holds a series of hosts and associated meta-information, such as login authorization.
 *
 * @author Alex Leigh
 */
public class HostGroup {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(HostGroup.class);
    private ArrayList<String> hosts = new ArrayList<>();
    private CredentialBinder<String> creds = new CredentialBinder<>();
    private HashMap<String, String> sshKeys = new HashMap<>();

    private String defaultSshKey;

    public ArrayList<String> getHosts() {
        return hosts;
    }

    public void addHost(String hostname) {
        hosts.add(hostname);
    }

    public Credential getCredential(String hostname) {
        return creds.getCredential(hostname);
    }

    public void setDefaultCredential(Credential defaultCredential) {
        creds.setDefaultCredential(defaultCredential);
    }

    public void setDefaultSshKey(String defaultSshKey) {
        this.defaultSshKey = defaultSshKey;
    }

    public void setSshKey(String hostname, String key) {
        sshKeys.put(hostname, key);
    }

    public CredentialBinder<String> getCreds() {
        return creds;
    }

    public String getSshKey(String hostname) {
        String key = sshKeys.get(hostname);

        if (key != null) {
            return key;
        }

        if (defaultSshKey != null) {
            return defaultSshKey;
        }

        return null;
    }
}
