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

public class ShopItemPurchaseButton : MonoBehaviour
{
    [SerializeField] private Button purchaseButton;
    [SerializeField] private GameObject onObject;
    [SerializeField] private GameObject offObject;
    [SerializeField] private GameObject soldOutObject;
    [SerializeField] private GameObject learnedObject;
    [SerializeField] private GameObject purchasedObject;
    private ShopItemType itemType;
    private bool isPurchased;

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

    public void SetItemType(ItemCategory category, ShopItemType type)
    {
        itemType = type;
        switch (type)
        {
            case ShopItemType.Consumable:
                soldOutObject.SetActive(false);
                break;
            case ShopItemType.NonConsumable:
                learnedObject.SetActive(category == ItemCategory.Recipe);
                purchasedObject.SetActive(category == ItemCategory.Pan);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
        }
    }

    public void SetItemPurchased(bool active)
    {
        isPurchased = active;
    }

    public void SetButtonActive(bool active)
    {
        switch (itemType)
        {
            case ShopItemType.Consumable:
                onObject.SetActive(active);
                offObject.SetActive(!active);
                break;
            case ShopItemType.NonConsumable:
                onObject.SetActive(active && !isPurchased);
                offObject.SetActive(!active && !isPurchased);
                soldOutObject.SetActive(isPurchased);
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}
