package be.iminds.ilabt.jfed.experimenter_gui;

import javax.inject.Singleton;
import java.io.IOException;
import java.util.Properties;

/**
 * JFedVersionInfo
 */
@Singleton
public class JFedVersionInfo {
    private static final String VERSION_PROPERTIES_FILE = "/version.properties";
    private static final String PROP_VERSION = "version";

    private static final String PROP_GIT_COMMIT = "build.git_commit";
    private static final String PROP_GIT_BRANCH = "build.git_branch";

    private static final String PROP_BUILD_JENKINS = "build.jenkins_build";

    private final String version;
    private final String jenkinsBuild;
    private final String gitCommit;
    private final String gitBranch;

    private final String fullVersionString;

    public JFedVersionInfo() {
        Properties prop = new Properties();
        //now load the version-properties file
        try {
            prop.load(getClass().getResourceAsStream(VERSION_PROPERTIES_FILE));
        } catch (IOException e) {
            throw new RuntimeException("Something went wrong fetching the jFed version: " + e.getMessage(), e);

        }

        this.version = prop.getProperty(PROP_VERSION, "Unknown");

        //now load the info into the disabled textfield
        StringBuilder fullVersionSB = new StringBuilder();
        fullVersionSB.append(this.version);

        jenkinsBuild = prop.getProperty(PROP_BUILD_JENKINS);
        if (jenkinsBuild != null && !jenkinsBuild.isEmpty())
            fullVersionSB.append("  - build #").append(jenkinsBuild);

        gitCommit = prop.getProperty(PROP_GIT_COMMIT);
        if (gitCommit != null && !gitCommit.isEmpty())
            fullVersionSB.append("  - git commit #").append(gitCommit);

        gitBranch = prop.getProperty(PROP_GIT_BRANCH);
        fullVersionSB.append(" on ").append(gitBranch);

        fullVersionString = fullVersionSB.toString();
    }

    public String getVersion() {
        return version;
    }

    public String getJenkinsBuild() {
        return jenkinsBuild;
    }

    public String getGitCommit() {
        return gitCommit;
    }

    public String getGitBranch() {
        return gitBranch;
    }

    public String getFullVersionString() {
        return fullVersionString;
    }
}
