#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 RecoilAmount : float = 0.1;
public var RecoilRandomDispl : float = 0.1;

public var bobbingRunSpeed : float = 12;

private var OrigbobbingSpeed : float = 8;
private var OrigbobbingAmount : Vector2 = Vector2(0.1, 0.05);
private var OrigRecoilAmount : float = 0.1;
private var OrigRecoilRandomDispl : 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 Recoil : 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;
	OrigRecoilAmount = RecoilAmount;
	OrigRecoilRandomDispl = RecoilRandomDispl;
	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){
	if(Gun != null)
		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;
	RecoilAmount = OrigRecoilAmount;
	RecoilRandomDispl = OrigRecoilRandomDispl;
}


function Update () {
	var wavesliceX : float = 0.0;
	var wavesliceY : float = 0.0;
	var horizontal : float  = Input.GetAxis("Horizontal");
	var vertical : float = Input.GetAxis("Vertical");
	
	MyPos = Gun.localPosition;
	if(FPCStatus.isShooting){
		AssignCurrentValues(midpointX, midpointY);
		MyPos.x += Random.Range(0.0f, RecoilRandomDispl);
		MyPos.y += Random.Range(0.0f, RecoilRandomDispl);
		MyPos.z -= RecoilAmount;
		if(MyPos.z < 0)
			MyPos.z = 0;
		
		Gun.localPosition = MyPos;
		if(!Recoil)
			Recoil = true;
		
		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;
	}
	
	
	// Calculo los MidPoint y waveslice para posicionar el arma.
	if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0){
		midpointDisplX = midpointX;
		midpointDisplY = midpointY;
		timerX = 0.0;
		timerY = 0.0;
		
		if(Recoil){
			Gun.localPosition = Vector3.Lerp(Gun.localPosition, GunOriginalPosition, 3*Time.deltaTime);
			if(Vector3.Distance(Gun.localPosition, GunOriginalPosition) < (0.01)){
				Gun.localPosition = GunOriginalPosition;
				Recoil = false;
			}
		}
	}
	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;
		
	}
	
	// Calculo los TRanslateChange para posicionar el arma, basandome en los valores
	// de waveslice y midpoint.
	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);
}
