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

public class Enemy : Character {

	[SerializeField] int purse = 0;
	[SerializeField] int fill = 4;

	bool isFinished = false;
	bool hasExited = false;

	// Use this for initialization
	void Start () {
		base.Start();
		target = GameManager.instance.GetPlayer().transform.position;
		health = maxHealth;
		purse += Random.Range(0, 2);
	}
	
	// Update is called once per frame
	void Update () {
		base.Update();
		
		if (!isDead) {
			lastPosition = transform.position;
			if (!isFinished) {
				if (GameManager.instance.GetPlayer().GetComponent<Character>().isDead) {
					isFinished = true;
					RunAway();
				} else {
					target = GameManager.instance.GetPlayer().transform.position;
				}
				if (!isRolling && !isAttacking && coinsInRange.Count > 0) {
					Collider[] hitColliders = Physics.OverlapSphere(transform.position, transform.localScale.x * coinRadius);
					if (hitColliders.Length > 0) {
						for (int x = 0; x < hitColliders.Length; x += 1) {
							if (coinsInRange.Contains(hitColliders[x].gameObject) && hitColliders[x].transform.GetComponent<Coin>().isActive) {
								target = hitColliders[x].transform.position;
								break;
							}
						}
					}
				} else if (!isRolling && !isAttacking && charsInRange.Contains(GameManager.instance.GetPlayer())) {
					isAttacking = true;
					int random = Random.Range(0, 2);
					if (random == 1) {
						ThrowPunchLeft();
					} else {
						ThrowPunchRight();
					}
				}
			}

			if (!isRolling && !isAttacking) {
				float trackX = target.x - transform.position.x;
				float trackY = target.y - transform.position.y;
				float speedX = Mathf.Sign(trackX) * movementSpeed * Time.deltaTime;
				float speedY = Mathf.Sign(trackY) * movementSpeed * Time.deltaTime;
				float posX = transform.position.x + speedX;
				float posY = transform.position.y + speedY;
				if (Mathf.Abs(transform.position.x - posX) > Mathf.Abs(transform.position.x - target.x)) {
					posX = target.x;
				}
				if (Mathf.Abs(transform.position.y - posY) > Mathf.Abs(transform.position.y - target.y)) {
					posY = target.y;
				}
				transform.position = new Vector3(posX, posY);
				if (isFinished) {
					float d = Vector2.Distance(transform.position, target);
					if (d <= .5f) {
						if (!hasExited && !GameManager.instance.GetPlayer().GetComponent<Character>().isDead) {
							GameManager.instance.SpawnEnemy();
							GameManager.instance.SpawnEnemy();
							transform.gameObject.SetActive(false);
							// StartCoroutine(CallForHelp());
						}
						transform.gameObject.SetActive(false);
					}
				}
			}
		}
	}

	override public void CoinPickup(GameObject coin) {
		Coin c = coin.GetComponent<Coin>();
		purse += c.GetValue();
		// play noise
		GameManager.instance.PlayEnemyCoinCollect();
		c.Hooray();

		if (purse >= fill) {
			isFinished = true;
			RunAway();
		}
	}

	IEnumerator CallForHelp() {
		hasExited = true;
		GameManager.instance.SpawnEnemy();
        yield return new WaitForSeconds(1);
		GameManager.instance.SpawnEnemy();
		transform.gameObject.SetActive(false);
	}

	void RunAway() {
		float xDif = GameManager.instance.GetPlayer().transform.position.x - transform.position.x;
		float yDif = GameManager.instance.GetPlayer().transform.position.y - transform.position.y;

		Bounds bounds = GetComponent<SphereCollider>().bounds;
		Vector3 screenPosition;

		if (yDif >= 0 && Mathf.Abs(yDif) >= Mathf.Abs(xDif)) {
			screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), 0, 0));
			target = new Vector3(screenPosition.x, screenPosition.y - (bounds.size.y / 2) - 1, transform.position.z);
		} else if (yDif < 0 && Mathf.Abs(yDif) >= Mathf.Abs(xDif)) {
			screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Screen.height, 0));
			target = new Vector3(screenPosition.x, screenPosition.y + (bounds.size.y / 2) + 1, transform.position.z);
		} else if (xDif < 0 && Mathf.Abs(yDif) < Mathf.Abs(xDif)) {
			screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Random.Range(0,Screen.height), 0));
			target = new Vector3(screenPosition.x + (bounds.size.x / 2) + 1, screenPosition.y, transform.position.z);
		} else {  
			screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(0, Random.Range(0,Screen.height), 0));
			target = new Vector3(screenPosition.x - (bounds.size.x / 2) - 1, screenPosition.y, transform.position.z);
		}
	}

	override public void YouDied() {
		GameManager.instance.PlayEnemyDeath();
		GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1 - 50;
		fistLeft.SetActive(false);
		fistRight.SetActive(false);
		legs.SetActive(false);

		animator.SetBool("isDead", true);
		isDead = true;

		GameManager.instance.SetEnemies();
		GameManager.instance.SetCharacters();

		GameManager.instance.AddKill(1);

		if (GameManager.instance.GetEnemies().Count == 0) {
			GameManager.instance.SpawnEnemy();
		}

		// drop coins
		for (int x = 0; x < purse; x += 1) {
			DropCoin();
		}
	}

	public void KillMe() {
		GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1 - 50;
		fistLeft.SetActive(false);
		fistRight.SetActive(false);
		legs.SetActive(false);

		animator.SetBool("isDead", true);
		isDead = true;
	}
}
