using UnityEngine;
using System.Collections;

public class GarbageSpawnerScript : MonoBehaviour {

	public int trainCarNumber = 0;
	private BoxCollider myBoxCollider;

	// Use this for initialization
	void Awake () {

		myBoxCollider = gameObject.GetComponent<BoxCollider>();

		if(!myBoxCollider){
			Debug.LogError("Cannot find BoxCollider!");
		}

	}
	
	// Update is called once per frame
	void Update () {

		/*
		if(Input.GetKeyUp(KeyCode.G)){
			spawnGarbage();
		}
		*/
	
	}

	public void spawnGarbage(){

		Vector3 spawnPosition = Vector3.zero;

		//Debug.Log ("GarbageSpawner::in train car # " + trainCarNumber);
		//Debug.Log ("GarbageSpawner::box collider size = " + myBoxCollider.size);

		spawnPosition.x = transform.position.x + Random.Range(-myBoxCollider.size.x/2, myBoxCollider.size.x/2);
		//spawnPosition.y = transform.position.y + Random.Range(-myBoxCollider.size.y/2, myBoxCollider.size.y/2);
		spawnPosition.y = 0.025f;
		spawnPosition.z = transform.position.z + Random.Range(-myBoxCollider.size.z/2, myBoxCollider.size.z/2);

		//TODO: spawn random kinds of garbage

		GameObject someGarbage;

		if(Random.Range(0,2) == 1){
			someGarbage = Instantiate(Resources.Load("GarbageA")) as GameObject;
		}else{
			someGarbage = Instantiate(Resources.Load("GarbageB")) as GameObject;
		}

		//Debug.Log ("Spawned Garbage of type " + someGarbage.name);

		if(!someGarbage){
			Debug.LogError ("Failed to make garbage!");
		}

		someGarbage.transform.position = spawnPosition;

	}

}
