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

public class RecipeView : MonoBehaviour
{
    [SerializeField] private GameObject lockPanel;
    [SerializeField] private Text lockPanelText;
    [SerializeField] private Button detailButton;
    [SerializeField] private Text flavorName;
    [SerializeField] private Text flavorPrice;
    [SerializeField] private Text cornAmountText;
    [SerializeField] private Text flavor1AmountText;
    [SerializeField] private Text flavor2AmountText;
    [SerializeField] private GameObject flavor2View;
    private static readonly string amountFormat = "x{0}";
    private static readonly string lockPanelTextFormat = "店舗レベル{0}で開放";
    private static readonly string lockPanelTextShopFormat = "仕入れから購入可能";
    public IObservable<Unit> RecipeClickObservable => detailButton.OnClickAsObservable().TakeUntilDestroy(this);

    private void Start()
    {
    }

    public void SetRecipe(ProductData data)
    {
        flavorName.text = data.name;
        flavorPrice.text = data.price.ToString();
        cornAmountText.text = string.Format(amountFormat, data.MaterialList[0].amount);
        flavor1AmountText.text = string.Format(amountFormat, data.MaterialList[1].amount);
        if (data.GetMaterialCount() == 3)
        {
            flavor2View.SetActive(true);
            flavor2AmountText.text = string.Format(amountFormat, data.MaterialList[2].amount);
        }
        else
        {
            flavor2View.SetActive(false);
        }

        if (data.shopLevel == Const.SpecialShopLevel)
        {
            lockPanelText.text = string.Format(lockPanelTextShopFormat, data.shopLevel);
        }
        else
        {
            lockPanelText.text = string.Format(lockPanelTextFormat, data.shopLevel);
        }
    }

    public void SetLockPanel(bool hasRecipe)
    {
        lockPanel.SetActive(hasRecipe);
    }
}
