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

public class ShopItem
{

}

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

    #endregion

    


    public GameObject ShopGrid;
    public GameObject prefab;
    public Text ToolTip;
    public Text ErrorCode;
    public Text playerMoney;
    public Text playerMoneyInventory;

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

    public List<ShopObject> ShopContent = new List<ShopObject>();

    //MUST BE CHANGED FOR REAL PLAYER CLASS
    private PlayerData player;

    private void Start()
    {
        //GenerateItems();
        PopulateShop();

        player = FindObjectOfType<PlayerData>();
        UpdatePlayerMoney();
    }

    private void OnEnable()
    {
        Junk.OnPickedUp += UpdatePlayerMoney;
    }

    private void OnDisable()
    {
        Junk.OnPickedUp -= UpdatePlayerMoney;
    }

    public void PopulateShop()
    {
        for (int i = 0; i < ShopContent.Count; i++)
        {
            slots.Add(Instantiate(prefab));
            slots[i].transform.SetParent(ShopGrid.transform, false);
            slots[i].AddComponent<Offer>().Init(i, true);
            slots[i].GetComponentInChildren<Text>().text = ShopContent[i].price.ToString();
            slots[i].GetComponentsInChildren<Image>()[1].sprite = ShopContent[i].item.sprite;
            slots[i].GetComponentInChildren<Button>().onClick.AddListener(slots[i].GetComponent<Offer>().BuySelf);
            //Debug.Log(ShopContent[i].item);
            HideWhenEmpty(i);
        }
    }

    public void HideWhenEmpty(int _id)
    {
        Debug.Log("Hiding offer from PDA..");
        GameObject toDelete = slots[_id];
        toDelete.SetActive(false);
        slots[_id].GetComponent<Offer>().isActive = false;
        //Destroy(toDelete.gameObject);
        bool empty = true;
        for (int i = 0; i < slots.Count; i++)
        {
            if (slots[i].GetComponent<Offer>().isActive)
            {
                empty = false;
            }
        }
        if (empty)
        {
            ErrorCode.gameObject.SetActive(true);
        }
    }


    public void Buy(int _item_id)
    {
        ShopObject itemToBuy = ShopContent[_item_id];
        if (itemToBuy == null) return;

        if (itemToBuy.inStock <= 0)
        {
            Debug.Log("Item is sold out.");
            HideWhenEmpty(_item_id);
            return;
        }

        if ( itemToBuy.price > player.Money )
        {
            Debug.Log("Not enough money to buy this item.");
            return;
        }

        //The item is in stock and player has enough money
        player.Money -= itemToBuy.price;
        itemToBuy.inStock--;
        Debug.Log(itemToBuy.item);
        player.AddItemToInventory(itemToBuy.item, 1);
        Debug.Log($"You bought {itemToBuy.item.name}.");

        UpdatePlayerMoney();

        if (itemToBuy.inStock < 1)
        {
            HideWhenEmpty(_item_id);
        }
    }

    private void UpdatePlayerMoney()
    {
        playerMoney.text = player.Money.ToString();
        playerMoneyInventory.text = player.Money.ToString();
}
}

[System.Serializable]
public class ShopObject 
{
    public ItemObject item;
    public int price;
    public int inStock;

}