using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class EnemyAI2 : MonoBehaviour
{
    /*
    public GameObject currentHitObject;

    public float sphereRadius;
    public float maxDistance;
    public LayerMask layerMask;

    public NavMeshAgent nav;
    private Vector3 origin;
    private Vector3 direction;

    private float currentHitDistance;

    void Start()
    {
        nav = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        origin = transform.position;
        direction = transform.forward;
        RaycastHit hit;
        Debug.DrawRay(transform.position + Vector3.up, transform.forward, Color.green);
        Debug.DrawRay(transform.position + Vector3.up, (transform.forward+transform.right).normalized, Color.green);
        Debug.DrawRay(transform.position + Vector3.up, (transform.forward - transform.right), Color.green);
        if (Physics.SphereCast(origin,sphereRadius,direction,out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
        {
            if()
            currentHitObject = hit.transform.gameObject;
            currentHitDistance = hit.distance;
            nav.SetDestination(hit.transform.position);
        } else
        {
            currentHitDistance = maxDistance;
            currentHitObject = null;
            
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Debug.DrawLine(origin, origin + direction * currentHitDistance);
        Gizmos.DrawWireSphere(origin + direction * currentHitDistance, sphereRadius);
    }
    */
    public NavMeshAgent agent;
    public enum State
    {
        PATROL,
        CHASE,
        INVESTIGATE
    }

    public State state;
    private bool alive;

    public GameObject[] waypoints;
    private int waypointInd;
    public float patrolSpeed = 0.2f;

    public float chaseSpeed = 0.3f;
    public GameObject target;

    private Vector3 investigateSpot;
    private float timer = 0;
    public float investigateWait = 3;
    private float stayTimer = 0;

    public float heightMultiplier;
    public float sightDist = 10;

    public CharacterController cc;

    private void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        cc = GetComponent<CharacterController>();

        agent.updatePosition = true;
        agent.updateRotation = false;
        waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
        waypointInd = Random.Range(0, waypoints.Length);

        state = State.PATROL;

        alive = true;

        heightMultiplier = 1.36f;

        StartCoroutine("FSM");
    }

    IEnumerator FSM()
    {
        while (alive)
        {
            switch (state)
            {
                case State.PATROL:
                    Patrol();
                    break;
                case State.CHASE:
                    Chase();
                    break;
                case State.INVESTIGATE:
                    Investigate();
                    break;
            }
            yield return null;
        }
    }

    void Patrol()
    {
        agent.speed = patrolSpeed;
        if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2)
        {
            agent.SetDestination(waypoints[waypointInd].transform.position);
            //cc.Move(agent.desiredVelocity);
        }
        else if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2) {
            waypointInd = Random.Range(0, waypoints.Length);
        }
        else
        {
            //cc.Move(Vector3.zero);
        }
    }
    Transform temp;
    void Chase()
    {
        RaycastHit hit;
        
        if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, transform.forward, out hit, sightDist))
        {
            Debug.Log(hit.collider.gameObject.tag);
            if (hit.collider.gameObject.tag == "Player")
            {
                temp = hit.collider.gameObject.transform;
            }
            else {
                state = State.INVESTIGATE;
            }
        }
        agent.speed = chaseSpeed;
        transform.LookAt(temp.transform.position);
        agent.SetDestination(temp.transform.position);
        //cc.Move(agent.desiredVelocity);

    }

    void Investigate()
    {
        timer += Time.deltaTime;
        RaycastHit hit;
        Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, transform.forward * sightDist, Color.green);
        Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward + transform.right).normalized * sightDist, Color.green);
        Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized * sightDist, Color.green);
        
        agent.SetDestination(this.transform.position);
        //cc.Move(Vector3.zero);
        transform.LookAt(investigateSpot);

        if (timer >= investigateWait)
        {
            state = EnemyAI2.State.PATROL;
            timer = 0;
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        stayTimer += Time.deltaTime;
        if (other.tag == "Player")
        {
            state = EnemyAI2.State.INVESTIGATE;
            investigateSpot = other.gameObject.transform.position;
        }
    }
    private void OnTriggerStay(Collider other)
    {
            RaycastHit hit;
            if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, transform.forward, out hit, sightDist))
            {
                if (hit.collider.gameObject.tag == "Player")
                {
                    state = EnemyAI2.State.CHASE;
                    target = hit.collider.gameObject;
                }
            }
            if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, (transform.forward + transform.right).normalized, out hit, sightDist))
            {
                if (hit.collider.gameObject.tag == "Player")
                {
                    state = EnemyAI2.State.CHASE;
                    target = hit.collider.gameObject;
                }
            }
            if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized, out hit, sightDist))
            {
                if (hit.collider.gameObject.tag == "Player")
                {
                    state = EnemyAI2.State.CHASE;
                    target = hit.collider.gameObject;
                }
            }
    }

}