using System;
using I2.Loc;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace MyGame.Scenes.marketing.Scripts
{
    public class ShopCustomizeTutorialDialog : MonoBehaviour
    {
        private static readonly string CallbackTag = "ShopCustomizeTutorialDialogCallback";
        private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
        private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");

        [SerializeField] private Animator backgroundAnimator;
        [SerializeField] private Button closeButton;

        private void Start()
        {
            closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
            {
                LocalCacheManager.Load<Action>(CallbackTag, null)?.Invoke();
                LocalCacheManager.Remove(CallbackTag);
                transform.parent.SetLocalScale(0);
                backgroundAnimator.SetTrigger(CloseTrigger);
                this.CallWaitForSeconds(.25f, () =>
                {
                    TransitionManager.Instance.UnloadScene(GameScenes.CustomizeExplanation);
                });
            }).AddTo(this);
        }
        
        public static void ShowDialog(Action onClose = null){
            LocalCacheManager.Save(CallbackTag, onClose);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.CustomizeExplanation);
        }
    }
}