using System.Collections.Generic;
using UnityEngine;

public sealed class AI_World
{
    private static readonly AI_World instance = new AI_World();
    private static AIStates worldStates;
    private static Queue<GameObject> patients;
    private static Queue<GameObject> chargers;
    private static Queue<GameObject> treatmentBeds;

    static AI_World()
    {
        worldStates = new AIStates();
        patients = new Queue<GameObject>();
        chargers = new Queue<GameObject>();
        treatmentBeds = new Queue<GameObject>();

        GameObject[] cubicleArray = GameObject.FindGameObjectsWithTag("Charger");
        foreach(GameObject cubicle in cubicleArray)
        {
            chargers.Enqueue(cubicle);
        }

        if (cubicleArray.Length > 0)
        {
            worldStates.SetState("FreeCharger", cubicleArray.Length);
        }
        
        GameObject[] bedArray = GameObject.FindGameObjectsWithTag("Bed");
        
        foreach(GameObject bed in bedArray)
        {
            treatmentBeds.Enqueue(bed);
        }
        
        if (bedArray.Length > 0)
        {
            worldStates.SetState("FreeBeds", bedArray.Length);
        }
    }

    private AI_World() { }

    public void AddPatient(GameObject patient)
    {
        patients.Enqueue(patient);
        worldStates.ModifyState("Patients", 1);
    }

    public GameObject RemovePatient()
    {
        if (patients.Count == 0) return null;
        worldStates.ModifyState("Patients", -1);
        return patients.Dequeue();
    }

    public void AddCharger(GameObject charger)
    {
        worldStates.ModifyState("FreeCharger", 1);
        chargers.Enqueue(charger);
    }

    public GameObject RemoveCharger()
    {
        if (chargers.Count == 0) return null;
        worldStates.ModifyState("FreeCharger", -1);
        return chargers.Dequeue();
    }
    
    public void AddTreatmentBed(GameObject bed)
    {
        treatmentBeds.Enqueue(bed);
        worldStates.ModifyState("FreeBeds", 1);
    }
    
    public GameObject RemoveTreatmentBed()
    {
        if (treatmentBeds.Count == 0) return null;
        worldStates.ModifyState("FreeBeds", -1);
        return treatmentBeds.Dequeue();
    }

    public static AI_World Instance
    { 
        get { return instance; } 
    }

    public AIStates GetWorld()
    {
        return worldStates;
    }
}