package be.iminds.ilabt.jfed.ssh_terminal_tool.ssh_key_info;

import be.iminds.ilabt.jfed.lowlevel.ssh_key_info.InvalidSshKeyInfoException;
import be.iminds.ilabt.jfed.lowlevel.ssh_key_info.putty.PuTTYPrivateKeyFile;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * User: twalcari
 * Date: 10/11/2016
 * Time: 11:00
 */
public class PreferencesSshKeyInfoFactory {
    private static final Logger LOG = LoggerFactory.getLogger(PreferencesSshKeyInfoFactory.class);

    private PreferencesSshKeyInfoFactory() {
    }

    public static PreferencesPuttySshKeyInfo createPreferencesPuttySshKeyInfo(JFedPreferences jFedPreferences,
                                                                              JFedPreferences.PreferenceKey overridePref,
                                                                              JFedPreferences.PreferenceKey puttyKeyFilePref)
            throws InvalidSshKeyInfoException {

        boolean overrideVal = jFedPreferences.getBoolean(overridePref);
        assert overrideVal;

        File puttyKeyFile = jFedPreferences.getFile(puttyKeyFilePref);
        try {
            PuTTYPrivateKeyFile ppk = PuTTYPrivateKeyFile.read(puttyKeyFile);

            return new PreferencesPuttySshKeyInfo(puttyKeyFile, ppk);
        } catch (IOException e) {
            LOG.error("Failed to read putty key file: " + e.getMessage(), e);
            throw new InvalidSshKeyInfoException("Could not read PPK file", e);
        }
    }

    public static PreferencesSshKeyInfo createPreferencesSshKeyInfo(JFedPreferences jFedPreferences,
                                                                    JFedPreferences.PreferenceKey overridePref,
                                                                    JFedPreferences.PreferenceKey pubKeyFilePref,
                                                                    JFedPreferences.PreferenceKey privKeyFilePref,
                                                                    JFedPreferences.PreferenceKey pubKeyTextPref,
                                                                    JFedPreferences.PreferenceKey privKeyTextPref)
            throws InvalidSshKeyInfoException {
        boolean overrideVal = jFedPreferences.getBoolean(overridePref);
        assert overrideVal;

        File pubKeyFile = jFedPreferences.getFile(pubKeyFilePref);
        File privKeyFile = jFedPreferences.getFile(privKeyFilePref);

        String pubKeyText = jFedPreferences.getString(pubKeyTextPref);
        String privKeyText = jFedPreferences.getString(privKeyTextPref);

        if (privKeyFile == null && privKeyText == null) {
            throw new InvalidSshKeyInfoException("No private key was provided");
        }

        if (privKeyText == null) {
            try {
                privKeyText = IOUtils.fileToString(privKeyFile);
            } catch (IOException e) {
                LOG.debug("Could not open private key file from preferences: {}", privKeyFile.getPath(), e);
                throw new InvalidSshKeyInfoException("Could not read private key file " + privKeyFile.getAbsolutePath(), e);
            }
        }

        if (pubKeyText == null && pubKeyFile != null) {
            try {
                pubKeyText = IOUtils.fileToString(pubKeyFile);
            } catch (IOException e) {
                LOG.debug("Could not open public key file from preferences: {}", pubKeyFile.getPath(), e);
            }
        }

        return new PreferencesSshKeyInfo(privKeyFile, pubKeyFile, privKeyText, pubKeyText);
    }
}
