using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityStandardAssets.Characters.ThirdPerson;


public class EnemyAIVer2 : MonoBehaviour {

	public NavMeshAgent agent;
	public ThirdPersonCharacter character;
	public Transform playerPos;
    public int health = 4;
	// Variables for chasing 
	public float chaseSpeed = 1f;
	public GameObject player; // target for enemy should be player
	public float playerDistance; // distance to keep away from player so enemy can shoot
	private float direction = 0;
	private bool tooClose = false;
	private float fireRate = 0;
    private Rigidbody rb;

	// Use this for initialization
	void Start () {
		agent = GetComponent<NavMeshAgent> ();
		character = GetComponent<ThirdPersonCharacter> ();
		agent.updatePosition = true;
		agent.updateRotation = false;
		playerDistance = Vector3.Distance (player.transform.position, transform.position);
        playerPos = player.transform;
        rb = GetComponent<Rigidbody>();
	}
	
	// Update is called once per frame
	void Update () {
		playerDistance = Vector3.Distance (player.transform.position, transform.position);
		direction = AngleBetweenTwoPoints(transform.position, player.transform.position);
		Look ();
		fireRate += Time.deltaTime;
		if(fireRate >= 2)
		{
			spawnBullet (direction);
			fireRate = 0;
		}
		//Chase ();
		if (playerDistance > 15) {
			//tooClose = false;
			Chase();
		} 
		if (playerDistance < 5) {
			//tooClose = true;
			Retreat();
		}
		//if (tooClose) {
		//	Retreat ();
		//}
		//if (!tooClose) {
		//	Chase ();
		//}
	}

    private void LateUpdate()
    {
        if (health <= 0)
        {
            Destroy(gameObject);
        }
    }

    void Chase() {
        //transform.Translate (Vector3.forward * Time.deltaTime * 10);
        rb.velocity = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (direction - 180)), 0, Mathf.Sin(Mathf.Deg2Rad * (direction - 180))) * 500 * Time.deltaTime;
    }

    void Retreat() {
        //transform.Translate (Vector3.back * Time.deltaTime * 10);
        rb.velocity = new Vector3(Mathf.Cos(Mathf.Deg2Rad * direction), 0, Mathf.Sin(Mathf.Deg2Rad * direction)) * 500 * Time.deltaTime;

    }

    void Shoot() {
		
	}

	void Look() {
		transform.LookAt (playerPos);
	}

	void spawnBullet(float direction)
	{
		GameObject bullet = Instantiate(Resources.Load("EnemyBullet", typeof(GameObject))) as GameObject;
		bullet.transform.position = transform.position;
		bullet.transform.rotation = transform.rotation;
		bullet.GetComponent<EnemyBulletBehavior>().direction = direction - 180;
		Physics.IgnoreCollision (bullet.GetComponent<Collider> (), GetComponent<Collider> ());
	}

	float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
	{
		return Mathf.Atan2(a.z - b.z, a.x - b.x) * Mathf.Rad2Deg;
	}

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Bullet")
        {
            health -= collision.gameObject.GetComponent<BulletBehavior>().damage;
        }
    }
}
