using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class StoreUIManager : MonoBehaviour
{
    [Header("Store UI Elements")]
    [SerializeField] private GameObject storePanel;
    [SerializeField] private Button buyButton;
    [SerializeField] private Button cancelButton;
    [SerializeField] private TextMeshProUGUI productDetailsText;

    private IAPManager iapManager;
    private string currentProductID;
    private int currentCoinsToAdd;

    private void Awake()
    {
        // Initialize the IAPManager
        iapManager = FindObjectOfType<IAPManager>();

        // Add listeners to buttons
        buyButton.onClick.AddListener(OnBuyClicked);
        cancelButton.onClick.AddListener(OnCancelClicked);

        // Hide the store UI initially
        storePanel.SetActive(false);
    }

    // Show the store UI with product details
    public void ShowStore(string productID, int coinsToAdd)
    {
        currentProductID = productID;
        currentCoinsToAdd = coinsToAdd;

        // Update product details (optional)
        productDetailsText.text = $"Do you want to buy {coinsToAdd} coins?";

        // Show the store panel
        storePanel.SetActive(true);
    }

    // Called when the "Buy" button is clicked
    private void OnBuyClicked()
    {
        // Call the IAPManager to initiate the purchase
        iapManager.BuyProduct(currentProductID, currentCoinsToAdd);

        // Hide the store panel after initiating the purchase
        storePanel.SetActive(false);
    }

    // Called when the "Cancel" button is clicked
    private void OnCancelClicked()
    {
        // Just close the store panel without doing anything
        storePanel.SetActive(false);
    }
}
