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

public class LevelGenerator : MonoBehaviour 
{
	public GameObject ground;
	public GameObject water;
	public GameObject trampoline;
	public GameObject initialGround;
	public GameObject finalObj;

	public GameObject[] groundObj;
	public GameObject[] enemiesObj;

	public float enviromentRmd = 10;
	public float enemiesRmd = 50;

	public int width = 25;
	public int height = 6;

	public int randomAmount = 60;

	public List<GameObject> tiles;

	private GameObject startPoint;
	private GameObject endPoint;

	private int waterSpot;

	void Start() 
	{
		tiles = new List<GameObject> ();
		randomAmount = GameManager.envirotraps;
		enemiesRmd = GameManager.enemyPorcentage;

		startPoint = (GameObject) Instantiate(initialGround);
		startPoint.transform.SetParent (transform);
		startPoint.transform.localScale = Vector3.one;
		startPoint.name = "Start";

		startPoint.transform.localPosition = new Vector3 (Mathf.RoundToInt(Random.Range (-10, 0)),
			-3,
			0);
		startPoint.transform.localEulerAngles = Vector3.up * -90;

		GameObject.FindGameObjectWithTag (Tags.Player).transform.position = startPoint.transform.position + (Vector3.up * 4);

		tiles.Add (startPoint);

		endPoint = (GameObject) Instantiate(initialGround);
		endPoint.transform.SetParent (transform);
		endPoint.transform.localScale = Vector3.one;
		endPoint.name = "End";

		endPoint.transform.localPosition = new Vector3 (startPoint.transform.localPosition.x + (width * 8),
			/*Mathf.RoundToInt(Random.Range (-height, height + 1)*/startPoint.transform.localPosition.y,
			0);
		endPoint.transform.localEulerAngles = Vector3.up * 90;

		GameObject final = (GameObject)Instantiate (finalObj, endPoint.transform);
		final.transform.localPosition = finalObj.transform.localPosition;
		final.transform.localRotation = finalObj.transform.localRotation;

		BuildRoad ();
	}

	void BuildRoad ()
	{
		Vector3 start = startPoint.transform.localPosition;
		Vector3 end = endPoint.transform.localPosition;
		int roadAmount = Mathf.RoundToInt(end.x - start.x);

		/*for(int x = 0; x < maxWidth; x++)
		{
			for(int y = 0; y < height; y++)
			{
				tiles [x, y] = new Vector3 ();
			}
		}*/

		for(int i = 1; i < width; i++)
		{
			if (ShouldntCreate()) 
			{
				GameObject newWater = (GameObject) Instantiate(water);
				newWater.transform.SetParent (transform);
				newWater.transform.localScale = Vector3.one;

				newWater.transform.localPosition = new Vector3 (start.x + (8 * i),
					start.y,
					0);
				newWater.transform.localEulerAngles = Vector3.up * 90;
				if(waterSpot >= 2)
				{
					/*newCube.name = "Impulse";
					newCube.tag = Tags.Impulse;
					render.material.color = Color.grey;
					waterSpot = 0;
					newCube.GetComponent<BoxCollider> ().center = new Vector3 (0, 0, 0);*/
				}
				tiles.Add (newWater);

				if(Random.Range(0,100) < 50)
				{
					newWater.GetComponent<MeshRenderer>().enabled = false;
				}
			}
			else
			{
				GameObject newGround = (GameObject) Instantiate(ground);
				newGround.transform.SetParent (transform);
				newGround.transform.localScale = Vector3.one;
				newGround.transform.localEulerAngles = Vector3.up * 90;

				newGround.transform.localPosition = new Vector3 (start.x + (8 * i),
					start.y,
					0);
				newGround.name = "Ground";
				waterSpot = 0; 
				tiles.Add (newGround);
				/*float rmd = (int)Random.Range (0, 100);
				if(rmd > randomAmount)
				{
					newWater.name = "HighGround";
					tiles.Add (newCube);

					//CreateBlocksUp (newCube.transform.localPosition, i);
				}
				else 
				{
					tiles.Add (newCube);
				}*/
			}
		}
		tiles.Add (endPoint);
		FillRoad ();
		//transform.localScale = new Vector3 (0.4f, 0.4f, transform.localScale.z);
	}

