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

public class ShopItemDetailView : MonoBehaviour
{
    public static readonly string ItemDetailTag = "ItemDetail";
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private Text name;
    [SerializeField] private Transform iconTarget;
    [SerializeField] private Text text;
    [SerializeField] private Button closeButton;

    private void Start()
    {
        var data = LocalCacheManager.Load<ShopData>(ItemDetailTag);
        SetData(data);
        closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            transform.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.ShoppingExplanation);
            });
        }).AddTo(this);
    }

    private void SetData(ShopData shopData)
    {
        name.text = shopData.Name;
        text.text = shopData.Text;
        iconTarget.DestroyAllChildrens();
        Instantiate(shopData.GetPrefab(), iconTarget);
    }
}