import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import javax.swing.*;

public class FreeSpaceOpenInstaller {
	//Name of this Installer.
	private static final String name = "FreeSpace Open Installer";
	//Default directory to install to.
	private static final String defaultDir = "C:\\Games\\FreeSpace2\\";
	//URL of the latest version of this Installer.
	private static final String latestURL = "http://www.fsoinstaller.com/files/installer/java/";
	//URL of the directory where version.txt, filenames.txt, and endnote.txt reside.
	private static final String homeURL = "http://www.fsoinstaller.com/files/installer/java/";
	//Version of the Installer.
	private static final String version = "3.6.9 Config";
	private static final String type = "JAR";
	private static final JTextArea status = new JTextArea(20, 50);
	private static final JTextArea installnotes = new JTextArea(20, 50);
	private static final int FILTER = 1024;
	private static InstallerGUI gui;
	private static String proxypass = null;
	
	public static void main(String[] args){
		if ((new File("proxy.txt")).exists())
			getProxyInfo();
		ArrayList<InstallerPage> pages = new ArrayList<InstallerPage>();
		pages.add(new IntroPage());
		pages.add(new DirPage());
		pages.add(new ConfigPage());
		pages.add(new SelectPage());
		pages.add(new InstallPage());
		pages.add(new ExitPage());
		gui = new InstallerGUI(pages);
	}
	public static String name()
	{
		return name;
	}
	public static String defaultDir()
	{
		return defaultDir;
	}
	public static String latestURL()
	{
		return latestURL;
	}
	public static String homeURL()
	{
		return homeURL;
	}
	public static String type()
	{
		return type;
	}
	public static JTextArea status()
	{
		return status;
	}
	public static JTextArea installnotes()
	{
		return installnotes;
	}
	public static String version()
	{
		return version;
	}
	public static InstallerGUI GUI()
	{
		return gui;
	}
	
	public static void download(String address, String localFileName) {
		OutputStream out = null;
		URLConnection conn = null;
		InputStream  in = null;
		try {
			URL url = new URL(address);
			try{
			out = new BufferedOutputStream(
				new FileOutputStream(localFileName));
			}catch(FileNotFoundException e){
				(new File(localFileName)).createNewFile();
				out = new BufferedOutputStream(
						new FileOutputStream(localFileName));
			}
			conn = getConnection(url);
			in = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int numRead, count = 0;
			long numWritten = 0;
			while ((numRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, numRead);
				numWritten += numRead;
				if (count == FILTER){
					count = 0;
					FreeSpaceOpenInstaller.replaceline(FreeSpaceOpenInstaller.status, "Downloading: " + localFileName + "\t " + (int)(((double)numWritten/conn.getContentLength())*100) + "% Done" + "\t " + ((double)numWritten)/1024 + " KB");
				} else count++;
			}
			FreeSpaceOpenInstaller.replaceline(FreeSpaceOpenInstaller.status, "Downloaded: " + localFileName + "\t\t" + ((double)numWritten)/1024 + " KB\n");
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException ioe) {
			}
		}
	}
	
	public static void replaceline(JTextArea area, String line){
		try {
			area.replaceRange(null, area.getLineStartOffset(area.getLineCount() - 1), area.getLineEndOffset(area.getLineCount() - 1));
			area.replaceRange(line, area.getLineStartOffset(area.getLineCount() - 1), area.getLineEndOffset(area.getLineCount() - 1));
		}
		catch (Exception E){E.printStackTrace();}
	}
	
	private static void getProxyInfo()
	{
		THFileIO proxyfile = new THFileIO("proxy.txt");
		System.getProperties().put( "proxySet", "true" );
		String host = proxyfile.readFromFile();
		System.getProperties().put( "proxyHost", host.substring(0, host.indexOf(":")));
		System.getProperties().put( "proxyPort", host.substring(host.indexOf(":") + 1));
		if (proxyfile.hasNext())
			proxypass = Codecs.base64Encode(proxyfile.readFromFile());
	}
	public static URLConnection getConnection(URL url)
	{
		URLConnection con = null;
		try {
			con = url.openConnection();
			if (proxypass != null)
			{
				con.setRequestProperty("Proxy-Authorization", proxypass);
			}
		}
		catch( IOException E) {
			E.printStackTrace();
		}
		return con;
	}
}
