package jguard.client;

import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.OSValidator;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Config {
    
    private Properties prop = new Properties();
    private String configFile = "config";
    private OutputStream output = null;
    private InputStream input = null;
    
    public static Map<String, String> conf = new HashMap<String, String>();

    public Config() {
        
    }
    
    private InputStream getFile(String configFile) throws Exception{
        if (OSValidator.isWindows()) {
            return new FileInputStream(configFile);
        } else if (OSValidator.isUnix()) {
            return new FileInputStream("/jGuard/"+configFile);
        }
        return null;
    }
    
    public String get(String name) {

	try {

            input = getFile(configFile);

            // load a properties file
            prop.load(input);

            return prop.getProperty(name);

	} catch (Exception ex) {
            LogFile.printErr("Config file exception: " + ex.getLocalizedMessage());
	} finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    LogFile.printErr("Config file exception: " + e.getLocalizedMessage());
                }
            }
	}
        return "";
    }
    
    public void set(String name, String value) {
        try {

            output = new FileOutputStream(configFile);

            // set the properties value
            prop.setProperty(name, value);

            // save properties to project root folder
            prop.store(output, null);

	} catch (IOException io) {
            LogFile.printErr("Config file exception: " + io.getLocalizedMessage());
	} finally {
            if (output != null) {
                try {
                        output.close();
                } catch (IOException e) {
                    LogFile.printErr("Config file exception: " + e.getLocalizedMessage());
                }
            }
	}
    }
    
    public Properties loadAll(){
        try {

            input = getFile(configFile);
            
            if (input == null) {
                LogFile.printErr("Config file error: input is null");
                    return null;
            }
            prop.load(input);
            
            return prop;
	} catch (Exception ex) {
            LogFile.printErr("Config file exception: " + ex.getLocalizedMessage());
            return null;
	} finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    LogFile.printErr("Config file exception: " + e.getLocalizedMessage());
                }
            }
	}
    }
    
    public void StoreLogin(){
        
    }
    
    public void store(Properties prop){
        try {

		output = new FileOutputStream(configFile);

		// save properties to project root folder
		prop.store(output, null);

	} catch (IOException io) {
		System.err.println("Config file exception: " + io.getLocalizedMessage());
	} finally {
		if (output != null) {
			try {
				output.close();
			} catch (IOException e) {
				System.err.println("Config file exception: " + e.getLocalizedMessage());
			}
		}

	}
    }

}
