import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class WorldBuilder extends Applet
{
	TextField name, author, version, sizex, sizey;
	TextField px, py, pName, pro, natives, npro, startPriority;
	Button generate, updatePlanet;
	TextArea o;
	Maps m;

	public void passVar (MapCanvas canvas)
	{
		canvas.name = name;
		canvas.a = 16;
		canvas.b = 16;
		canvas.o = o;
		canvas.m = new Maps (16, 16);
		canvas.px = px;
		canvas.py = py;
		canvas.pName = pName;
		canvas.pro = pro;
		canvas.natives = natives;
		canvas.npro = npro;
		canvas.startPriority = startPriority;
		canvas.name = name;
		canvas.author = author;
		canvas.version = version;
		canvas.sizex = sizex;
		canvas.sizey = sizey;
	}


	public void init ()
	{

		setBackground (Color.gray);
		setLayout (new BorderLayout (3, 3));

		MapCanvas canvas = new MapCanvas ();
		add (canvas, BorderLayout.CENTER);

		name = new TextField ("name.xml", 15);
		author = new TextField ("Builder", 15);
		version = new TextField ("1.0", 6);
		sizex = new TextField ("16", 3);
		sizey = new TextField ("16", 3);

		px = new TextField ("0", 3);
		py = new TextField ("0", 3);
		pName = new TextField ("Nom", 1);
		pro = new TextField ("2", 1);
		natives = new TextField ("3", 1);
		npro = new TextField ("2", 1);
		startPriority = new TextField ("0", 1);

		generate = new Button ("Generate");
		generate.addActionListener (canvas);
		updatePlanet = new Button ("UpdatePlanet");
		updatePlanet.addActionListener (canvas);
		o = new TextArea ("Map will go here\nMap Will not show correctly when exceed size 250\nplanets will be 1 pixel at 250\n" +
				"Don't enter weird values or you might crash the program lol" +
				"\nGenerate will give you all you ened for xml\nUpdate , updates planet at that cordinate" +
				"\nNew to refresh grid"+
				"\nThis is so crappy plenty of updates to come :)"+
				"\nMapBuilder vBeta 0.1 by DeGhost Jan 05");

		Panel p1 = new Panel ();
		p1.add (generate);
		p1.add (new Label ("Name"));
		p1.add (name);
		p1.add (new Label ("Author"));
		p1.add (author);
		p1.add (new Label ("Version"));
		p1.add (version);
		p1.add (new Label ("size x"));
		p1.add (sizex);
		p1.add (new Label ("Size y"));
		p1.add (sizey);
		Button New = new Button ("New");
		New.addActionListener (canvas);
		p1.add (New);


		Panel pcord2 = new Panel ();
		Panel pcord1 = new Panel ();
		pcord1.add (new Label ("x"));
		pcord1.add (px);
		pcord2.add (new Label ("y"));
		pcord2.add (py);

		Panel p4 = new Panel (new BorderLayout ());
		Panel p3 = new Panel (new GridLayout (0, 2, 10, 20));


		p3.add (new Label ("Planet"));
		p3.add (new Label (" "));
		p3.add (pcord1);
		p3.add (pcord2);
		p3.add (new Label ("Name"));
		p3.add (pName);
		p3.add (new Label ("production"));
		p3.add (pro);
		p3.add (new Label ("Natives"));
		p3.add (natives);
		p3.add (new Label ("Natives Pro"));
		p3.add (npro);
		p3.add (new Label ("Start Prior"));
		p3.add (startPriority);
		p3.add (updatePlanet);
		p4.add ("North", p3);


		setLayout (new BorderLayout ());
		add ("North", p1);
		add ("Center", canvas);
		add ("West", p4);
		add ("South", o);
		passVar (canvas);
	} // init method


	public Insets getInsets ()
	{
		// Specify how wide a border to leave around the edges of the applet.
		return new Insets (3, 3, 3, 3);
	}
} // WorldBuilder class


public class MapCanvas extends Canvas implements MouseListener, ActionListener
{
	TextField name, author, version, sizex, sizey;
	TextField px, py, pName, pro, natives, npro, startPriority;
	TextArea o;
	Maps m;
	int a, b;
	Graphics g;

	MapCanvas ()
	{
		addMouseListener (this);
		setBackground (Color.white);
	}


	public void actionPerformed (ActionEvent evt)
	{
		// Respond when the user clicks on a button.
		String command = evt.getActionCommand ();
		if (command.equals ("Generate"))
		{
			m.create (name.getText ().trim (), author.getText ().trim (), version.getText ().trim (), sizex.getText ().trim (), sizey.getText ().trim (), o);
		}
		else if (command.equals ("UpdatePlanet"))
		{
			if (Integer.parseInt (px.getText ().trim ()) < a + 1 && Integer.parseInt (py.getText ().trim ()) < b + 1 &&
					Integer.parseInt (px.getText ().trim ()) > 0&& Integer.parseInt (py.getText ().trim ()) > 0)
				m.add (Integer.parseInt (px.getText ().trim ()),
						Integer.parseInt (py.getText ().trim ()),
						(pName.getText ().trim ()),
						Integer.parseInt (pro.getText ().trim ()),
						Integer.parseInt (natives.getText ().trim ()),
						Integer.parseInt (npro.getText ().trim ()),
						Integer.parseInt (startPriority.getText ().trim ()));
			repaint ();
		}
		else if (command.equals ("New"))
		{
			a = Integer.parseInt (sizex.getText ().trim ());
			b = Integer.parseInt (sizey.getText ().trim ());
			m = new Maps (a, b);
			repaint ();
		}
	}


