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

public class Bat : MonoBehaviour {

	// Use this for initialization
	public Light spotLight;
	public float radius;
	public GameObject target;
	public GameObject selectedEnemy;
	public GameObject waveCircle;
	private Coroutine coroutine = null;
	private GameObject enemies;
	public SpriteRenderer waveSprite;
	public float minWaveCircleScale = 1.0f;
	public float maxWaveCircleScale;
	public float maxLightAngle = 179.0f;
	public float minLightAngle = 52.0f;
	public float maxOpacity = 0.6f;
	public float minOpacity = 0.3f;
	public float seenTime;
	public float minimumSeenTime;
	public bool check = false;
	public Score score;
	void Start () {
		spotLight.spotAngle = minLightAngle;
		enemies = GameObject.Find("Enemies");
		target.SetActive(false);
		chooseEnemy();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (check && selectedEnemy != null && Input.touchCount >0 &&Input.GetTouch(0).phase==TouchPhase.Ended )
			selectEnemy();
	}
	void attackEnemy()
	{
		check = false;
		Vector3 enemyPosition = selectedEnemy.transform.position;
		Vector3 enemyNewPosition;
		if (transform.position.y >= 0)
		{
			enemyNewPosition = new Vector3(Random.Range(-2f,2f),-9f,0f);
		}
		else
		{
			enemyNewPosition = new Vector3(Random.Range(-2f,2f),9f,0f);
		}
		selectedEnemy.GetComponent<Enemy>().killAnimation();
		StartCoroutine(attackFly(enemyPosition,enemyNewPosition));

	}
	IEnumerator attackFly(Vector3 endPosition,Vector3 newSpawn)
	{
		float t = 0f;
		GameObject tempEnemy = selectedEnemy;
		selectedEnemy = null;
		Vector3 startPosition = transform.position;
		while(t < 1)
		{
			t += Time.deltaTime / seenTime;
			transform.position = Vector3.Lerp(startPosition, endPosition, t);
			yield return null;
		}
		transform.position = endPosition;
		tempEnemy.GetComponent<Enemy>().spawnNew(newSpawn);
		seenTime = Mathf.Clamp(minimumSeenTime, seenTime - 0.025f, seenTime);
		score.enemyKilled(tempEnemy.GetComponent<Enemy>().score);

		chooseEnemy();
	}

	
	void selectEnemy()
	{
		StopCoroutine(coroutine);
		foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
		{	
			if(enemy != selectedEnemy)
				enemy.GetComponent<EnemyFly>().startObject();	
		}
		target.SetActive(false);
		attackEnemy();
	}

	void chooseEnemy()
	{
		
		foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
 		{

			enemy.GetComponent<EnemyFly>().stopObject();
		}
		coroutine=StartCoroutine(checkEnemy());
	}

	IEnumerator checkEnemy()
	{
		Collider2D[] colliders = Physics2D.OverlapCircleAll(gameObject.transform.position, radius);
		List<Collider2D> collides = new List<Collider2D>();

		foreach (Collider2D collide in colliders)
		{
			if (!collide.name.StartsWith("Border"))
				collides.Add(collide);
		}
		if (collides.Count > 0)
		{
			target.transform.position = collides[0].transform.position;
		}
		else
		{
			Debug.Log("ben buraya bos oldugumdan girdim");
			gameOver();
			yield break;
		}
		waveSprite.color = new Color(waveSprite.color.r, waveSprite.color.g, waveSprite.color.b, maxOpacity);
		waveCircle.transform.localScale = new Vector3(minWaveCircleScale, minWaveCircleScale, 1);
		spotLight.spotAngle = minLightAngle;
		float t = 0f;
		float tempScale;
		while(t < 1)
		{
			t += Time.deltaTime / seenTime;
			spotLight.spotAngle = Mathf.Lerp(minLightAngle, maxLightAngle , t);
			tempScale = Mathf.Lerp(minWaveCircleScale, maxWaveCircleScale, t);
			waveCircle.transform.localScale = new Vector3(tempScale, tempScale, 1);
			waveSprite.color = new Color(waveSprite.color.r, waveSprite.color.g, waveSprite.color.b, Mathf.Lerp(maxOpacity,minOpacity,t));
			yield return null;
		}
		spotLight.spotAngle = maxLightAngle;
		waveCircle.transform.localScale = new Vector3(maxWaveCircleScale, maxWaveCircleScale, 1);
		waveSprite.color = new Color(waveSprite.color.r, waveSprite.color.g, waveSprite.color.b, minOpacity);
		t = 0f;
		while(t < 1)
		{
			t += Time.deltaTime / seenTime;
			spotLight.spotAngle = Mathf.Lerp(maxLightAngle, minLightAngle , t);
			tempScale = Mathf.Lerp(maxWaveCircleScale, minWaveCircleScale, t);
			waveCircle.transform.localScale = new Vector3(tempScale, tempScale, 1);
			waveSprite.color = new Color(waveSprite.color.r, waveSprite.color.g, waveSprite.color.b, Mathf.Lerp(minOpacity,maxOpacity,t));
			yield return null;
		}
		waveCircle.transform.localScale = new Vector3(minWaveCircleScale, minWaveCircleScale, 1);
		waveSprite.color = new Color(waveSprite.color.r, waveSprite.color.g, waveSprite.color.b, maxOpacity);
		spotLight.spotAngle = minLightAngle;
		//////

		selectedEnemy = null;

		foreach (Collider2D collider in collides)
		{
			if (collider != null)
			{
				selectedEnemy = collider.gameObject;
				target.transform.position = collider.transform.position;
				target.SetActive(true);
				check = true;
				yield return new WaitForSeconds(1f);
			}
		}
		check = false;
		target.SetActive(false);
		gameOver();
	}
	void gameOver()
	{
		
		score.saveScore();
	}
}
