package org.whitesource.teamcity.server;

import com.thoughtworks.xstream.XStream;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.ServerPaths;
import jetbrains.buildServer.serverSide.crypt.RSACipher;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.util.XmlUtil;
import org.jetbrains.annotations.NotNull;
import org.whitesource.teamcity.common.WssUtils;

import java.io.*;

/**
 * Global configuration for the plugin.
 *
 * @author Edo.Shor
 */
public class GlobalSettingsManager {

    /* --- Static members --- */

    private static final String LOG_COMPONENT = "GlobalConfig";

    private final static String CONFIG_FILE_NAME = "whitesource-config.xml";

    /* --- Members --- */

    private File configFile;

    private XStream xStream;

    private GlobalSettings globalSettings;

    /* --- Constructors --- */

    /**
     * Constructor
     *
     * @param serverPaths
     */
    public GlobalSettingsManager(@NotNull ServerPaths serverPaths) {
        xStream = new XStream();
        xStream.processAnnotations(GlobalSettings.class);
        xStream.setClassLoader(GlobalSettings.class.getClassLoader());

        configFile = new File(serverPaths.getConfigDir(), CONFIG_FILE_NAME);
        loadConfig();
    }

    /* --- Public methods --- */

    public void save() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(configFile);
            xStream.toXML(globalSettings, fos);
        } catch (FileNotFoundException e) {
            Loggers.SERVER.error(WssUtils.logMsg(LOG_COMPONENT,"Failed to save config file " + configFile), e);
        } finally {
            FileUtil.close(fos);
        }
    }

    public boolean isProxy() {

        return globalSettings.getProxy() != null &&
                !StringUtil.isEmptyOrSpaces(globalSettings.getProxy().getHost());
    }

    public String getHexEncodedPublicKey() {
        return RSACipher.getHexEncodedPublicKey();
    }

    /* --- Private methods --- */

    private void loadConfig(){
        if (configFile.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                globalSettings = (GlobalSettings) xStream.fromXML(fis);
            } catch (IOException e) {
                Loggers.SERVER.error(WssUtils.logMsg(LOG_COMPONENT,"Failed to load config file " + configFile), e);
            } finally {
                FileUtil.close(fis);
            }
        }
    }

    /* --- Getters / Setters --- */

    public GlobalSettings getGlobalSettings() {
        return globalSettings;
    }

    public void setGlobalSettings(GlobalSettings globalSettings) {
        this.globalSettings = globalSettings;
    }
}

@@ -330,52 +330,8 @@
 l;%0D%0A
-import jetbrains.buildServer.util.XmlUtil;%0D%0A
 impo
@@ -1651,32 +1651,33 @@
 g(LOG_COMPONENT,
+ 
 %22Failed to save 
@@ -1702,32 +1702,32 @@
 nfigFile), e);%0D%0A
-
         %7D finall
@@ -1813,26 +1813,24 @@
 sProxy() %7B%0D%0A
-%0D%0A
         retu
@@ -1824,32 +1824,75 @@
 %0A        return 
+globalSettings != null &&%0D%0A                
 globalSettings.g
@@ -2178,16 +2178,17 @@
 Config()
+ 
 %7B%0D%0A     
@@ -2438,32 +2438,32 @@
 Exception e) %7B%0D%0A
-
                 
@@ -2513,16 +2513,17 @@
 MPONENT,
+ 
 %22Failed 

Fix for NPE on empy global config