package cz.hsrs.db.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ServerUtil extends DBUtil {

	public ServerUtil() {}

	public List<URL> getBackupUrl() throws SQLException, MalformedURLException {
		LinkedList<URL> urls = new LinkedList<URL>();
		String query = "SELECT * FROM server.backup_server;";
		ResultSet res = stmt.executeQuery(query);
		while (res.next()) {
			urls.add(new URL(res.getString("url")));
		}
		return urls;
	}

	public void callServers(List<URL> urls, final String query) throws MalformedURLException {
		for (final URL url : urls) {
			final URL urlreq = new URL(url.toString() + "?" + query);
			(new CallServer(urlreq, System.out)).start();

		}		

	}

	private static class CallServer extends Thread {

		private final URL url;
		private final PrintStream ps;
		private final Map<String, String> resp;

		CallServer(URL url, PrintStream ps) {
			this.url = url;
			this.ps = ps;
			this.resp = new HashMap<>();
		}

		public void run() {
			try {
				final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
				ps.println(url.toString()+ "..." + in.readLine());
			} catch (IOException e) {
				ps.println(url.toString()+ "..." + e.getMessage());;				
			}
		}

		public Map<String, String> getResp() {
			return resp;
		}				
	}
}
