using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour 
{
	private float hotrodY;
	public float hotrodSpeed;

	private float bomberY, bomberX;
	public float bomberSpeed;
	public Vector3 bomberCenter = new Vector3(0.0f, 2.5f, 0.0f);

	private float spinnerX, spinnerY, spinnerX2;
	public float spinnerSpeed;

	public float speed = 5.0f; // extra base speed
	public float smooth = 2.0f; // for rotation speed
	private float gap = 2.50f;

	public GameObject enemyBullet;
	public float waitTilNextFire = 0.0f;
	public int fireRate = 5;


	public bool canMove, isBomber, isHotrod, isSpinner;
	
	
	void Start()
	{
		hotrodY = transform.position.y - gap;

		bomberY = transform.position.y - (gap * 3);
		bomberX = transform.position.x + (gap * 8);

		spinnerY = transform.position.y - (gap * 2);
		spinnerX = transform.position.x - (gap * 2);
		spinnerX2 = transform.position.x + (gap * 2);

		canMove = true;
	}

	void Update()
	{
		if(canMove)
		{
			MoveEnemy();
		}
	}


	void MoveEnemy()
	{
		// Hotrod **********
		if(isHotrod)
		{
			transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, hotrodY, transform.position.z), hotrodSpeed * Time.deltaTime);
			hotrodY -= gap;

			if (waitTilNextFire <= 0)
			{
				Instantiate(enemyBullet, transform.position, transform.rotation);
				waitTilNextFire = 1;
			}
			waitTilNextFire -= Time.deltaTime * fireRate;
		}
		// End Hotrod ********


		// Bomber ********
		if(isBomber)
		{
			transform.position = Vector3.MoveTowards(transform.position, bomberCenter, speed * Time.deltaTime);
			if(transform.position.y == bomberCenter.y)
			{
				transform.position = Vector3.MoveTowards(transform.position, new Vector3(bomberX, transform.position.y, transform.position.z), bomberSpeed * Time.deltaTime);
				Quaternion target = Quaternion.Euler(0, 0, 90);
				transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
			}
			if(transform.position.x == bomberX)
			{
				Destroy(gameObject);
			}
		}
		// End Bomber ********


		// Spinner
		if(isSpinner)
		{
			transform.position = Vector3.MoveTowards(transform.position, new Vector3(spinnerX, spinnerY, transform.position.z) , spinnerSpeed * Time.deltaTime);
			if(transform.position.x == spinnerX)
			{
				if (waitTilNextFire <= 0)
				{
					Instantiate(enemyBullet, transform.position, transform.rotation);
					waitTilNextFire = 1;
				}
				waitTilNextFire -= Time.deltaTime * fireRate;
			}
		}
		// End Spinner
	}

	/*void EnemyShoot()
	{
		Instantiate(enemyBullet, transform.position, transform.rotation);
	}*/

}