	void FillRoad ()
	{
		int count = 0;
		foreach(GameObject obj in tiles)
		{
			if(obj.layer == LayerMask.NameToLayer(Layers.Ground) && count != 0)
			{
				if(Random.Range(0,100) < enviromentRmd)
				{
					int rmdObj = Random.Range (0, groundObj.Length);
					GameObject spawn = (GameObject)Instantiate (groundObj[rmdObj]);
					spawn.transform.parent = obj.transform;
					spawn.transform.localPosition = groundObj [rmdObj].transform.localPosition;
					spawn.transform.localRotation = groundObj [rmdObj].transform.localRotation;
					spawn.transform.localScale = groundObj [rmdObj].transform.localScale;
				}
				if(Random.Range(0,100) < enemiesRmd)
				{
					int rmdObj = Random.Range (0, enemiesObj.Length);
					GameObject spawn = (GameObject)Instantiate (enemiesObj[rmdObj]);
					spawn.transform.parent = obj.transform;
					spawn.transform.localPosition = enemiesObj [rmdObj].transform.localPosition;
					spawn.transform.localRotation = enemiesObj [rmdObj].transform.localRotation;
					spawn.transform.localScale = enemiesObj [rmdObj].transform.localScale;
				}
			}
			else if(obj.layer == LayerMask.NameToLayer(Layers.Water))
			{
				if(tiles[count+1].layer == LayerMask.NameToLayer(Layers.Water))
				{
					GameObject lirio = (GameObject)Instantiate (trampoline);
					lirio.transform.parent = obj.transform;
					lirio.transform.localPosition = new Vector3 (0, 3.6f, 4);
					lirio.transform.localScale = new Vector3(1, 0.1f, 1);
					obj.GetComponent<MeshRenderer>().enabled = true;
					tiles [count + 1].GetComponent<MeshRenderer>().enabled = true;
				}
			}
			count++;
		}
	}

	bool ShouldntCreate ()
	{
		int rmd = (int)Random.Range (0, 100);
		if(rmd > randomAmount)
		{
			return false;
		}

		return true;
	}

	void CreateBlocksUp (Vector3 cubePos, int index)
	{
		int rmd = (int)Random.Range(1, height + 1);
		for(int i = 0; i < rmd; i++)
		{
			if(i == 2 && !tiles[index - 1].name.Contains("HighGround2") && !tiles[index - 2].name.Contains("HighGround2"))
			{
				return;
			}
			GameObject newCube = GameObject.CreatePrimitive (PrimitiveType.Cube);
			newCube.transform.SetParent (transform);
			newCube.transform.localScale = Vector3.one;
			newCube.name = "UpperGround";
			newCube.transform.localPosition = new Vector3 (cubePos.x,
				cubePos.y + (i + 1),
				cubePos.z);
			Renderer render = newCube.GetComponent<Renderer> ();
			render.material.color = Color.yellow;
		}
		if(rmd == 2)
		{
			tiles[index].name = "HighGround2";
		}

		if((tiles.Count > 2 && !tiles[index - 1].name.Contains("Impulse") && !tiles[index - 1].name.Contains("HighGround")) && 
			(tiles.Count > 3 && !tiles[index - 2].name.Contains("Impulse") && !tiles[index - 2].name.Contains("HighGround")) && 
			(tiles.Count > 4 && !tiles[index - 3].name.Contains("Impulse") && !tiles[index - 3].name.Contains("HighGround")))
		{

			if(rmd >= 2)
			{
				GameObject otherCube = GameObject.CreatePrimitive (PrimitiveType.Cube);
				otherCube.transform.SetParent (transform);
				otherCube.transform.localScale = new Vector3(1, 0.2f, 1);

				otherCube.transform.localPosition = new Vector3 (cubePos.x - 1.8f,
					cubePos.y + 1.4f,
					cubePos.z);
				Renderer render = otherCube.GetComponent<Renderer> ();
				render.material.color = Color.green;

				if(rmd >= 3)
				{
					GameObject secondCube = GameObject.CreatePrimitive (PrimitiveType.Cube);
					secondCube.transform.SetParent (transform);
					secondCube.transform.localScale = new Vector3(1, 0.2f, 1);

					secondCube.transform.localPosition = new Vector3 (cubePos.x - 2.8f,
						cubePos.y + 2.4f,
						cubePos.z);
					Renderer rendersecondCube = secondCube.GetComponent<Renderer> ();
					render.material.color = Color.green;
				}
			}
		}
	}
}
