using UnityEngine;
using System.Collections;

public class Attack : MonoBehaviour 
{
	public float attackTime = 1;

	public int currentDamage;
	
	public Transform startPosition;
	public Transform endPosition;
	
	private double lastTime;

	private bool isGoingBack = true;
	private bool isAttacking = false;
	
	void Awake () 
	{
	}

	public bool isAttack()
	{
		return isAttacking;
	}

	public void Reset()
	{
		isGoingBack = true;
		lastTime = Time.time - attackTime;
		transform.position = startPosition.position;
	}

	public void Activate(bool start, bool resetTime)
	{
		if (resetTime)
			lastTime = Time.time;

		if (start)
			isGoingBack = true;
		else
			isGoingBack = false;

		isAttacking = true;
	}

	void OnTriggerEnter(Collider other) 
	{
		if (other.transform.tag == "Player")
		{
			other.GetComponent<PlayerAttack>().TakeDamage(currentDamage);
		}
	}

	void Update () 
	{
		double timeSince = (Time.time - lastTime);

		if (!isGoingBack)
		{
			if (timeSince < attackTime)
			{
				transform.position = Vector3.Lerp(startPosition.position, endPosition.position, (float)timeSince / attackTime);
			}
			else
			{
				Activate(true, true);
			}
		}
		else
		{
			if (timeSince < attackTime)
			{
				transform.position = Vector3.Lerp(endPosition.position, startPosition.position, (float)timeSince / (attackTime * 0.6f));
			}				
			else
			{
				transform.position = startPosition.position;
				isAttacking = false;
			}
		}
	}
}
