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

public class RecipeSelectDialog : MonoBehaviour
{
    [SerializeField] private GameObject content;
    [SerializeField] private RecipeView recipePrefab;
    [SerializeField] private Button closeButton;
    // Start is called before the first frame update
    void Start()
    {
        closeButton.OnClickAsObservable().Subscribe(_ =>
        {
            TransitionManager.Instance.UnloadScene(GameScenes.Recipe);
        }).AddTo(this);
        
        // レシピ一覧生成
        content.transform.DestroyAllChildrens();
        foreach (var recipeData in RecipeData.GetAllRecipe())
        {
            var view = Instantiate(recipePrefab, content.transform);
            view.SetRecipe(recipeData);
            // 所持レシピ確認
            if (GameDataManager.GameData.MyRecipes.Contains(recipeData.RecipeId))
            {
                view.SetLockPanel(false);
                view.RecipeClickObservable.Subscribe(_ =>
                {
                    LocalCacheManager.Save(RecipeDetailView.DetailRecipeTag, recipeData);
                    TransitionManager.Instance.LoadSceneAdditive(GameScenes.RecipeChoice);
                }).AddTo(this);
            }
            else
            {
                view.SetLockPanel(true);
            }
        }
    }
}
