#pragma strict

enum AxisCam { X, Y, XY}
public var AxisMov : AxisCam = AxisCam.X;

public var bobbingSpeed : float = 8;
public var bobbingAmount : Vector2 = Vector2(0.1, 0.05);
public var RecallAmount : float = 0.1;
public var RecallRandomDispl : float = 0.1;

private var OrigbobbingSpeed : float = 8;
private var OrigbobbingAmount : Vector2 = Vector2(0.1, 0.05);
private var OrigRecallAmount : float = 0.1;
private var OrigRecallRandomDispl : float = 0.1;

public var HorizDispl : Vector2 = Vector2(0.1, 0.1); // Desplazamiento horizontal del arma cuando te mueves a los lados
public var VertDispl : Vector2 = Vector2(0.1, 0.1); // Desplazamiento Vertical del arma cuando te avanzas/retrocedes
private var OrigHorizDispl : Vector2 = Vector2.zero;
private var OrigVertDispl : Vector2 = Vector2.zero; 

public var DisableIfAiming : boolean = false;


private var midpointX : float = 0.0;
private var midpointY : float = 0.0;

private var midpointDisplX : float = 0.0;
private var midpointDisplY : float = 0.0;

private var MyPos : Vector3 = Vector3.zero;

private var timerX : float = 0.0;
private var timerY : float = 0.0;
private var Gun : Transform;
private var GunOriginalPosition : Vector3;

private var Recall : boolean = false;
private var InmediateReturn : boolean = false;

private var GunStatusScript : GunStatus;
 
function Start() {
	GunStatusScript = this.gameObject.GetComponent(GunStatus);
	Gun = GunStatusScript.GunNormal;
	ReasignOriginalValues(GunStatusScript.OrigPosGunNormal);
	OrigbobbingSpeed = bobbingSpeed;
	OrigbobbingAmount = bobbingAmount;
	OrigRecallAmount = RecallAmount;
	OrigRecallRandomDispl = RecallRandomDispl;
	OrigHorizDispl = HorizDispl;
	OrigVertDispl = VertDispl;
}

function ReasignOriginalValues(NewGunPos : Vector3){
	GunOriginalPosition = NewGunPos;
	if(AxisMov == AxisCam.X)
		midpointX = NewGunPos.x;
	else
	if(AxisMov == AxisCam.Y)
		midpointY = NewGunPos.y;
	else
	if(AxisMov == AxisCam.XY){
		midpointX = NewGunPos.x;
		midpointY = NewGunPos.y;
	}
}

function ReasignOriginalRotValues(NewGunRot : Quaternion){
	Gun.localRotation = NewGunRot;
}

function AssignCurrentValues(PosX : float, PosY : float){
	//GunOriginalPosition = NewGunPos;
	if(AxisMov == AxisCam.X)
		MyPos.x = PosX;
	else
	if(AxisMov == AxisCam.Y)
		MyPos.y = PosY;
	else
	if(AxisMov == AxisCam.XY){
		MyPos.x = PosX;
		MyPos.y = PosY;
	}
}

function RestoreBobbingValues(){
	bobbingSpeed = OrigbobbingSpeed;
	bobbingAmount = OrigbobbingAmount;
	RecallAmount = OrigRecallAmount;
	RecallRandomDispl = OrigRecallRandomDispl;
}

 
function Update () {
	var wavesliceX : float = 0.0;
	var wavesliceY : float = 0.0;
	var horizontal : float  = Input.GetAxis("Horizontal");
	var vertical : float = Input.GetAxis("Vertical");
	
	if(Recall){
		if(InmediateReturn){
			Gun.localPosition = GunOriginalPosition;
			Recall = false;
		}
		else{
			Gun.localPosition = Vector3.Lerp(Gun.localPosition, GunOriginalPosition, 8.0f * Time.deltaTime);
			if(Vector3.Distance(Gun.localPosition, GunOriginalPosition) < (0.1f/5.0f)){
				Gun.localPosition = GunOriginalPosition;
				Recall = false;
			}
		}
	}
	
	MyPos = Gun.localPosition;
	if(FPCStatus.isShooting){
		AssignCurrentValues(midpointX, midpointY);
		MyPos.x += Random.Range(0.0f, RecallRandomDispl);
		MyPos.y += Random.Range(0.0f, RecallRandomDispl);
		MyPos.z -= RecallAmount;
		if(!Recall){
			Recall = true;
			InmediateReturn = false;
		}
		else{
			InmediateReturn = true;
		}
		Gun.localPosition = MyPos;
		timerX = 0.0;
		timerY = 0.0;
		return;
	}
	
	// Si se apunta se mira si hay que desactivar el balanceo
	// y no se permite el desplazamiento horizontal/vertical del arma.
	if(FPCStatus.IsAiming){
		if(DisableIfAiming)
			return;
		HorizDispl = Vector2.zero;
		VertDispl = Vector2.zero;
	}
	else{
		HorizDispl = OrigHorizDispl;
		VertDispl = OrigVertDispl;
	}
		
		
	if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0){
		midpointDisplX = midpointX;
		midpointDisplY = midpointY;
		timerX = 0.0;
		timerY = 0.0;
	}
	else {
		/*if(!FPCStatus.IsAiming){*/
			if(horizontal > 0)
				midpointDisplX = midpointX+HorizDispl.x;
			else
			if(horizontal < 0)
				midpointDisplX = midpointX-HorizDispl.y;
			else
			if(horizontal == 0)
				midpointDisplX = midpointX;
				
			if(vertical > 0)
				midpointDisplY = midpointY-VertDispl.x;
			else
			if(vertical < 0)
				midpointDisplY = midpointY+VertDispl.y;
			else
			if(vertical == 0)
				midpointDisplY = midpointY;
		/*}*/
			
		wavesliceX = Mathf.Sin(timerX);
		timerX += bobbingSpeed * Time.deltaTime;
		if (timerX > Mathf.PI * 2) 
			timerX -= Mathf.PI * 2;
	
		wavesliceY = Mathf.Sin(timerY);
		timerY = timerX * 2;
		if (timerY > Mathf.PI * 2) 
			timerY -= Mathf.PI * 2;
			
	}
	if (wavesliceX != 0 || wavesliceY != 0) {
		var translateChangeX : float = wavesliceX * bobbingAmount.x;
		var totalAxesX : float = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
		totalAxesX = Mathf.Clamp01(totalAxesX);
		translateChangeX = totalAxesX * translateChangeX;
		/*if(FPCStatus.IsAiming)
			translateChangeX = Mathf.Clamp(translateChangeX, 0, bobbingAmount.x);*/
		//Debug.Log(translateChangeX);
		
		var translateChangeY : float = wavesliceY * bobbingAmount.y;
		var totalAxesY : float = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
		totalAxesY = Mathf.Clamp01(totalAxesY);
		translateChangeY = totalAxesY * translateChangeY;
		/*if(FPCStatus.IsAiming)
			translateChangeY = translateChangeY * bobbingAmount.y;*/
		
		AssignCurrentValues(midpointDisplX + translateChangeX, midpointDisplY + translateChangeY);
	}
	else 
		AssignCurrentValues(midpointDisplX, midpointDisplY);
	
	Gun.localPosition = Vector3.Lerp(Gun.localPosition, MyPos, bobbingSpeed * Time.deltaTime);
}
