using System;
using UniRx;
using UnityEngine;

public class ShopHeaderView : MonoBehaviour
{
    [SerializeField] private ShopHeaderTabView materialTab;
    [SerializeField] private ShopHeaderTabView recipeTab;

    public IReadOnlyReactiveProperty<ItemCategory> Tab => tab;
    private readonly ReactiveProperty<ItemCategory> tab = new ReactiveProperty<ItemCategory>();
    private void Start()
    {
        tab.AddTo(this);
        tab.Value = ItemCategory.Material;
        tab.Subscribe(tabName =>
        {
            materialTab.SetToggle(tabName == ItemCategory.Material);
            recipeTab.SetToggle(tabName == ItemCategory.Recipe);
        }).AddTo(this);
        materialTab.ClickObservable.Select(_ => ItemCategory.Material)
            .Merge(recipeTab.ClickObservable.Select(_ => ItemCategory.Recipe))
            .Subscribe(tabName =>
            {
                tab.Value = tabName;
            }).AddTo(this);
    }

    public void SetTab(ItemCategory category)
    {
        tab.SetValueAndForceNotify(category);
    }
}
