package com.redpois0n.gscrot;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class Config {
	
	public static final Map<String, String> CONFIG = new HashMap<String, String>();
	public static final File CONFIG_FILE = new File(".gscrot");
	
	public static final String KEY_COPY_URL_TO_CLIPBOARD = "copy-url-to-clipboard";
	public static final String KEY_COPY_IMAGE_TO_CLIPBOARD = "copy-image-to-clipboard";
	public static final String KEY_PREFERRED_FORMAT = "preferred-format";
	public static final String KEY_DEFAULT_FILENAME = "image";
		
	/**
	 * Gets value from configuration
	 * @param k
	 * @param def default value if nothing is found in config
	 * @return
	 */
	public static String get(String k, String def) {
		if (CONFIG.containsKey(k)) {
			return CONFIG.get(k);
		} else {
			return def;
		}
	}
	
	/**
	 * Gets value as boolean from configuration
	 * @param k
	 * @param def default value
	 * @return
	 */
	public static boolean get(String k, boolean def) {
		return get(k, def + "").equalsIgnoreCase("true");
	}
	
	/**
	 * Sets value in configuration
	 * @param k
	 * @param v
	 */
	public static void put(String k, String v) {
		CONFIG.put(k, v);
	}
	
	public static void load() throws Exception {
		CONFIG.clear();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(CONFIG_FILE)));
		
		String line;
		
		while ((line = reader.readLine()) != null) {
			if (!line.startsWith("#") && line.contains("=")) {
				String k = line.substring(0, line.indexOf("="));
				String v = line.substring(line.indexOf("=") + 1, line.length());
								
				CONFIG.put(k, v);
			}
		}
		
		reader.close();
	}
	
	public static void save() throws Exception {
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(CONFIG_FILE)));
		
		writer.write("# gscrot generated config");
		writer.newLine();
		
		for (String k : CONFIG.keySet()) {
			writer.write(k + "=" + CONFIG.get(k));
			writer.newLine();
		}
		
		writer.close();
	}

}

@@ -28,70 +28,8 @@
 t;%0A%0A
-import java.io.BufferedReader;%0Aimport java.io.BufferedWriter;%0A
 impo
@@ -126,111 +126,23 @@
 ava.
-io.InputStreamReader;%0Aimport java.io.OutputStreamWriter;%0Aimport java.util.HashMap;%0Aimport java.util.Map
+util.Properties
 ;%0A%0Ap
@@ -162,17 +162,16 @@
 onfig %7B%0A
-%09
 %0A%09public
@@ -188,64 +188,42 @@
 nal 
-Map%3CString, String%3E CONFIG = new HashMap%3CString, String%3E
+Properties CONFIG = new Properties
 ();%0A
@@ -283,17 +283,16 @@
 crot%22);%0A
-%09
 %0A%09public
@@ -581,18 +581,16 @@
 image%22;%0A
-%09%09
 %0A%09/**%0A%09 
@@ -613,32 +613,37 @@
 m configuration%0A
+%09 * %0A
 %09 * @param k%0A%09 *
@@ -645,32 +645,47 @@
 k%0A%09 * @param def
+%0A%09 *           
  default value i
@@ -786,96 +786,46 @@
 %7B%0A%09%09
-if (CONFIG.containsKey(k)) %7B%0A%09%09%09return CONFIG.get(k);%0A%09%09%7D else %7B%0A%09%09%09return def;%0A%09%09%7D
+return CONFIG.getProperty(k, def);
 %0A%09%7D%0A
-%09
 %0A%09/*
@@ -863,32 +863,37 @@
 m configuration%0A
+%09 * %0A
 %09 * @param k%0A%09 *
@@ -903,16 +903,31 @@
 aram def
+%0A%09 *           
  default
@@ -1057,17 +1057,16 @@
 e%22);%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 
@@ -1091,24 +1091,29 @@
 nfiguration%0A
+%09 * %0A
 %09 * @param k
@@ -1196,25 +1196,24 @@
 t(k, v);%0A%09%7D%0A
-%09
 %0A%09public sta
@@ -1253,76 +1253,12 @@
 %7B%0A%09%09
-CONFIG.clear();%0A%09%09%0A%09%09BufferedReader reader = new BufferedReader(new 
+File
 Inpu
@@ -1268,15 +1268,15 @@
 ream
-Reader(
+ fis = 
 new 
@@ -1307,307 +1307,35 @@
 ILE)
-))
 ;%0A%09%09
-%0A%09%09String line;%0A%09%09%0A%09%09while ((line = reader.readLine()) != null) %7B%0A%09%09%09if (!line.startsWith(%22#%22) && line.contains(%22=%22)) %7B%0A%09%09%09%09String k = line.substring(0, line.indexOf(%22=%22));%0A%09%09%09%09String v = line.substring(line.indexOf(%22=%22) + 1, line.length());%0A%09%09%09%09%09%09%09%09%0A%09%09%09%09CONFIG.put(k, v);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09reader
+CONFIG.load(fis);%0A%09%09fis
 .clo
@@ -1343,17 +1343,16 @@
 e();%0A%09%7D%0A
-%09
 %0A%09public
@@ -1396,55 +1396,12 @@
 %7B%0A%09%09
-BufferedWriter writer = new BufferedWriter(new 
+File
 Outp
@@ -1412,15 +1412,15 @@
 ream
-Writer(
+ fos = 
 new 
@@ -1452,33 +1452,31 @@
 ILE)
-))
 ;%0A%09%09
-%0A%09%09writer.write(%22# 
+CONFIG.store(fos, %22
 gscr
@@ -1504,144 +1504,11 @@
 ;%0A%09%09
-writer.newLine();%0A%09%09%0A%09%09for (String k : CONFIG.keySet()) %7B%0A%09%09%09writer.write(k + %22=%22 + CONFIG.get(k));%0A%09%09%09writer.newLine();%0A%09%09%7D%0A%09%09%0A%09%09writer
+fos
 .clo

Use Properties instead of HashMap to reduce code.