using System;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class ShopItemView : MonoBehaviour
{
    [SerializeField] private Button detailButton;
    [SerializeField] private ShopItemPurchaseButton purchaseButton;
    [SerializeField] private Transform iconTarget;
    [SerializeField] private Text itemName;
    [SerializeField] private Text price;
    [SerializeField] private Text stockCount;

    public IObservable<Unit> DetailButtonObservable => detailButton.OnClickAsObservable().TakeUntilDestroy(this);
    public IObservable<Unit> PurchaseButtonObservable => purchaseButton.ClickObservable.TakeUntilDestroy(this);

    public void SetData(ShopData shopData)
    {
        itemName.text = shopData.name;
        price.text = $"{shopData.price}";
        if (shopData.ConsumeType == ShopItemType.NonConsumable)
        {
            stockCount.transform.parent.gameObject.SetActive(false);
        }
        iconTarget.DestroyAllChildrens();
        Instantiate(shopData.GetPrefab(), iconTarget);
        purchaseButton.SetItemType(shopData.Category, shopData.ConsumeType);
    }

    public void SetStockCount(int stock)
    {
        stockCount.text = $"{stock}";
        purchaseButton.SetItemPurchased(stock > 0);
    }

    public void SetItemActive(bool active)
    {
        purchaseButton.SetButtonActive(active);
    }
}
