using UnityEngine;
using System.Collections;

[RequireComponent(typeof(JoyStickInputController))]
public class Roll : MonoBehaviour
{
	[SerializeField] private int speed = 300;
	public BoardManager boardManager;
	private JoyStickInputController inputController;
	private bool rotating;

	private Vector3 size;
	private Vector3 offsets;

	private void Start()
	{
		Initialize();
	}

	public void Initialize()
	{
		inputController = GetComponent<JoyStickInputController>();

		size = GetComponent<BoxCollider>().size;
		offsets = size / 2f;
		boardManager.PlayerHasMoved(transform.position, size, true, gameObject);
	}

	private void Update()
	{
		if (rotating)
		{
			return;
		}

		Vector3 position = transform.position;
		Quaternion tempEndRotation;
		if (inputController.GetInputAction() == Action.Right)
		{
			Vector3 pos = new Vector3(position.x, position.y - offsets.y, position.z + offsets.z);
			Vector3 potentialSize = new Vector3(size.x, size.z, size.y);

			if (!boardManager.CanMoveInDirection(
				position,
				SimulateMovement(transform, pos, Vector3.right, 90, out tempEndRotation),
				new Vector3(size.x, size.z, size.y)))
			{
				return;
			}

			offsets = new Vector3(offsets.x, offsets.z, offsets.y);
			size = potentialSize;
			StartCoroutine(DoRotation(pos, Vector3.right, 90));
		}
		else if (inputController.GetInputAction() == Action.Left)
		{
			Vector3 pos = new Vector3(position.x, position.y - offsets.y, position.z - offsets.z);
			Vector3 potentialSize = new Vector3(size.x, size.z, size.y);

			if (!boardManager.CanMoveInDirection(
				position,
				SimulateMovement(transform, pos, -Vector3.right, 90, out tempEndRotation), potentialSize))
			{
				return;
			}

			offsets = new Vector3(offsets.x, offsets.z, offsets.y);
			size = potentialSize;
			StartCoroutine(DoRotation(pos, -Vector3.right, 90));
		}
		else if (inputController.GetInputAction() == Action.Down)
		{
			Vector3 pos = new Vector3(position.x + offsets.x, position.y - offsets.y, position.z);
			Vector3 potentialSize = new Vector3(size.y, size.x, size.z);
			if (!boardManager.CanMoveInDirection(
				position,
				SimulateMovement(transform, pos, -Vector3.forward, 90, out tempEndRotation), potentialSize))
			{
				return;
			}

			offsets = new Vector3(offsets.y, offsets.x, offsets.z);
			size = potentialSize;
			StartCoroutine(DoRotation(pos, -Vector3.forward, 90));
		}
		else if (inputController.GetInputAction() == Action.Up)
		{
			Vector3 pos = new Vector3(position.x - offsets.x, position.y - offsets.y, position.z);
			Vector3 potentialSize = new Vector3(size.y, size.x, size.z);

			if (!boardManager.CanMoveInDirection(
				position,
				SimulateMovement(transform, pos, Vector3.forward, 90, out tempEndRotation), potentialSize))
			{
				return;
			}

			offsets = new Vector3(offsets.y, offsets.x, offsets.z);
			size = potentialSize;
			StartCoroutine(DoRotation(pos, Vector3.forward, 90));
		}
	}

	private IEnumerator DoRotation(Vector3 pos, Vector3 axis, float degrees)
	{
		rotating = true;
		Transform t = transform;

		Quaternion endRotation;
		Vector3 endPosition = SimulateMovement(t, pos, axis, degrees, out endRotation);
		AudioController.Play("Movement solo 1");

		for (float i = 0; i < degrees; i += Time.deltaTime * speed)
		{
			yield return null;
			t.RotateAround(pos, axis, Time.deltaTime * speed);
		}

		t.rotation = endRotation;
		t.position = endPosition;

		boardManager.PlayerHasMoved(endPosition, size, true, gameObject);

		rotating = false;
	}

	private Vector3 SimulateMovement(Transform t, Vector3 pos, Vector3 axis, float degrees,
		out Quaternion endRotation)
	{
		Quaternion startRotation = t.rotation;
		Vector3 startPosition = t.position;
		t.RotateAround(pos, axis, degrees);
		endRotation = t.rotation;
		Vector3 endPosition = t.position;
		t.rotation = startRotation;
		t.position = startPosition;
		return endPosition;
	}
}