using UnityEngine;
using System.Collections;

public class HandControl : MonoBehaviour {

	#region Private Variables
	[SerializeField]
	private Sprite[] _stages;

	private SpriteRenderer _renderer;

	private bool _forwardAnim = false;

	private int _currentStage = 0;
	private float _duration = 3f;
	private float _startTime = -1f;
	private Vector3 _defaultPos;
	private Vector3 _lowestPos;
	#endregion


	#region Getters and Setters
	public int stage {
		get {
			return _currentStage;
		}
	}
	#endregion


	#region Events
	void Awake () {

		_renderer = GetComponent <SpriteRenderer> ();
		
	}

	void Start () {

		_defaultPos = _lowestPos = transform.position;

		_lowestPos.y = _defaultPos.y - 5f;
		
	}

	void OnTriggerEnter2D () {

		_startTime = Time.time;
		_forwardAnim = true;

	}

	void Update () {

		if (_forwardAnim) {

			float t = (Time.time - _startTime) / _duration;
			transform.position = Vector3.Lerp (transform.position, _lowestPos, t);

			if (t >= 0.9f) {
				_currentStage++;
				_renderer.sprite = _stages [_currentStage];
				_forwardAnim = !_forwardAnim;
				_startTime = Time.time;
			}

		} else {

			float t = (Time.time - _startTime) / _duration;
			transform.position = Vector3.Lerp (transform.position, _defaultPos, t);

		}

	}
	#endregion

}
