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().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);
    }
}