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


public class EnemyAI : MonoBehaviour
{

    NavMeshAgent nm;
    public Transform target;


    public float distanceThreshold = 10f;


    public enum AIState { idle, chasing, attack, wander };

    public AIState aiState = AIState.idle;

    public Animator animator;

    public float attackThreshold = 1.5f;

    public float wanderThreshold = 10f;



    // Start is called before the first frame update
    void Start()
    {
        nm = GetComponent<NavMeshAgent>();
        StartCoroutine(Think());
        target = GameObject.FindGameObjectWithTag("Player").transform;

    }

    // Update is called once per frame
    void Update()
    {

    }


    IEnumerator Think()
    {
        while (true)
        {
            switch (aiState)
            {
                case AIState.idle:
                    float dist = Vector3.Distance(target.position, transform.position);
                    if (dist < distanceThreshold)
                    {
                        aiState = AIState.chasing;
                        animator.SetBool("Chasing", true);
                    }
                    if (dist < wanderThreshold)
                    {
                        //go into wander state
                        aiState = AIState.wander;

                    }
                    nm.SetDestination(transform.position);
                    break;
                case AIState.chasing:
                    dist = Vector3.Distance(target.position, transform.position);
                    if (dist > distanceThreshold)
                    {
                        aiState = AIState.idle;
                        animator.SetBool("Chasing", false);
                    }

                    if (dist < attackThreshold)
                    {
                        aiState = AIState.attack;
                        animator.SetBool("Attacking", true);
                    }
                    nm.SetDestination(target.position);
                    break;
                case AIState.attack:
                    Debug.Log("Attack!");
                    nm.SetDestination(transform.position);
                    dist = Vector3.Distance(target.position, transform.position);
                    if (dist > attackThreshold)
                    {
                        aiState = AIState.chasing;
                        animator.SetBool("Attacking", false);
                    }
                    break;
                case AIState.wander:
                    Debug.Log("Wandering");
                    dist = Vector3.Distance(target.position, transform.position);

                    if (dist > wanderThreshold)
                    {
                        aiState = AIState.chasing;
                    }
                    break;

                default:
                    break;
            }
            yield return new WaitForSeconds(0.2f);
        }



    }



}