using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class GameManager : MonoBehaviour
{
    public int currency;
    public int health = 2;
    public float timeLeft;

    System.Random random = new System.Random();

    private TextMeshProUGUI soulsLeft;
    private TextMeshProUGUI wallet;
    private TextMeshProUGUI timeDisp;

    public Image[] hpmaximum;

    public bool allFound;

    public List<GameObject> rooms = new List<GameObject>();
    public GameObject spawnRoom;
    public GameObject furnacePrefab;
    public GameObject soulPrefab;
    public GameObject exitPrefab;
    public int roomCount = 0;
    public int maxRooms = 10;
    public bool GenerationComplete = false;
    public bool entitiesSpawned = false;
    public GameObject shopUI;

    public List<GameObject> roomSpawnpoints = new List<GameObject>();
    public List<GameObject> spawnedRooms = new List<GameObject>();
    public List<GameObject> furnaceSpawnpoints = new List<GameObject>();
    public List<GameObject> soulSpawnpoints = new List<GameObject>();
    public List<GameObject> exitSpawnpoints = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        roomSpawnpoints = new List<GameObject>();
        spawnedRooms = new List<GameObject>();
        soulsLeft = GameObject.Find("Canvas/Soul Counter").GetComponent<TextMeshProUGUI>();
        wallet = GameObject.Find("Canvas/Wallet/Currency Display").GetComponent<TextMeshProUGUI>();
        timeDisp = GameObject.Find("Canvas/Timer").GetComponent<TextMeshProUGUI>();
        GenerateMap();
        for (int i = 0; i < hpmaximum.Length; i++)
        {
            if (i < health)
            {
                hpmaximum[i].enabled = true;
            }
            else
            {
                hpmaximum[i].enabled = false;
            }
        }
    }

    public void GenerateMap()
    {
        entitiesSpawned = false;
        roomCount = 0;
        GameObject room = Instantiate(spawnRoom);
        spawnedRooms.Add(room);
        room.transform.position = new Vector3(0, 0, 0);
        Invoke("GenerateRooms", 5f);
    }

    public void GenerateRooms()
    {
        while (roomCount < maxRooms)
        {
            Invoke("GenerateRoom", 1f);
            roomCount++;
        }
        GenerationComplete = true;
    }

    public bool GenerateRoom()
    {
        return roomSpawnpoints[random.Next(0, roomSpawnpoints.Count)].GetComponent<RoomSpawner>().SpawnRoom();
    }

    public int Random(int max = 1)
    {
        return random.Next(0, max);
    }

    public void SpawnEntity(GameObject _entity, List<GameObject> _entities)
    {
        GameObject entity = Instantiate(_entity);
        entity.transform.position = _entities[random.Next(0, _entities.Count)].transform.position;
    }

    public List<GameObject> Clear(List<GameObject> list)
    {
        foreach (GameObject entity in list)
        {
            Destroy(entity, 10);
        }
        return new List<GameObject>();
    }

    public void removeEmpty(List<GameObject> list)
    {
        foreach(GameObject item in list)
        {
            if (item == null)
            {
                list.Remove(item);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (health > hpmaximum.Length)
        {
            health = hpmaximum.Length;
        }
        for (int i = 0; i < hpmaximum.Length; i++)
        {
            if (i < health)
            {
                hpmaximum[i].enabled = true;
            }
            else
            {
                hpmaximum[i].enabled = false;
            }
        }
        string remainingSouls = GameObject.FindGameObjectsWithTag("Soul").Length.ToString();
        if (GameObject.FindGameObjectsWithTag("Soul").Length > 0)
        {
            soulsLeft.text = "Souls Left:\n" + remainingSouls;
        }
        else
        {
            soulsLeft.text = "Souls Found!\nFind The Exit!";
        }
        if (timeLeft > 0)
        {
            timeLeft -= Time.deltaTime;
            timeDisp.text = ((int)timeLeft + 1).ToString();
        }
        wallet.text = currency.ToString();

        if (GameObject.FindGameObjectsWithTag("Soul").Length == 0 && !allFound && entitiesSpawned)
        {
            allFound = true;
            SpawnEntity(exitPrefab, exitSpawnpoints);
        }

        if (GenerationComplete && !entitiesSpawned)
        {
            removeEmpty(soulSpawnpoints);
            removeEmpty(furnaceSpawnpoints);
            removeEmpty(exitSpawnpoints);

            entitiesSpawned = true;

            
        
            
            
            foreach (GameObject s in soulSpawnpoints)
            {
                int a = random.Next(0, 99);
                if (a <= 75 && s != null)
                {
                    GameObject entity = Instantiate(soulPrefab);
                    entity.transform.position = s.transform.position;
                }
            }
            Clear(soulSpawnpoints);
            SpawnEntity(furnacePrefab, furnaceSpawnpoints);
        }
    }
}
