package re.anywhere.client;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;

public class getip {
	static public String getAllIpAddress(String divider) {
		return getLocalIpAddress(divider)+divider+getextip(divider);
	}

	static public String getLocalIpAddress(String divider) {
		String derp = " ";
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						derp = derp  + inetAddress.getHostAddress().toString() + " ";
					}
				}
			}
		} catch (Exception ex) {
			return "NO CONNECTION!";
		}
		derp = derp.trim().replace(" ",divider);
		return derp;
	}


	public static String geturl(String url)
	{
		String derp = "";
		try{
			URL eatme = new URL(url);


			URLConnection conn = eatme.openConnection();
			// setting these timeouts ensures the client does not deadlock indefinitely
			// when the server has problems.
			conn.setConnectTimeout(3000);
			conn.setReadTimeout(3000);

			BufferedReader in = new BufferedReader(
					new InputStreamReader(
							conn.getInputStream()),128);

			String inputLine;
			while ((inputLine = in.readLine()) != null)
			{
				derp = derp + inputLine;
				System.out.println(inputLine);
			}
			in.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			derp = "";//derp + e.getMessage();
		}
		return derp;
	}


	// from http://code.google.com/p/wget-java/source/browse/trunk/wgetJava/edu/depauw/csc/dcheeseman/wgetjava/WGETJava.java
	public static boolean DownloadFile(String url, String theFile) {
		URLConnection con;
		try{

			URL theURL = new URL(url);
			con = theURL.openConnection();
			con.connect();
			con.setConnectTimeout(3000);
			con.setReadTimeout(3000);


			String type = con.getContentType();
			//System.out.println(type);


			if (type != null) {
				byte[] buffer = new byte[4 * 1024];
				int read;


//				String theFile = filepath;


				FileOutputStream os = new FileOutputStream(theFile);
				InputStream in = con.getInputStream();


				while ((read = in.read(buffer)) > 0) {
					os.write(buffer, 0, read);
				}


				os.close();
				in.close();


				return true;
			} else {
				return false;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}


	public static String getextip(String eventualseparator)
	{
		String derp0 = "";
		String derp1 = geturl("http://f3.to/ip/");
		String derp2 = geturl("http://robots-everywhere.com/ip/");
		if (derp1.contentEquals(derp2))
			derp0 = derp0 + derp1;
		else
		{
			boolean d1 = derp1.matches("\\d\\d?\\d?.\\d\\d?\\d?.\\d\\d?\\d?.\\d\\d?\\d?");
			boolean d2 = derp2.matches("\\d\\d?\\d?.\\d\\d?\\d?.\\d\\d?\\d?.\\d\\d?\\d?");

			if (d1 && d2)
				derp0 = derp1 + eventualseparator + derp2;
			else if (d1)
				derp0 = derp0 + derp1;
			else if (d2)
				derp0 = derp0 + derp2;
			else
				derp0 = "(no external IP?)";
		}
		return derp0;
	}


}
