using UnityEngine;
using System.Collections;

public class MineController : MonoBehaviour, IInteractable, IResetable
{
	private Animator animator;
	[SerializeField]
	private AudioClip beep1;
	[SerializeField]
	private AudioClip beep2;
	[SerializeField]
	private AudioClip aBoom;
	private bool flashing = false;
	private bool safe = false;
	private bool kill = false;
	[SerializeField]
	private float timerStart = 4f;
	private float timerCurrent;

	void Start()
	{
		animator = GetComponent<Animator>();
		timerCurrent = timerStart;
	}

	void Update()
	{
		if(flashing)
		{
			timerCurrent -= Time.deltaTime;
			if(timerCurrent <= 0)
			{
				Boom();
				//Need an explosion sound
			}
		}
	}

	void OnTriggerEnter2D(Collider2D collider)
	{
		if(collider.CompareTag("Player"))
		{
			flashing = true;
			StartRandomColourAnimation();
		}
	}

	void OnTriggerStay2D(Collider2D collider)
	{
		if(kill && collider.CompareTag("Player"))
		{
			Debug.Log("Kill Player");
			collider.GetComponent<DeathSpawnController>().KillPlayer();
		}
	}

	void OnTriggerExit2D(Collider2D collider)
	{
		if(collider.CompareTag("Player"))
		{
			flashing = false;
			StopAnimation();
			timerCurrent = timerStart;
		}
	}

	void OnCollisionEnter2D(Collision2D collision)
	{
		if(collision.collider.CompareTag("Player"))
		{
			Boom();
		}
	}

	public void SetSafe()
	{
		safe = true;
	}

	public void SetUnsafe()
	{
		safe = false;
	}

	public void StartRandomColourAnimation()
	{
		if(flashing && !kill)
		{
			if(Random.Range(0,2) == 0)
			{
				animator.SetTrigger("flashRed");
				Camera.main.GetComponent<AudioController>().PlaySoundEffect(beep1);
				SetUnsafe();
				//Play red sound effect
			}
			else
			{
				animator.SetTrigger("flashGreen");
				Camera.main.GetComponent<AudioController>().PlaySoundEffect(beep2);
				SetSafe();
				//Play Green Sound efect
			}
		}
	}

	public void StopAnimation()
	{
		gameObject.GetComponent<Animator>().ResetTrigger("flashRed");
		gameObject.GetComponent<Animator>().ResetTrigger("flashGreen");
		gameObject.GetComponent<Animator>().ResetTrigger("disarm");
		gameObject.GetComponent<Animator>().ResetTrigger("boom");
		gameObject.GetComponent<Animator>().ResetTrigger("tick");
		animator.SetTrigger("reset");
	}

	private void Disarm()
	{
		animator.SetTrigger("disarm");
		flashing = false;
	}

	private void Boom()
	{
		animator.SetTrigger("boom");
		if(!kill)
		{
			Camera.main.GetComponent<AudioController> ().PlaySoundEffect (aBoom);
		}
		kill = true;
	}

	public void Interact()
	{
		if(safe)
		{
			animator.SetTrigger("disarm");
			kill = false;
			foreach(Collider2D collider in gameObject.GetComponents<Collider2D>())
			{
				collider.enabled = false;
			}
		}
		else
		{
			animator.SetTrigger("boom");
		}
	}

	public void RemoveThis()
	{
		gameObject.GetComponent<SpriteRenderer>().enabled = false;
		foreach(Collider2D collider in gameObject.GetComponents<Collider2D>())
		{
			collider.enabled = false;
		}
		StopAnimation();
		flashing = false;
		timerCurrent = timerStart;
	}

	public void Reset()
	{
		gameObject.GetComponent<SpriteRenderer>().enabled = true;
		foreach(Collider2D collider in gameObject.GetComponents<Collider2D>())
		{
			collider.enabled = true;
		}
	}
}