using UnityEngine;
using System.Collections;

public class TurretController : MonoBehaviour
{
	[SerializeField]public GameObject aimObject = null;  // Aim. Fill from editor or externally

	[SerializeField]Transform turretTransform = null;  // Fill from editor - side rotated platform
	[SerializeField]Transform cannonTransform = null;  // Fill from editor - up/down rotated cannon
	[SerializeField]Transform startRotation = null;  // Fill from editor - up/down rotated cannon
	
	[SerializeField]float maxRotationSpeed = 240f; // max tower rotation speed per second
	[SerializeField]float maxPitchRotationSpeed = 440f; // max gun rotation speed per second

	Quaternion baseStateFactor = Quaternion.identity; // BaseRotation
//	Quaternion defaultTurretRotation = Quaternion.identity; // BaseRotation
//	Quaternion defaultCannonRotation = Quaternion.identity; // BaseRotation

	[SerializeField]
	float minPitchAngleBorder = 0;
	[SerializeField]
	Vector2 minPitchAngleBetween = new Vector2 (270, 360);
	[SerializeField]
	float maxPitchAngleBorder = 180f;
	[SerializeField]
	Vector2 maxPitchAngleBetween = new Vector2 (180, 270);

	Transform selfTransform = null;
	float currentYawAngle = 0.1f;
	float needYawAngle = 0;
	float currentPitchAngle = 0.1f;
	float needPitchAngle = 0; // for guns

	void Start ()
	{
		selfTransform = transform;
		if (startRotation != null)
		{
			baseStateFactor = startRotation.transform.localRotation;
		}
//		if (turretTransform != null)
//		{
//			defaultTurretRotation = turretTransform.transform.localRotation;
//		}
//		if (cannonTransform != null)
//		{
//			defaultCannonRotation = cannonTransform.transform.localRotation;
//		}
	}

	void Update ()
	{
		if (aimObject != null) { // need turret corps angle to aim
			Vector3 directionInCorpsSpace = Vector3.forward;
			directionInCorpsSpace = 
				Quaternion.Inverse (selfTransform.parent.rotation * baseStateFactor) * 
				(aimObject.transform.position - selfTransform.position);
			Vector2 flatDirection = new Vector2 (directionInCorpsSpace.x, directionInCorpsSpace.z);
			needYawAngle = Utils.GetAngle (flatDirection);
			float distance = flatDirection.magnitude;
			needPitchAngle = Utils.GetAngle (new Vector2 (directionInCorpsSpace.y, distance));
			if (AngleBetween (needPitchAngle, minPitchAngleBetween.x, minPitchAngleBetween.y)) {
				needPitchAngle = minPitchAngleBorder;
			}
			if (AngleBetween (needPitchAngle, maxPitchAngleBetween.x, maxPitchAngleBetween.y)) {
				needPitchAngle = maxPitchAngleBorder;
			}
		} else {
			needYawAngle = 0;
			needPitchAngle = 0;
		}

		if (currentYawAngle != needYawAngle) {
			currentYawAngle = Utils.RotateToAngle (currentYawAngle, needYawAngle, maxRotationSpeed * Time.deltaTime);
			if (turretTransform != null) {
				turretTransform.localEulerAngles = new Vector3 (0, currentYawAngle, 0);
			}
		}
		if (currentPitchAngle != needPitchAngle) {
			currentPitchAngle = Utils.RotateToAngle (currentPitchAngle, needPitchAngle, maxPitchRotationSpeed * Time.deltaTime);
			if (cannonTransform != null) {
				cannonTransform.localEulerAngles = new Vector3 (- currentPitchAngle, 0, 0);
			}
		}
	}

	static bool AngleBetween (float checkAnagle, float a, float b)
	{
		if (a > b) {
			if (a - b > 180f) {
				return !(checkAnagle < a && checkAnagle > b);
			} else {
				return (checkAnagle < a && checkAnagle > b);
			}
		} else {
			if (b - a > 180f) {
				return !(checkAnagle < b && checkAnagle > a);
			} else {
				return (checkAnagle < b && checkAnagle > a);
			}
		}
	}
}
