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

public class VerticalCue : CueBehaviour
{
	void Start ()
	{
		rigidBody = GetComponent<Rigidbody2D> ();
		birdRigidBody = GameObject.FindGameObjectWithTag ("Player").GetComponent<Rigidbody2D>();
		restPosition = transform.position.y;
	}

	// Update is called once per frame
	void FixedUpdate ()
	{
		switch(currentState)
		{
		case CueStates.rest:
			alreadyPushed = false;
			break;

		case CueStates.pushing:
			if (transform.position.y < extrudedPosition)
			{
				float distance = pushSpeed * Time.fixedDeltaTime;
				if (transform.position.y + distance <= extrudedPosition) rigidBody.MovePosition((Vector2)transform.position + new Vector2(0f, distance));
				else
				{
					rigidBody.MovePosition ( new Vector2 (transform.position.x, extrudedPosition));
					currentState = CueStates.extruded;
				}
			}

			if (transform.position.y >= extrudedPosition)
			{
				rigidBody.MovePosition ( new Vector2 (transform.position.x, extrudedPosition));
				currentState = CueStates.extruded;
			}
			break;

		case CueStates.returning:
			alreadyPushed = false;
			if (transform.position.y > restPosition)
			{
				float distance = returnSpeed * Time.fixedDeltaTime;
				if (transform.position.y + distance >= restPosition) rigidBody.MovePosition((Vector2)transform.position + new Vector2 (0f, distance));
				else
				{
					rigidBody.MovePosition ( new Vector2 (transform.position.x, restPosition));
					currentState = CueStates.rest;
				}
			}
			break;
		case CueStates.extruded:
			currentState = CueStates.returning;
			alreadyPushed = true;
			break;
		}
	}

	/*void OnCollisionEnter2D (Collision2D col)
	{
		if (!alreadyPushed && currentState == CueStates.pushing && col.collider.gameObject.CompareTag("Player"))
		{			
			for(int i = 0; i < col.contacts.Length; i++)
			{
				birdRigidBody.AddForceAtPosition ((-col.contacts [i].normal.normalized + GameManager.verticalNormalMod).normalized * (forceMagnitude / col.contacts.Length), col.contacts [i].point);
			}
			alreadyPushed = true;
		}
	}*/
}