using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class World : MonoBehaviour 
{
	public List<GameObject> LRHitPrefabs = new List<GameObject>();
	public List<GameObject> bottomHitPrefabs = new List<GameObject>();
	public List<Chunk> groundsInUse = new List<Chunk>();
	public int maxNumberOfTiles = 2; 
	public int MaxDistanceToTravel = 50;
	public int OffCenter =0;
	public int ChunkSize = 40;
	public Player playerRef;
	public int numberOfTiles = 1;

	public void CreateNewChunk(SideHit side,Chunk c)
	{
		//check if chunk position already exists
		for(int i=0;i<groundsInUse.Count;i++)
		{
			if(side == SideHit.Right)
			{
				if((c.XLocation+1) == groundsInUse[i].XLocation && c.YLocation == groundsInUse[i].YLocation)
				{
					return;
				}
			}
			if(side == SideHit.Left)
			{
				if((c.XLocation-1) == groundsInUse[i].XLocation && c.YLocation == groundsInUse[i].YLocation)
				{
					return;
				}
			}
			if(side == SideHit.Bottom)
			{
				if((c.XLocation) == groundsInUse[i].XLocation && (c.YLocation-1) == groundsInUse[i].YLocation)
				{
					return;
				}
			}
		}
		//take the oldest chunk and remake it in t
//		Destroy(groundsInUse[0]);
//		groundsInUse.Remove(groundsInUse[0]);
		Vector3 tempPos = new Vector3 ();
		switch(side)
		{
		case SideHit.Bottom:
			GameObject g = Instantiate (bottomHitPrefabs [Random.Range (0, bottomHitPrefabs.Count)]) as GameObject;
			// im going down generate a new block below and set a new down variable
			g.GetComponent<Chunk>().XLocation = c.XLocation;
			g.GetComponent<Chunk>().YLocation = c.YLocation-1;
			playerRef.CurrentDepth = c.YLocation-1;
			tempPos.y = ChunkSize * g.GetComponent<Chunk>().YLocation;
			tempPos.x = ChunkSize * g.GetComponent<Chunk>().XLocation;
			g.transform.position = tempPos;
			g.transform.parent = gameObject.transform;
			groundsInUse.Add (g.GetComponent<Chunk>());
			break;
		case SideHit.Left:
			GameObject g1 = Instantiate (LRHitPrefabs [Random.Range (0, LRHitPrefabs.Count)]) as GameObject;
			// im going left generate a new block to the left im off center by an additonal -1
			g1.GetComponent<Chunk>().XLocation = c.XLocation-1;
			g1.GetComponent<Chunk>().YLocation = c.YLocation;
			g1.GetComponent<Chunk>().LeftAligned();
			tempPos.y = ChunkSize * g1.GetComponent<Chunk>().YLocation;
			tempPos.x = ChunkSize * g1.GetComponent<Chunk>().XLocation;
			g1.transform.position = tempPos;
			g1.transform.parent = gameObject.transform;
			groundsInUse.Add (g1.GetComponent<Chunk>());
			break;
		case SideHit.Right:
			// im going right generate a new block to the right im off center by an additonal 1
			GameObject g2 = Instantiate (LRHitPrefabs [Random.Range (0, LRHitPrefabs.Count)]) as GameObject;
			g2.GetComponent<Chunk>().XLocation = c.XLocation+1;
			g2.GetComponent<Chunk>().YLocation = c.YLocation;
			g2.GetComponent<Chunk>().RightAligned();
			tempPos.y = ChunkSize * g2.GetComponent<Chunk>().YLocation;
			tempPos.x = ChunkSize * g2.GetComponent<Chunk>().XLocation;
			g2.transform.position = tempPos;
			g2.transform.parent = gameObject.transform;
			groundsInUse.Add (g2.GetComponent<Chunk>());
			break;
		}


	}
	// Update is called once per frame
	void Update () 
	{
		for(int i=0;i<groundsInUse.Count;i++)
		{
			if(groundsInUse[i].YLocation-2 > playerRef.CurrentDepth)
			{
				Destroy(groundsInUse[i].gameObject);
				groundsInUse.RemoveAt(i);
			}
		}
	}

	public void EndWorld()
	{
		Destroy(gameObject);
	}

}