	public void paint (Graphics out)
	{
		this.g = out;
		border ();
		int size = 500 / Math.max (a, b);
		for (int c1 = 0 ; c1 < a ; c1++)
			for (int c2 = 0 ; c2 < b ; c2++)
				if (m.planet [c1] [c2] != null && m.planet [c1] [c2].exist == true)
					plot (c1 + 1, b - c2, g, Color.red, size);

	}


	public void plot (int x, int y, Graphics out, Color c, int multi)
	{
		out.setColor (c);
		out.fillRect (x * multi, y * multi, multi - 1, multi - 1);
	}


	public void border ()
	{
		RenderingHints out = new RenderingHints (null);
		int size = 500 / Math.max (a, b);
		for (int c = 0 ; c < a + 2 ; c++)
		{
			plot (c, 0, g, Color.green, size);
			plot (c, b + 1, g, Color.green, size);
			g.setColor (Color.black);
			g.drawLine (c * size - 1, 0, c * size - 1, (b + 2) * size - 2);
		}
		for (int c = 0 ; c < b + 2 ; c++)
		{
			plot (0, c, g, Color.green, size);
			plot (a + 1, c, g, Color.green, size);
			g.setColor (Color.black);
			g.drawLine (0, c * size - 1, (a + 2) * size - 2, c * size - 1);
		}
	}


	public void mousePressed (MouseEvent evt)
	{
		int size = 500 / Math.max (a, b);
		px.setText (evt.getX () / size + "");
		py.setText (b + 1 - evt.getY () / size + "");

	}


	public void mouseReleased (MouseEvent evt)
	{
	}


	public void mouseClicked (MouseEvent evt)
	{
	}


	public void mouseEntered (MouseEvent evt)
	{
	} // Some empty routines.


	public void mouseExited (MouseEvent evt)
	{
	} //    (Required by the MouseListener


	public void mouseMoved (MouseEvent evt)
	{
	}
} // end class SimplePaintCanvas


class Maps
{
	public Planet[] [] planet;
	public int a, b;
	public Maps (int a, int b)
	{
		planet = new Planet [a] [b];
		this.a = a;
		this.b = b;
	}


	public void decon (TextArea o)
	{
	}


	public void create (String name, String author, String version, String sizex, String sizey, TextArea o)
	{
		o.setText ("");
		o.append ("<spacemap name=\"" + name + "\" author=\"" + author + "\" version=\"" + version + "\" sizex=\"" + sizex + "\" sizey=\"" + sizey + "\">");
		for (int c1 = 0 ; c1 < a ; c1++)
			for (int c2 = 0 ; c2 < b ; c2++)
				if (planet [c1] [c2] != null)
					o.append ("\n        <planet x=\"" + planet [c1] [c2].x +
							"\" y=\"" + planet [c1] [c2].y +
							"\" name=\"" + planet [c1] [c2].name +
							"\" baseproduction=\"" + planet [c1] [c2].baseproduction +
							"\" natives=\"" + planet [c1] [c2].natives +
							"\" nativeproduction=\"" + planet [c1] [c2].nativeproduction +
							"\" startpriority=\"" + planet [c1] [c2].startpriority +
							"\"/>");
		o.append ("\n</spacemap>");
	}


	public void add (int px, int py, String pName, int pro, int natives, int npro, int startPriority)
	{
		if (planet [px - 1] [py - 1] == null)
			planet [px - 1] [py - 1] = new Planet ();
		planet [px - 1] [py - 1].name = pName;
		planet [px - 1] [py - 1].natives = natives;
		planet [px - 1] [py - 1].baseproduction = pro;
		planet [px - 1] [py - 1].nativeproduction = npro;
		planet [px - 1] [py - 1].startpriority = startPriority;
		planet [px - 1] [py - 1].exist = true;
		planet [px - 1] [py - 1].x = px;
		planet [px - 1] [py - 1].y = py;
	}


	public void remove (int px, int py)
	{
		if (planet [px - 1] [py - 1] != null)
			planet [px - 1] [py - 1].exist = false;
	}


	public String remove (String s1, String s2, String s3)
	{
		if (s1 == s2 || s1 == s3)
			return "";
		return s1;
	}
}


class Planet
{
	public String name;
	public int natives, baseproduction, nativeproduction, startpriority;
	public int x, y;
	public boolean exist;
	public Planet ()
	{
		name = "";
		natives = -1;
		baseproduction = -1;
		nativeproduction = -1;
		startpriority = -1;
		x = -1;
		y = -1;
		exist = false;
	}
}
