import java.applet.Applet;
import java.applet.AppletStub;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.JFrame;

/**
 * An Applet used for loading the RS3 Client.
 * @author Frosty Teh Snowman <Skype:travis.mccorkle>
 */
public class RS3Applet extends Applet implements AppletStub {


	private static final long serialVersionUID = 1670498001014004354L;

	public static boolean RSPS = false;
	
	/**
	 * Chooses ip based on if Rs or Rsps
	 */
	public static String Host = RSPS ? "127.0.0.1" : "world2.runescape.com";
	
	/**
	 * The parameters of the client.
	 */
	private Properties client_parameters = new Properties();
	
	/**
	 * The current frame of the client application.
	 */
	public JFrame clientFrame = null;
	
	/**
	 * The main entry point of the current application.
	 * @param args The command line arguments.
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	public static void main(String... args) throws MalformedURLException, IOException {
		RS3Applet rs3applet = new RS3Applet();
		rs3applet.readParameters();
		rs3applet.openFrame();
		rs3applet.startClient();
		rs3applet.clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * Starts the actual client.
	 */
	private void startClient() {
		try {
			Class<?> client_class = Class.forName("client");

            Object v_client = client_class.getConstructor().newInstance();
            client_class.getSuperclass().getMethod("supplyApplet", Applet.class).invoke(v_client, this);
            client_class.getMethod("init").invoke(v_client);
            client_class.getMethod("start").invoke(v_client);			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Opens the actual frame application.
	 */
	private void openFrame() {
		clientFrame = new JFrame("841 Deob");
		clientFrame.add(this);
		clientFrame.setVisible(true);
		clientFrame.setSize(800, 600);		
	}

	/**
	 * Reads the parameters text file, and stores the parameters.
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	private void readParameters() throws MalformedURLException, IOException {
		if (!RSPS) {
			for (String line : fetchPageDetails()) {
				if (line.contains("<param name=")) {
					String key = line.split("<param name=\"")[1].split("\" ")[0];
					String value = line.split("value=\"")[1].split("\">'")[0];
					if (value.isEmpty())
						value = "";
					client_parameters.put(key, value);
				}
			}
		} else {

		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.applet.AppletStub#appletResize(int, int)
	 */
	@Override
	public void appletResize(int dimensionX, int dimensionY) {
		super.resize(new Dimension(dimensionX, dimensionY));
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.applet.Applet#getParameter(java.lang.String)
	 */
	@Override
	public String getParameter(String paramName) {
		return (String) client_parameters.get(paramName);
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.applet.Applet#getDocumentBase()
	 */
	@Override
	public URL getDocumentBase() {
		try {
			return new URL("http://"+Host);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.applet.Applet#getCodeBase()
	 */
	@Override
	public URL getCodeBase() {
		try {
			return new URL("http://"+Host);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Fetches the current parameters from the specified address
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public static ArrayList<String> fetchPageDetails() throws MalformedURLException, IOException {
		ArrayList<String> pageSource = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://"+Host).openStream()));
		String line;
		while ((line = reader.readLine()) != null)
			pageSource.add(line);
		reader.close();
		return pageSource;
	}
	
}
