package jagex;

import java.applet.Applet;
import java.applet.AppletStub;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class Launcher extends Applet implements AppletStub {

	/**
	 * @author: Pax M
	 */

	private static final long serialVersionUID = -8674701302975553393L;

	public Properties parameters = new Properties();
	public JFrame frame = null;

	public static int REVISION = 831, MINOR_VERSION = 2;

	public static boolean localHost = false, helloMrDecompilerGoFuckYourself = true;
	public static final boolean ISAAC = true;

	private static String IPA = helloMrDecompilerGoFuckYourself ? "127.0.0.1" : "192.99.228.161"; // 130.211.99.188
	private static String IPA_ADDRESS = "http://" + IPA;

	public static void main(String... args) {
		Launcher rs3applet = new Launcher();
		try {
			rs3applet.doFrame();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void doFrame() throws IOException {
		getParameters();
		openFrame();
		startClient();
		InputStream imageStream = Launcher.class
				.getResourceAsStream("images/64.png");
		BufferedImage bufferedImage = ImageIO.read(imageStream);
		frame.setIconImage(bufferedImage);
	}

	@Override
	public void init() {
		getParameters();
		startClient();
	}

	private void startClient() {
		try {
			client kagani = new client();
			kagani.supplyApplet(this);
			kagani.init();
			kagani.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void openFrame() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice();
		int width = gd.getDisplayMode().getWidth() / 2 + 100;
		int height = gd.getDisplayMode().getHeight() - 150;

		frame = new JFrame("Kagani - " + REVISION);
		frame.add(this);
		frame.setVisible(true);
		frame.setMinimumSize(new Dimension(width / 2, height / 2));
		frame.setSize(new Dimension(width, height));
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void getParameters() {
		parameters
				.put("java_arguments",
						"-Xmx768m -Xss100m -nojvm -Dsun.java2d.noddraw=true -XX:CompileThreshold=1500 -Xincgc -XX:+UseConcMarkSweepGC -XX:+UseParNewGC");
		parameters.put("centerimage", "true");
		parameters
				.put("image", "http://www.runescape.com/img/game/splash6.gif");
		parameters.put("separate_jvm", "true");
		parameters.put("boxborder", "false");
		parameters.put("boxbgcolor", "black");
		parameters
				.put("28",
						"C42C1E534C0AE9F5D937FDD53A2AB061EEC84D7787AD4E2825B3AFDF3A5A919C257A6ABCB0E2CB50715B5EDE3880BB8F");
		parameters.put("18", "0irU8SxE4on1Tl9/y7sxdiPtbP2kt8WC");
		parameters.put("4", "false");
		parameters.put("25", "0");// Language
		parameters.put("41", "false");
		parameters.put("36", localHost ? "3" : "1");
		parameters.put("39", localHost ? "3" : "1");
		parameters.put("40", IPA);
		parameters.put("-1", "3fOGqwfW5MMZ8acLynrCzA");
		parameters
				.put("24",
						"halign=true|valign=true|image=rs_logo.gif,0,-43|rotatingimage=rs3_loading_spinner.gif,0,47,9.6|progress=true,Verdana,13,0xFFFFFF,0,51");// "halign=true|valign=true|image=rs_logo.gif,0,-43|rotatingimage=rs3_loading_spinner.gif,0,47,9.6|progress=true,Verdana,13,0xFFFFFF,0,51");
		parameters.put("23", "1237575166");
		parameters.put("14", "0");
		parameters.put("42", "kaganicache");
		parameters.put("27", localHost ? "3" : "1");
		parameters.put("12", "false");
		parameters.put("38", "0");
		parameters.put("32", "true");
		parameters.put("7", "");
		parameters.put("21", ".runescape.com");
		parameters
				.put("37",
						"http://services.runescape.com/m=gamelogspecs/clientstats?data=");
		parameters.put("31", "false");
		parameters.put("22", "true");
		parameters.put("20", IPA);
		parameters.put("3", "0");
		parameters.put("34", IPA_ADDRESS);
		parameters.put("5", "-1082895259");
		parameters.put("1", "");// Display
		parameters.put("30", "0");
		parameters.put("26", "wwGlrZHF5gKN6D3mDdihco3oPeYN2KFybL9hUUFqOvk");
		parameters.put("43", "1119795612");
		parameters.put("29", "false");
		parameters.put("2", "0");
		parameters.put("35", "");
		parameters.put("33", "225");
		parameters.put("13", "1");
		parameters.put("15", IPA_ADDRESS);
		parameters.put("9", "");
		parameters.put("11", "false");
		parameters.put("16", "false");
		parameters.put("0", "retUtMMUGOukXomtmWaUig");
	}

	@Override
	public void appletResize(int dimensionX, int dimensionY) {
		super.resize(new Dimension(dimensionX, dimensionY));
	}

	@Override
	public String getParameter(String paramName) {
		return (String) parameters.get(paramName);
	}

	@Override
	public URL getDocumentBase() {
		try {
			return new URL(IPA_ADDRESS);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public URL getCodeBase() {
		try {
			return new URL(IPA_ADDRESS);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
}