using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif

public class SpawnManager : MonoBehaviour
{
    public List<GameObject> npcPrefabs = new List<GameObject>();
    public List<GameObject> itemPrefabs = new List<GameObject>();

    public int minNumberOfNPCs = 1;
    public int maxNumberOfNPCs = 3;
    public float minCompetency = 0f;
    public float maxCompetency = 1.0f;
    public float minDurability = 0f;
    public float maxDurability = 1.0f;
    private int decimalPlaces = 1;

    public List<GameObject> spawnedNPCs { get; private set; } = new List<GameObject>();
    public List<GameObject> spawnedItems { get; private set; } = new List<GameObject>();

    private bool allowSpawning = false;

    public void SpawnButtonPressed()
    {
        allowSpawning = true;
        SpawnNPCs();
    }

    public void SpawnNPCs()
    {
        if (!Application.isPlaying)
        {
            Debug.LogError("Spawning is only allowed in Play mode.");
            return;
        }

        DestroyAllNPCs();
        DestroyAllItems();

        int numberOfNPCs = Random.Range(minNumberOfNPCs, maxNumberOfNPCs + 1);

        for (int i = 0; i < numberOfNPCs; i++)
        {
            // NPC Spawning
            GameObject npcPrefab = GetRandomPrefab(npcPrefabs);
            GameObject newNPC = Instantiate(npcPrefab, transform.position + Random.insideUnitSphere * 5f, Quaternion.identity);
            spawnedNPCs.Add(newNPC);

            float npcCompetency = Random.Range(minCompetency, maxCompetency);
            npcCompetency = Mathf.Round(npcCompetency * Mathf.Pow(10, decimalPlaces)) / Mathf.Pow(10, decimalPlaces);

            NPC refToNPC = newNPC.AddComponent<NPC>();
            refToNPC.UpdateCompetency(npcCompetency);

            // Item Spawning
            GameObject itemPrefab = GetRandomPrefab(itemPrefabs);
            GameObject newItem = Instantiate(itemPrefab, transform.position + Random.insideUnitSphere * 5f, Quaternion.identity);
            spawnedItems.Add(newItem);

            Item refToItemScript = newItem.GetComponent<Item>();

            if (refToItemScript != null)
            {
                refToItemScript.minDurability = minDurability;
                refToItemScript.maxDurability = maxDurability;

                refToItemScript.GetRandomDurability(npcCompetency);
            }
        }
    }

    void DestroyAllNPCs()
    {
        foreach (GameObject npc in spawnedNPCs)
        {
            DestroyImmediate(npc);
        }
        spawnedNPCs.Clear(); // Clear the list after destroying NPCs
    }

    void DestroyAllItems()
    {
        foreach (GameObject item in spawnedItems)
        {
            DestroyImmediate(item);
        }
        spawnedItems.Clear(); // Clear the list after destroying items
    }

    GameObject GetRandomPrefab(List<GameObject> prefabList)
    {
        if (prefabList.Count > 0)
        {
            int randomIndex = Random.Range(0, prefabList.Count);
            return prefabList[randomIndex];
        }
        else
        {
            Debug.LogError("Prefab list is empty!");
            return null;
        }
    }

    [ContextMenu("Reset Scene")]
    private void ResetScene()
    {
#if UNITY_EDITOR
        EditorSceneManager.LoadScene(EditorSceneManager.GetActiveScene().buildIndex);
#endif
    }
}