using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BeginLevel : MonoBehaviour {

	List<Target> targets;

	enum Tiles {UNDECIDED, KEEPEMPTY, ALREADYFULL};

	static int gridsize = 7;
	static float gridsquare = 7.0f;

	// Use this for initialization
	void Start () {

		Globals g = FindObjectOfType(typeof(Globals)) as Globals;
		int wantTrailers = Random.Range(1,Mathf.Min(1+g.difficulty/2,4));

		if (g.difficulty == 2)
			wantTrailers = 2;

		int wantblocks = 1;
		if (g.difficulty != 0)
		{
			wantblocks = Random.Range ((gridsize*gridsize)/20,(gridsize*gridsize)/(10-Mathf.Min(9,g.difficulty)));
		}

		if (null != GameObject.Find("Halp"))
		{
			wantTrailers = 2;
			wantblocks = (gridsize*gridsize)/5;
		}

		targets = new List<Target>();

		transform.localScale = new Vector3(3*gridsize*gridsquare,1e-07f,3*gridsize*gridsquare);

		int[,] Grid = new int[gridsize,gridsize];

		Grid[0,0] = -1;
		Grid[1,0] = -1;
		Grid[0,1] = -1;

		Grid[gridsize-1,gridsize-1] = -1;
		Grid[gridsize-1,gridsize-2] = -1;
		Grid[gridsize-2,gridsize-1] = -1;

		Grid[0,gridsize-1] = -1;
		Grid[gridsize-1,0] = -1;

		// create the player
		bool done = false;
		do
		{
			int x = Random.Range(0,gridsize);
			int y = Random.Range(0,gridsize);
			if (Grid[x,y] == 0)
			{
				Grid[x,y] = 1;
				Vector3 pos = new Vector3( (x-(gridsize/2))*gridsquare, 1.0f, (y-(gridsize/2))*gridsquare );
				Quaternion rot = Quaternion.identity;
				rot.eulerAngles = new Vector3(0.0f,Random.value * 360.0f,0.0f);
				Instantiate(Resources.Load("Prefabs/Truck"), pos, rot);
				done = true;
			}
		}
		while (!done);

		int haveTrailers = 0;
		int colourOffset = Random.Range(1,4);

		while (haveTrailers < wantTrailers)
		{
			int x = Random.Range(0,gridsize);
			int y = Random.Range(0,gridsize);

			int x2 = Random.Range(0,gridsize);
			int y2 = Random.Range(0,gridsize);
			if (Grid[x,y] == 0 && Grid[x2,y2] == 0 && (x != x2 || y != y2))
			{
				Grid[x,y] = 1;
				Grid[x2,y2] = 1;

				Vector3 pos = new Vector3( (x-(gridsize/2))*gridsquare, 1.0f, (y-(gridsize/2))*gridsquare );
				Quaternion rot = Quaternion.identity;
				rot.eulerAngles = new Vector3(0.0f,Random.Range (0,4) * 90.0f,0.0f);
				GameObject trailer = Instantiate(Resources.Load("Prefabs/Trailer"), pos, rot) as GameObject;
				trailer.GetComponent<Trailer>().SetColour((haveTrailers + colourOffset) % 3);

				pos = new Vector3( (x2-(gridsize/2))*gridsquare, 0.0f, (y2-(gridsize/2))*gridsquare );
				rot = Quaternion.identity;
				GameObject target = Instantiate(Resources.Load("Prefabs/Target"), pos, rot) as GameObject;
				target.GetComponent<Target>().SetTrailer(trailer);

				targets.Add (target.GetComponent<Target>());

				haveTrailers += 1;
			}
		}

		/*
		if (Random.value < 0.2)
		{
			done = false;
			do
			{
				int x = Random.Range(0,gridsize);
				int y = Random.Range(0,gridsize);
				if (Grid[x,y] == 0)
				{
					Grid[x,y] = 1;
					Vector3 pos = new Vector3( (x-(gridsize/2))*gridsquare, 1.0f, (y-(gridsize/2))*gridsquare );
					Quaternion rot = Quaternion.identity;
					rot.eulerAngles = new Vector3(0.0f,Random.Range (0,4) * 90.0f,0.0f);
					Instantiate(Resources.Load("Prefabs/Ramp"), pos, rot);
					done = true;
				}
			}
			while (!done);
		}
		*/


		int haveblocks = 0;

		int tries = 500;

		while (haveblocks < wantblocks && tries > 0)
		{
			tries --;
			int x = Random.Range(0,gridsize);
			int y = Random.Range(0,gridsize);

			if (Grid[x,y] == 0)
			{
				Grid[x,y] = 5;
				haveblocks++;
			}

			try
			{
				if (Random.value >= 0.5)
				{
					if (Grid[x+1,y] == 0)
					{
						Grid[x+1,y] = 5;
						haveblocks++;
					}
					if (Grid[x+2,y] == 0)
					{
						Grid[x+2,y] = 5;
						haveblocks++;
					}
					if (Grid[x-1,y] == 0)
					{
						Grid[x-1,y] = 5;
						haveblocks++;
					}
					if (Grid[x-2,y] == 0)
					{
						Grid[x-2,y] = 5;
						haveblocks++;
					}
				}
				else
				{
					if (Grid[x,y+1] == 0)
					{
						Grid[x,y+1] = 5;
						haveblocks++;
					}
					if (Grid[x,y+2] == 0)
					{
						Grid[x,y+2] = 5;
						haveblocks++;
					}
					if (Grid[x,y-1] == 0)
					{
						Grid[x,y-1] = 5;
						haveblocks++;
					}
					if (Grid[x,y-2] == 0)
					{
						Grid[x,y-2] = 5;
						haveblocks++;
					}
				}
			}
			catch (System.IndexOutOfRangeException e)
			{
				continue;
			}
		}

		/*
		for (int x=0; x<gridsize; x++)
		{
			for (int y=0; y<gridsize; y++)
			{
				if (Grid[x,y] == 0)
				{
					Grid[x,y] = 5;
				}
			}
		}
		*/

		for (int x=0; x<gridsize; x++)
		{
			for (int y=0; y<gridsize; y++)
			{
				if (Grid[x,y] == 5)
				{
					Vector3 pos = new Vector3( (x-(gridsize/2))*gridsquare, 0.0f, (y-(gridsize/2))*gridsquare );

					Material buildingmaterial = Resources.Load("CustomMaterials/Building" + Random.Range(1,4)) as Material;

					if (Random.value < 0.3)
					{
						Vector3 offset = Vector3.zero;
						Vector3 size = Vector3.zero;

						if (Random.value < 0.5)
						{

							size = new Vector3( 2 + (1.5f*Random.value), Random.Range(2,14), Random.Range(2,6));
							offset = (size.x) * Vector3.left;
						}
						else
						{
							size = new Vector3( Random.Range (2,6), Random.Range(2,14), 2+(1.5f*Random.value));
							offset = (size.z) * Vector3.forward;
						}

						GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
						cube.transform.position = pos + offset + (0.01f+size.y/2)*Vector3.up;
						cube.transform.localScale = size;
						cube.GetComponent<Renderer>().material = buildingmaterial;

						cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
						cube.transform.position = pos - offset + (0.01f+size.y/2)*Vector3.up;
						cube.transform.localScale = size;
						cube.GetComponent<Renderer>().material = buildingmaterial;
					}
					else
					{
						Vector3 size = new Vector3( Random.Range (2,6), Random.Range(2,14), Random.Range(2,6));
						GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
						cube.transform.position = pos + (0.01f+size.y/2)*Vector3.up;
						cube.transform.localScale = size;
						cube.GetComponent<Renderer>().material = buildingmaterial;
					}
				}
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
	
		bool valid = true;
		foreach(Target t in targets)
		{
			valid = (valid && t.Pass());
		}
	}
}
