using System;
using System.Linq;
using I2.Loc;
using MyGame.Scripts;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace MyGame.Scenes.MiniGame.Scripts
{
    public class ScrollGameWatchVideoDialog : MonoBehaviour
    {
        private static readonly string SuccessCallbackTag = "ScrollGameDialogSuccessCallback";
        private static readonly string CloseCallbackTag = "ScrollGameDialogCloseCallback";
        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 Button closeButton2;
        [SerializeField] private Button movieButton;
        [SerializeField] private GameObject watchMovieObject;
        [SerializeField] private GameObject waitObject;
        
        [SerializeField] private Text playCountText;
        [SerializeField] private Text timeLimitText;

        private void Start()
        {
            var gameData = GameDataManager.GameData;
            var count = Const.ScrollGameDailyPlayMaxCount - gameData.ScrollGameTodayPlayCount;
            var canWatch = count > 0;
            watchMovieObject.SetActive(canWatch);
            waitObject.SetActive(!canWatch);

            playCountText.text = $"{count}";
            var limitTime = gameData.GetScrollGameLimitTime();
            if (limitTime.TotalSeconds > 0)
            {
                timeLimitText.text = String.Format(ScriptLocalization.UI.MissionDailyTimeLimit, limitTime.Hours, limitTime.Minutes, limitTime.Seconds);
            }
            else
            {
                timeLimitText.text = String.Format(ScriptLocalization.UI.MissionDailyTimeLimit, 0, 0, 0);
            }
            
            closeButton.OnClickAsObservable().Merge(closeButton2.OnClickAsObservable()).Take(1).Subscribe(_ =>
            {
                LocalCacheManager.Load<Action>(CloseCallbackTag, null)?.Invoke();
                LocalCacheManager.Remove(CloseCallbackTag);
                transform.parent.SetLocalScale(0);
                backgroundAnimator.SetTrigger(CloseTrigger);
                this.CallWaitForSeconds(.25f, () =>
                {
                    TransitionManager.Instance.UnloadScene(GameScenes.WatchMovieMiniGame);
                });
            }).AddTo(this);
            movieButton.OnClickAsObservable().Take(1).Subscribe(_ =>
            {
                GetRewardDialog.ShowRewardVideo(GameScenes.WatchMovieMiniGame, AdManager.AD_PLACEMENT_SCROLLGAME, () =>
                {
                    LocalCacheManager.Load<Action>(SuccessCallbackTag, null)?.Invoke();
                    LocalCacheManager.Remove(SuccessCallbackTag);
                });
            }).AddTo(this);
        }

        public static void ShowDialog(Action onSuccess = null, Action onClose = null){
            LocalCacheManager.Save(SuccessCallbackTag, onSuccess);
            LocalCacheManager.Save(CloseCallbackTag, onClose);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.WatchMovieMiniGame);
        }
    }
}