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

public class Enemy_OwlScript : MonoBehaviour {

	public float owlTimer = 60; //Based on seconds.
	float owlTimerMax;
	bool tickingDown = false;

	public float frameRateAnimation = 24;

	float timerSecondAnimation = 1;

	public List<Sprite> spritesIdle;
	int idleCounter = 0;

	public GameObject owlKillBox;
	SpriteRenderer ownRenderer;

	// Use this for initialization
	void Start () {

		owlTimerMax = owlTimer;
		ownRenderer = GetComponent<SpriteRenderer> ();
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey (KeyCode.T)) {
			OwlTimerTickDown ();

		}

		if (tickingDown == false) {

			owlTimer += (Time.deltaTime);
			if (owlTimer >= owlTimerMax)
				owlTimer = owlTimerMax;

		}
		tickingDown = false;
		timerSecondAnimation -= Time.deltaTime * frameRateAnimation;
		if (timerSecondAnimation <= 0) {
			PlayIdleAnimation ();
			timerSecondAnimation = 1;
		}
	}

	public void OwlTimerTickDown()
	{
		owlTimer -= (Time.deltaTime);
		tickingDown = true;

		if (owlTimer <= 0) {

			GameObject player = GameObject.FindGameObjectWithTag("Player");

			Instantiate (owlKillBox, player.transform.position, player.transform.rotation);

		}
	}

	public void OwlTimerReset()
	{
		owlTimer = owlTimerMax;
	}

	void PlayIdleAnimation()
	{
		ownRenderer.sprite = spritesIdle [idleCounter];
	
		idleCounter++;

		if (idleCounter >= spritesIdle.Count)
			idleCounter = 0;


	}
}
