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

public class PDAInventory : MonoBehaviour
{
    #region Singleton
    public static PDAInventory Instance;
    private void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        else
        {
            Instance = this;
        }
    }
    #endregion

    public Text playerNameLabel;
    public GameObject PlayerStatsGrid;
    public GameObject PlayerStatPrefab;

    public GameObject inventoryGrid;
    public GameObject inventoryItemPrefab;

    private List<GameObject> statslots = new List<GameObject>();
    private List<GameObject> slots = new List<GameObject>();

    private PlayerData player;

    // Start is called before the first frame update
    void Start()
    {
        player = FindObjectOfType<PlayerData>();
        RefreshStatsUI();
        playerNameLabel.text = player.name;
    }

    private void OnEnable()
    {
        PlayerData.OnAddItemEvent += RefreshUI;
        PlayerData.OnStatChanged += RefreshStatsUI;
        Health.OnHealthChanged += RefreshHealth;
        BeaconManager.OnBeaconPlaced += BeaconPlaced;
    }
    private void OnDisable()
    {
        PlayerData.OnAddItemEvent -= RefreshUI;
        PlayerData.OnStatChanged -= RefreshStatsUI;
        Health.OnHealthChanged -= RefreshHealth;
        BeaconManager.OnBeaconPlaced -= BeaconPlaced;
    }


    public void BeaconPlaced(int[] coords)
    {
        RefreshUI();
    }

    public void RefreshUI()
    {
        for (int i = 0; i < slots.Count; i++)
        {
            GameObject slot = slots[i];
            Destroy(slot.gameObject);
        }
        slots.Clear();

        for(int i = 0; i < player.inventory.Container.Count; i++)
        {
            GameObject slot = Instantiate(inventoryItemPrefab);
            slots.Add(slot);
            slot.transform.SetParent(inventoryGrid.transform, false);
            slot.GetComponentsInChildren<Image>()[0].sprite = player.inventory.Container[i].item.sprite;
            slot.GetComponentsInChildren<Text>()[0].text = player.inventory.Container[i].item.name;
            if (player.inventory.Container[i].item.stackable == true){
                slot.GetComponentsInChildren<Text>()[1].text = player.inventory.Container[i].count.ToString();
            }
            else
            {
                slot.GetComponentsInChildren<Text>()[1].text = string.Empty;
            }
        }

    }

    public void RefreshStatsUI()
    {
        for (int i = 0; i < statslots.Count; i++)
        {
            GameObject slot = statslots[i];
            Destroy(slot.gameObject);
        }
        statslots.Clear();

        Health h = player.GetComponent<Health>();
        if(h != null)
        {
            GameObject hslot = Instantiate(PlayerStatPrefab);
            statslots.Add(hslot);
            hslot.transform.SetParent(PlayerStatsGrid.transform, false);
            hslot.GetComponent<Text>().text = "Health: " + h.Value.ToString();
        }

        for (int i = 0; i < player.stats.Count; i++)
        {
            GameObject slot = Instantiate(PlayerStatPrefab);
            statslots.Add(slot);
            slot.transform.SetParent(PlayerStatsGrid.transform, false);
            slot.GetComponentsInChildren<Text>()[0].text = player.stats[i].name + ": " + player.stats[i].value.ToString();
        }
    }

    public void RefreshHealth(float _health)
    {
        statslots[0].GetComponent<Text>().text = "Health: " + ((int)_health).ToString();
    }

}
