using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AI_Customer : MonoBehaviour
{
    [Header("Customer State")]
    [SerializeField] private int _chosenSeat;
    [SerializeField] private bool isSitting;
    [SerializeField] private bool visitIsOver;
    [SerializeField] private float visitDuration;

    private NavMeshAgent agent;
    private Animator animator;

    public Seat seat;

    public float distanceToSit;
    public float sapToSeatSpeed;
    [SerializeField] private GameObject exit;
    private void Awake()
    {
        visitIsOver = false;
        animator = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();
        seat = GameObject.FindGameObjectWithTag("Seat").GetComponent<Seat>();
        exit = GameObject.FindGameObjectWithTag("Exit");
        SeatOrCounterPick();
    }
    private void FixedUpdate()
    {
        if(Vector3.Distance(gameObject.transform.position, seat.seatsList[_chosenSeat]._seat.position) < distanceToSit) //Checks if player is close to seat/counter position
        {
            if (!visitIsOver)
            {
                animator.SetBool("Walk", false);

                if (!seat.seatsList[_chosenSeat].isCounter) //if its not a counter then sit down
                {
                    animator.SetBool("Sit", true);
                    agent.enabled = false;
                    isSitting = true;
                }
                else if (seat.seatsList[_chosenSeat].isCounter) //if its a counter stay standing
                {
                    animator.SetBool("Walk", false);
                    animator.SetBool("Sit", false);
                    StartCoroutine(SetVisitOver());
                }

                //Go to sit/stand position
                gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, seat.seatsList[_chosenSeat]._seat.position, sapToSeatSpeed);
                gameObject.transform.rotation = seat.seatsList[_chosenSeat]._seat.rotation;
            }
        }
        if (visitIsOver)
        {
            isSitting = false;
            animator.SetTrigger("Stand");
            animator.SetBool("Walk", true);
            agent.SetDestination(exit.transform.position);
            Destroy(gameObject, 8);
        }
    }
    public int SeatOrCounterPick() //Returns the picked seat/counter & Sets Agent destination to seat/counter position
    {
        for (int i = 0; i < seat.seatsList.Count; i++)
        {
            if (!seat.seatsList[i].isBusy && seat.seatsList[i].isCounter)
            {
                agent.SetDestination(seat.seatsList[i]._seat.position);
                animator.SetBool("Walk", true);
                _chosenSeat = i;
                seat.seatsList[_chosenSeat].isBusy = true;
                break;
            }
            else if (!seat.seatsList[i].isCounter && !seat.seatsList[i].isBusy)
            {
                agent.SetDestination(seat.seatsList[i]._seat.position);
                animator.SetBool("Walk", true);
                _chosenSeat = i;
                seat.seatsList[_chosenSeat].isBusy = true;
                break;
            }
        }
        return _chosenSeat;
    }
    public int CounterPick() //Returns the picked counter & Sets Agent destination to counter position
    {
        for (int i = 0; i < seat.seatsList.Count; i++)
        {
            if (seat.seatsList[i].isCounter && !seat.seatsList[i].isBusy)
            {
                agent.SetDestination(seat.seatsList[i]._seat.position);
                animator.SetBool("Walk", true);
                _chosenSeat = i;
                seat.seatsList[_chosenSeat].isBusy = true;
                break;
            }
        }
        return _chosenSeat;
    }
    private IEnumerator SetVisitOver()
    {
        yield return new WaitForSeconds(visitDuration);
        visitIsOver = true;
        seat.seatsList[_chosenSeat].isBusy = false;
        Debug.LogError("Set busy to false");
    }
}