using UnityEngine;
using System.Collections;

public enum RelativePosition{
	up,
	down,
	right,
	left,
}

public class AutoGenerateCtrl : MonoBehaviour {
	public RelativePosition position;
	public int areaMaximum = 5;	//the maximum number of objects in area when doing generate
	public LayerMask colliderMask = -1;
	public float generateDelayMin = 0.05f;
	public float generateDelayMax = 0.3f;
	//public float delayBias = 1;
	public float safeTime = 5;

	Camera camera;
	Transform _transform;
	float areaWidth=0;
	float areaHeight=0;
	Transform player;
	float generateTime = 0;

	Collider2D[] colliderInArea;

	bool inGenerateMode=false;

	// Use this for initialization
	void Start () {
		player = GameObject.FindWithTag ("Player").transform;
		camera = Camera.main;
		_transform = transform;
		areaHeight = GameManager.instance.GetCameraArea().y;
		areaWidth = GameManager.instance.GetCameraArea().x;
		switch(position){
		case RelativePosition.up:
			_transform.position = new Vector3(camera.transform.position.x, camera.transform.position.y + areaHeight,0);
			break;
		case RelativePosition.down:
			_transform.position = new Vector3(camera.transform.position.x, camera.transform.position.y - areaHeight,0);
			break;
		case RelativePosition.right:
			_transform.position = new Vector3(camera.transform.position.x+areaWidth, camera.transform.position.y,0);
			areaHeight += areaHeight*2;
			break;
		case RelativePosition.left:
			_transform.position = new Vector3(camera.transform.position.x-areaWidth, camera.transform.position.y,0);
			areaHeight += areaHeight*2;
			break;
		}
		generateTime = 0;
		colliderInArea = new Collider2D[areaMaximum];
	}
	
	// Update is called once per frame
	void Update () {
		generateTime -= Time.deltaTime;
		if(safeTime>0){
			safeTime -= Time.deltaTime;
		}
		if(generateTime<0){
			Vector3 genPosition;
			if(safeTime>0 && position==RelativePosition.right){
				if(Random.Range(0,1.0f)>0.7f){
					return;
				}
				genPosition = GetRandomPositionInSafeZone();
			}else{
				genPosition = GetRandomPositionInArea();
			}
			Vector2 p1 = new Vector2(_transform.position.x-areaWidth/2, _transform.position.y-areaHeight/2);
			Vector2 p2 = new Vector2(_transform.position.x+areaWidth/2, _transform.position.y+areaHeight/2);
			int count = Physics2D.OverlapAreaNonAlloc(p1, p2, colliderInArea, colliderMask.value);
			if(count < areaMaximum){
				GameManager.instance.GenerateAstroids(-1, genPosition);
			}
			generateTime = Random.Range(generateDelayMin, generateDelayMax);
		}
	}

	Vector3 GetRandomPositionInSafeZone(){
		float x = Random.Range (_transform.position.x - areaWidth / 2, _transform.position.x + areaWidth / 2);
		float y = 0;
		if(Random.Range(0,1.0f)>0.5f){
			y = Random.Range (_transform.position.y - areaHeight / 3, _transform.position.y - areaHeight / 12);
		}else{
			y = Random.Range (_transform.position.y + areaHeight / 12, _transform.position.y + areaHeight / 3);
		}
		return new Vector3 (x, y, 0);
	}

	Vector3 GetRandomPositionInArea(){
		float x = Random.Range (_transform.position.x - areaWidth / 2, _transform.position.x + areaWidth / 2);
		float y = Random.Range (_transform.position.y - areaHeight / 2, _transform.position.y + areaHeight / 2);
		return new Vector3 (x, y, 0);
	}

	void OnDrawGizmos() {
		Gizmos.color = Color.yellow;
		Gizmos.DrawWireCube (transform.position, new Vector3 (areaWidth, areaHeight, 1));
	}

}
