using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(Rigidbody))]
public class Trick_SlideSensor : MonoBehaviour {

	public BoxCollider sensor;
	public Rigidbody rb;

	public bool trick 			= false;
	public bool touchdown		= false;
	public float cooldown 		= 0f;
	public float maxCooldown	= 3f;
	public float splatCooldown	= 0.25f;
	public float minVelocity	= 5.0f;

	public float timeMultiplier	= 1f;
	public float baseScore		= 10f;
	public float timeScore		= 50f;

	public Collider otherCollider;

	// Use this for initialization
	void Start () {
		rb = GetComponent<Rigidbody> ();
		sensor = GetComponent<BoxCollider> ();
		sensor.isTrigger = true;
	}
	
	// Update is called once per frame
	void FixedUpdate () {

		if (touchdown) {
			cooldown = Mathf.Max(0,cooldown - Time.deltaTime);
			if (trick)
			{
				if (Mathf.Abs(rb.velocity.y) < minVelocity)
				{
					ScoreManager.EndTrick();
				}
			}
			else
			{
				if (cooldown <= 0 && Mathf.Abs(rb.velocity.y) >= minVelocity)
				{
					ScoreManager.NewTrick("Grind",baseScore,timeScore,timeMultiplier);
					trick = true;
				}
			}
		}
	}

	void OnTriggerEnter(Collider other)
	{
		// Already hit that.
		if (other == otherCollider)
			return;
		if (other.gameObject.CompareTag ("Grind")) 
		{
			otherCollider = other;
			touchdown = true;
			cooldown = splatCooldown;
		}
	}

	void OnTriggerStay(Collider other)
	{
		if (other == otherCollider) {
			//ScoreManager.
		}
	}

	void OnTriggerExit(Collider other)
	{
		if (other == otherCollider) {
			if (trick)
			{
				ScoreManager.EndTrick();
				trick = false;
			}
			otherCollider = null;
		}
		touchdown = false;
	}
}
