using UnityEngine;
using System.Collections;

public class DodaBehaviour : MonoBehaviour {

	static float RADIUS_DOTA = 4.4f;

	public float CurveSpeed = 5;
	public float MoveSpeed = 1;
	
	float fTime = 0;
	Vector3 vLastPos = Vector3.zero;

	bool isHitted = false;

	private Animator animator;

	// Use this for initialization
	void Start () 
	{
		vLastPos = transform.position;
		animator = this.GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (!isHitted) {
			vLastPos = transform.position;
			
			fTime += Time.deltaTime * CurveSpeed;

			float x = Mathf.PingPong(Time.time, RADIUS_DOTA)-(RADIUS_DOTA/2.0f);

			Vector3 vSin = new Vector3(x, -Mathf.Sin(fTime), 0);
			Vector3 vLin = new Vector3(x, 0, 0);
			
			transform.position += (vSin + vLin) * Time.deltaTime;
			
			Debug.DrawLine(vLastPos, transform.position, Color.green, 100);

			if (this.transform.position.x < -5.20f) {
				Debug.Log ("EstremoSx");
				animator.SetInteger("Direction", 1);
			} else if (this.transform.position.x > 4.1f) {
				Debug.Log ("EstremoDx");
				animator.SetInteger("Direction", 0);
			}


		} else {

			float step = 4.0f * Time.deltaTime;
			Vector3 target = new Vector3(8.2f, 2.8f, 0.0f);
			this.transform.position = Vector3.MoveTowards(this.transform.position, target, step);
		}
	}

	void OnCollisionEnter2D(Collision2D coll) {

		if (coll.gameObject.tag == "Player") {

			if (isHitted) {
				CharacterBehaviour c = (CharacterBehaviour)coll.gameObject.GetComponent(typeof(CharacterBehaviour));
				//c.addLife();
				RefereeBehaviour.IsLevelOver = true;
				return;
			}

			isHitted = true;
		}
	}
}
