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


[System.Serializable]
public class Seat
{
    public GameObject seatObject;
    public bool istaken;
}

public class SeatAllocator : MonoBehaviour {
    public static SeatAllocator instance;

    public GameObject[] chairs;
    [SerializeField]
    private List<Seat> seats;

    public GameObject[] exitPositions;

    public Guest targetGuest;

    void Start() {

        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }

        //seats = new Seat[chairs.Length];
        for (int i = 0; i < chairs.Length; i++) {
            seats.Add(new Seat());
            //Debug.Log("Creating seat " + i);
            seats[i].seatObject = chairs[i];
            seats[i].istaken = false;
        }
    }

    public int AssignSeat()
    {
        int index = -1;
        for (int i = 0; i < seats.Count; i++)
        {
            if (!seats[i].istaken)
            {
                seats[i].istaken = true;
                index = i;
                i = seats.Count;
            }
        }
        //Debug.Log("Returned assign seat index = " + index);
        //return  -1 if no available
        return index;
    }

    public void UnAssignSeat(int seat) {
        //Debug.Log("Unassign seat index = " + seat);
        if (seat < 0)
            return;
        seats[seat].istaken = false;
    }

    public Vector3 AssignSeatPosition() {
        int index = 0;
        for (int i = 0; i < seats.Count; i++)
        {
            if (!seats[i].istaken)
            {
                seats[i].istaken = true;
                index = i;
                i = seats.Count;
            }
        }

        return seats[index].seatObject.transform.position;
    }

    /*public Vector3[] AssignSeat(int numberOfGuests)
    {
        int index[] = 0;
        for (int i = 0; i < seats.Length; i++)
        {
            if (!seats[i].istaken)
            {
                seats[i].istaken = true;
                index = i;
                i = seats.Length;
            }
        }

        //return seats[index].seatObject.transform.position;
        return new Vector3(0,0,0);
    }*/
}
