using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyScript : MonoBehaviour {
    public float followRange = 2;
    public GameObject player;
    public float offset = .25f;
    bool inRange;
    bool inSight;
    public float speed = .1f;
    
    //These variables used for patrol mode
    public Vector3 Direction;
    public List<GameObject> PatrolNodes;
    bool patroling = true;
    public float patrolSpeed;
    public LayerMask layerMask;
    bool turning = false;
    Vector3 contactPoint;
    public float diameter;
    Vector3 direction;
    Transform lastNode;
    bool returningToPatrol = false;

    public AudioSource audio;
	// Use this for initialization
	void Start () {
        //player = GameObject.FindGameObjectWithTag("Player");
        inRange = false;
        inSight = false;
        lastNode = transform;
	
	}
	void Update()
    {

    }
	void FixedUpdate () 
    {
        checkIfInRange();
        movement();
        
	}

    void OnTriggerStay2D(Collider2D other)
    {
        Vector2 playerDirection = player.transform.position - transform.position;
        var lookingAt = Physics2D.Raycast(transform.position, playerDirection, 100, layerMask).transform.gameObject.tag;
        Debug.DrawRay(transform.position, playerDirection, Color.blue);
        if(other.tag == "Player" && lookingAt == "Player")
            inSight = true;
    }
    void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "Player")
            inSight = false;
        returningToPatrol = true;
    }
    void checkIfInRange()
    {
        float playerDist = Vector2.Distance(transform.position, player.transform.position);
        if ((playerDist < followRange) || inSight)
        {
            inRange = true;
            patroling = false;
        }
        else if (!inSight)
        {
            inRange = false;
            patroling = true;
        }
    }
    void movement()
    {
        if(inRange || inSight)
        {
            Vector2 playerDirection = player.transform.position - transform.position;
            float angle = Mathf.Atan2(playerDirection.y, playerDirection.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
            GetComponent<Rigidbody2D>().AddForce(playerDirection * speed);
        }
        else if (patroling == true)
        {
            //Movement
            float move = patrolSpeed * Time.deltaTime;
            transform.position += Direction * move;
            //Direction
            float angle = Mathf.Atan2(Direction.y, Direction.x) * Mathf.Rad2Deg;
            var rotateTo = Quaternion.AngleAxis(angle - 90, Vector3.forward);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotateTo, Time.deltaTime * 10);
            //print(Vector3.Distance(transform.position, contactPoint));
            //Turning
            if(turning && (Vector3.Distance(transform.position,contactPoint) > diameter))
            {
                //print("Turning");
                Direction = direction;
            }
        }
        else
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0,0);
        }
    }

    void GoToPlayer()
    {
        Vector2 playerDirection = player.transform.position - transform.position;
        float angle = Mathf.Atan2(playerDirection.y, playerDirection.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
        GetComponent<Rigidbody2D>().AddForce(playerDirection * speed);
    }
    public void Direct(List<Vector3> Directions,Transform position)
    {
            System.Random random = new System.Random();
            direction = Directions[random.Next(0, Directions.Count)];
            turning = true;
            contactPoint = gameObject.transform.position;
            lastNode = position;
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        audio.Play();
        other.gameObject.SendMessage("GameOver", SendMessageOptions.DontRequireReceiver);
    }

}
