using UnityEngine;
using System.Collections;

public class CharacterInteractionController : MonoBehaviour
{
	[SerializeField]
	private BoxCollider2D TopCollider;
	[SerializeField]
	private Renderer TopIndicator;
	[SerializeField]
	private BoxCollider2D BottomCollider;
	[SerializeField]
	private Renderer BottomIndicator;
	[SerializeField]
	private BoxCollider2D LeftCollider;
	[SerializeField]
	private Renderer LeftIndicator;
	[SerializeField]
	private BoxCollider2D RightCollider;
	[SerializeField]
	private Renderer RightIndicator;
	public bool canInteract = true;

	public void Collided(string direction, Collider2D collider)
	{
		if(canInteract)
		{
			MonoBehaviour[] scriptList = collider.GetComponents<MonoBehaviour>();
			foreach(MonoBehaviour interactionScript in scriptList)
			{
				IInteractable script = interactionScript as IInteractable;
				if(script != null)
				{
					if(Input.GetKeyDown(KeyCode.LeftArrow) && direction == "Left" && collider.CompareTag("Interactable"))
					{
						script.Interact();
						LeftIndicator.material.color = new Color(LeftIndicator.material.color.r, LeftIndicator.material.color.g,LeftIndicator.material.color.b, 1.0f);
					}
					if(Input.GetKeyDown(KeyCode.RightArrow) && direction == "Right" && collider.CompareTag("Interactable"))
					{
						script.Interact();
						RightIndicator.material.color = new Color(RightIndicator.material.color.r, RightIndicator.material.color.g,RightIndicator.material.color.b, 1.0f);
					}
					if(Input.GetKeyDown(KeyCode.UpArrow) && direction == "Top" && collider.CompareTag("Interactable"))
					{
						script.Interact();
						TopIndicator.material.color = new Color(TopIndicator.material.color.r, TopIndicator.material.color.g,TopIndicator.material.color.b, 1.0f);
					}
					if(Input.GetKeyDown(KeyCode.DownArrow) && direction == "Bottom" && collider.CompareTag("Interactable"))
					{
						script.Interact();
						BottomIndicator.material.color = new Color(BottomIndicator.material.color.r, BottomIndicator.material.color.g,BottomIndicator.material.color.b, 1.0f);
					}
				}
			}
		}
	}

	void LateUpdate()
	{
		if (Input.GetKeyUp (KeyCode.LeftArrow)) {
			LeftIndicator.material.color = new Color(LeftIndicator.material.color.r, LeftIndicator.material.color.g,LeftIndicator.material.color.b, 0.0f);
		}
		if (Input.GetKeyUp (KeyCode.RightArrow)) {
			RightIndicator.material.color = new Color(RightIndicator.material.color.r, RightIndicator.material.color.g,RightIndicator.material.color.b, 0.0f);
		}
		if (Input.GetKeyUp (KeyCode.UpArrow)) {
			TopIndicator.material.color = new Color(TopIndicator.material.color.r, TopIndicator.material.color.g,TopIndicator.material.color.b, 0.0f);
		}
		if (Input.GetKeyUp (KeyCode.DownArrow)) {
			BottomIndicator.material.color = new Color(BottomIndicator.material.color.r, BottomIndicator.material.color.g,BottomIndicator.material.color.b, 0.0f);
		}
	}
}
