using UnityEngine;
using System.Collections;

public class SwingArmControl : MonoBehaviour {

	public Vector2 AimPoint { get; set; }
	public Transform SwingArm;
	public float RotationSpdDegPerSec;
	public Vector2 PlayerInput;
	public float RampupTime;

	private float offset=0;
	private float targetAngle;
	private float lastIdle;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		//MoveArm();
		ExploreMoveArm();
		PositionArm();
	}
	

	private void MoveArm()
	{
		if(PlayerInput.magnitude == 0)
		{
			lastIdle = Time.fixedTime;
			return;
		}

		float face = transform.eulerAngles.z;
		float curr = SwingArm.localEulerAngles.z;
		float wish=0;

		if(PlayerInput.y == 1)
		{
			if(PlayerInput.x == 1)
				wish = Mathf.DeltaAngle(curr, 315);
			else if(PlayerInput.x == -1)
				wish = Mathf.DeltaAngle(curr, 45);
			else
				wish = Mathf.DeltaAngle(curr, 0);
		}
		else if(PlayerInput.y == -1)
		{
			if(PlayerInput.x == 1)
				wish = Mathf.DeltaAngle(curr, 225);
			else if(PlayerInput.x == -1)
				wish = Mathf.DeltaAngle(curr, 135);
			else
				wish = Mathf.DeltaAngle(curr, 180);
		}
		else if(PlayerInput.x == 1)
		{
			wish = -100; //Mathf.DeltaAngle(curr, 270);
		}
		else if(PlayerInput.x == -1)
		{
			wish = 100;//Mathf.DeltaAngle(curr, 90);
		}

		float ramp = Mathf.Min (1,(Time.fixedTime - lastIdle) / RampupTime);
		float factor = 0.3f + 0.7f*ramp*ramp;
		float max = factor*RotationSpdDegPerSec*Time.fixedDeltaTime;
		float move = Mathf.Clamp(wish, -max, max);

		SwingArm.localEulerAngles += new Vector3(0,0,move);
	}
	
	private void ExploreMoveArm()
	{
		if(PlayerInput.magnitude == 0)
		{
			SwingArm.eulerAngles = new Vector3(0,0,transform.eulerAngles.z + offset);
			lastIdle = Time.fixedTime;
			return;
		}
		
		float curr = offset;
		float wish=0;
		
		if(PlayerInput.y == 1)
		{
			if(PlayerInput.x == 1)
				wish = Mathf.DeltaAngle(curr, 315);
			else if(PlayerInput.x == -1)
				wish = Mathf.DeltaAngle(curr, 45);
			else
				wish = Mathf.DeltaAngle(curr, 0);
		}
		else if(PlayerInput.y == -1)
		{
			if(PlayerInput.x == 1)
				wish = Mathf.DeltaAngle(curr, 225);
			else if(PlayerInput.x == -1)
				wish = Mathf.DeltaAngle(curr, 135);
			else
				wish = Mathf.DeltaAngle(curr, 180);
		}
		else if(PlayerInput.x == 1)
		{
			wish = -100; //Mathf.DeltaAngle(curr, 270);
		}
		else if(PlayerInput.x == -1)
		{
			wish = 100;//Mathf.DeltaAngle(curr, 90);
		}
		
		float ramp = (RampupTime == 0) ? 1 : Mathf.Min (1,(Time.fixedTime - lastIdle) / RampupTime);
		float factor = 0.3f + 0.7f*ramp*ramp;
		float max = factor*RotationSpdDegPerSec*Time.fixedDeltaTime;
		float move = Mathf.Clamp(wish, -max, max);
		offset += move;

		//Debug.Log ("Moving Swingarm: ship facing " + transform.eulerAngles.z + " offset moved from " + curr + " to " + offset + " while ramp was " + ramp);

		SwingArm.eulerAngles = new Vector3(0,0,transform.eulerAngles.z + offset);
		//SwingArm.localEulerAngles += new Vector3(0,0,move);
	}

	private void PositionArm()
	{
		SwingArm.position = transform.position + new Vector3(0,0.1f,0);
	}

	#region defunct mouse controls
	/*
	private void ComputeAimTarget()
	{
		if(SwingArm == null)
			return;

		float dx = AimPoint.x - SwingArm.position.x;
		float dy = AimPoint.y - SwingArm.position.y;
		float aimangle = (Mathf.Atan2(dx, dy) * -180/Mathf.PI) % 360;
		
		targetAngle = aimangle;
	}

	private void RotateArm()
	{
		float curr = SwingArm.localEulerAngles.z;
		float delta = Mathf.DeltaAngle(curr, targetAngle); // targetAngle - curr;
		float max = RotationSpdDegPerSec*Time.fixedDeltaTime;
		float move = Mathf.Clamp(delta, -max, max);
		float agl = curr + move;
		SwingArm.localEulerAngles = new Vector3(0,0,agl);
	}
	*/
	#endregion
		
}
