#pragma strict
@script RequireComponent(AudioSource)

var Angle : float = 0;
var Range : float = 45;
var ZAngle : float = 45;

@Range(0,1)
var Position : float = 0.5;
var Speed : float = 0.5;
var StopTime : float = 3;

var AlertSfx : AudioClip;

private var Rotation0 : Quaternion = Quaternion.identity;
private var Rotation1 : Quaternion = Quaternion.identity;
private var RotationM : Quaternion = Quaternion.identity;
private var TimeLeft : float = 0;
private var Direction : float = 1;

private var CameraRotation : Quaternion;
private var VisTime : float = 0;

function ConvertToQuaternions()
{
	Rotation0 = Quaternion.Euler( ZAngle, Angle - Range, 0 );
	Rotation1 = Quaternion.Euler( ZAngle, Angle + Range, 0 );
	RotationM = Quaternion.Euler( ZAngle, Angle, 0 );
}

function Start()
{
	ConvertToQuaternions();
}

function Update()
{
	if( !VisTime )
	{
		if( TimeLeft > 0 )
		{
			TimeLeft -= Time.deltaTime;
		}
		else
		{
			audio.enabled = true;
			Position += Direction * Speed * Time.deltaTime;
			if( Position > 1 )
			{
				Position = 1;
				if( Direction > 0 )
				{
					audio.enabled = false;
					Direction = -Direction;
					TimeLeft = StopTime;
				}
			}
			else if( Position < 0 )
			{
				Position = 0;
				if( Direction < 0 )
				{
					audio.enabled = false;
					Direction = -Direction;
					TimeLeft = StopTime;
				}
			}
		}
	}

	var tgtrot : Quaternion = Quaternion.Slerp( Rotation0, Rotation1, Position );
	tgtrot = Quaternion.Slerp( tgtrot, CameraRotation, VisTime );
	var angle = 0.0;
	var axis = Vector3.zero;
	( tgtrot * Quaternion.Inverse( RotationM ) ).ToAngleAxis( angle, axis );
	if( angle > Range )
		angle = Range;
	if(transform.rotation == RotationM * Quaternion.AngleAxis( angle, axis ))
		audio.enabled = false;
	transform.rotation = RotationM * Quaternion.AngleAxis( angle, axis );
	
	for( var obj : GameObject in GameObject.FindGameObjectsWithTag( "Player" ) )
	{
		var pos : Vector3 = obj.transform.position;
		var mtx : Matrix4x4 = Matrix4x4.TRS( transform.position, transform.rotation, Vector3(1,1,1) ).inverse;
		var localPos : Vector3 = mtx.MultiplyPoint( pos );
		localPos.x /= localPos.z;
		localPos.y /= localPos.z;
		var q : float = 0.5;
		var see : boolean =
			localPos.x > -q && localPos.x < q &&
			localPos.y > -q && localPos.y < q &&
			localPos.z > 0.01 && localPos.z < 5
		;
		
		if( see )
		{
			audio.enabled = true;
			var rcd = pos - transform.position;
			see = !Physics.Raycast( transform.position, rcd.normalized, rcd.magnitude );
			//Debug.Log(see);

			CameraRotation = Quaternion.LookRotation( obj.transform.position - transform.position );
		}
		var oldVisTime = VisTime;
		VisTime = Mathf.MoveTowards( VisTime, see ? 1 : 0, Time.deltaTime * 0.5 );
		if( VisTime == 1 )
		{
			obj.SendMessage( "AlertTrigger" );

			if( VisTime != oldVisTime )
			{
				Agent.PlayClipAt(AlertSfx, transform.position, 0.5);
			}
		}
	}
}

function OnDrawGizmos()
{
	ConvertToQuaternions();
	var rot : Quaternion = Quaternion.Slerp( Rotation0, Rotation1, Position );
	
	Gizmos.color = Color(0,0.2,0.8,0.5);
	Gizmos.matrix = Matrix4x4.TRS( transform.position, rot, Vector3(1,1,1) );
	Gizmos.DrawFrustum( transform.position, 30, 1, 0.01, 1 );
	Gizmos.DrawFrustum( transform.position, 30, 2, 0.01, 1 );
	Gizmos.DrawFrustum( transform.position, 30, 5, 0.01, 1 );
}
