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

namespace MyGame.Scenes.Main.Scripts
{
    public class UpdateDialog : MonoBehaviour
    {
        private static readonly string CallbackTag = "UpdateDialogCallback";
        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 updateMessageText;

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

            updateMessageText.text = string.Format(ScriptLocalization.System.LevelUpdateComingSoon, Const.MaxShopLevel + 1);
        }
        
        public static void ShowDialog(Action onClose = null){
            LocalCacheManager.Save(CallbackTag, onClose);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.Update);
        }
    }
}