package com.github.fengtan.solrgui;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;
import org.ini4j.Profile.Section;

public class SolrGUIConfig {

	private static final String filename = ".solrgui";
	// TODO make it work on windows (hidden file)
	private static final String path = System.getProperty("user.home") + File.separator + filename;

	public static List<SolrGUIServer> getServers() {
		// TODO make sure file is writable + create file if does not exist
		// TODO what if 2 servers with same config ? mess up ini file ?
		List<SolrGUIServer> servers = new ArrayList<SolrGUIServer>();
		try {
			Ini ini = new Ini(new File(path));
			for (Map.Entry<String, Section> entry:ini.entrySet()) {
				Section section = entry.getValue();
				try {
					URL url = new URL(section.get("protocol"), section.get("host"), Integer.parseInt(section.get("port")), section.get("path"));
					servers.add(new SolrGUIServer(url, entry.getKey().toString())); // TODO test server name works
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();					
				}
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return servers;
	}

	public static void addServer(SolrGUIServer server) {
		try {
			Ini ini = new Ini(new File(path));
			ini.add(server.getName(), "protocol", server.getURL().getProtocol());
			ini.add(server.getName(), "host", server.getURL().getHost());
			ini.add(server.getName(), "port", server.getURL().getPort());
			ini.add(server.getName(), "path", server.getURL().getPath());
			ini.store();
		} catch (InvalidFileFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
}

@@ -345,16 +345,64 @@
 nfig %7B%0A%0A
+%09// TODO make sure hidden file works on windows%0A
 %09private
@@ -449,337 +449,399 @@
 i%22;%0A
-%09// TODO make it work on windows (hidden file)%0A%09private static final String path = System.getProperty(%22user.home%22) + File.separator + filename;%0A%0A%09public static List%3CSolrGUIServer%3E getServers() %7B%0A%09%09// TODO make sure file is writable + create file if does not exist%0A%09%09// TODO what if 2 servers with same config ? mess up ini file ?
+%0A%09public static Ini getIni() throws IOException %7B%0A%09%09// TODO make sure file is writable%0A%09%09// TODO what if 2 servers with same config ? mess up ini file ?%0A%09%09String path = System.getProperty(%22user.home%22) + File.separator + filename;%0A%09%09File file = new File(path);%0A%09%09if (!file.exists()) %7B%0A%09%09%09file.createNewFile();%0A%09%09%7D%0A%09%09return new Ini(file);%0A%09%7D%0A%09%0A%09public static List%3CSolrGUIServer%3E getServers() %7B
 %0A%09%09L
@@ -922,38 +922,23 @@
 i ini = 
-new Ini(new File(path)
+getIni(
 );%0A%09%09%09fo
@@ -1499,16 +1499,35 @@
 %09%7D%0A%09%09%09%7D%0A
+%09%09%09return servers;%0A
 %09%09%7D catc
@@ -1534,33 +1534,32 @@
 h (IOException e
-1
 ) %7B%0A%09%09%09// TODO A
@@ -1580,33 +1580,32 @@
 catch block%0A%09%09%09e
-1
 .printStackTrace
@@ -1605,27 +1605,24 @@
 ckTrace();%0A%09
-%09%7D%0A
 %09%09return ser
@@ -1618,24 +1618,51 @@
 %09return 
-servers;
+new ArrayList%3CSolrGUIServer%3E();%0A%09%09%7D
 %0A%09%7D%0A%0A%09pu
@@ -1737,30 +1737,15 @@
 i = 
-new Ini(new File(path)
+getIni(
 );%0A%09

Create config file if does not exist.