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

public class TastingView : MonoBehaviour
{
    public static readonly string TastingDataTag = "TastingData";
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private Button closeButton;
    [SerializeField] private Button tastingButton;
    [SerializeField] private Text popcornName;
    [SerializeField] private Image popcornImage;
    [SerializeField] private Image packageImage;

    private void Start()
    {
        var data = LocalCacheManager.Load<(IObservable<(int tankNum, RecipeData recipe)> selectedObservable, Action closeButtonAction, Action tastingButtonAction)>(TastingDataTag);
        data.selectedObservable.Subscribe(x =>
        {
            popcornName.text = x.recipe.Name;
        }).AddTo(this);
        closeButton.OnClickAsObservable().Subscribe(_ =>
        {
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.5f, () =>
            {
                data.closeButtonAction?.Invoke();
                TransitionManager.Instance.UnloadScene(GameScenes.Tasting);
            });
        }).AddTo(this);
        tastingButton.OnClickAsObservable().Subscribe(_ =>
        {
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.5f, () =>
            {
                data.tastingButtonAction?.Invoke();
                TransitionManager.Instance.UnloadScene(GameScenes.Tasting);
            });
        }).AddTo(this);
    }
}