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

namespace MyGame.Scripts
{
    public class CoinGetDialog : MonoBehaviour
    {
        private static readonly string CallbackTag = "CoinGetDialogCallback";
        private static readonly string AmountTag = "CoinGetDialogAmount";
        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 Text messageText;

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

            messageText.text = LocalCacheManager.Load<int>(AmountTag, 0).ToString();
        }

        public static void ShowDialog(int amount, Action closeCallback)
        {
            LocalCacheManager.Save(CallbackTag, closeCallback);
            LocalCacheManager.Save(AmountTag, amount);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.WatchMovieCoinGet);
        }
    }
}