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

namespace MyGame.Scripts
{
    public class GetRewardDialog : MonoBehaviour
    {
        private enum RewardType
        {
            Coin,
            CornField,
            IncreaseCustomer,
            PerfectPopcorn
        }
        private static readonly string OkCallbackTag = "GetRewardDialogOkCallbackTag";
        private static readonly string RewardTypeTag = "GetRewardDialogRewardTypeTag";
        private static readonly string coinMessage = "動画を見て兄弟を休憩させると\nコインがもらえます！\n動画を見ますか？";
        private static readonly string cornFieldMessage = "肥料を使うと一定時間\nコーンの成長を早めることができます\n動画を見て肥料を使いますか？";
        private static readonly string increaseCustomerMessage = "動画を見てお店を宣伝しますか？";
        private static readonly string perfectPopcornMessage = "動画を見て大成功にしますか？";
        
        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 okButton;
        [SerializeField] private Text messageText;
        [SerializeField] private Button narrowCloseButton;
        [SerializeField] private Button narrowOkButton;
        [SerializeField] private Text narrowMessageText;
        [SerializeField] private GameObject normalWindow;
        [SerializeField] private GameObject narrowWindow;
        [SerializeField] private GameObject coinObject;
        [SerializeField] private GameObject fertilizerObject;

        private void Start()
        {
            switch (LocalCacheManager.Load<RewardType>(RewardTypeTag))
            {
                case RewardType.Coin:
                    normalWindow.SetActive(true);
                    narrowWindow.SetActive(!normalWindow.activeSelf);
                    coinObject.SetActive(true);
                    fertilizerObject.SetActive(!coinObject.activeSelf);
                    messageText.text = coinMessage;
                    SetButtonAction(closeButton, okButton);
                    break;
                case RewardType.CornField:
                    normalWindow.SetActive(true);
                    narrowWindow.SetActive(!normalWindow.activeSelf);
                    coinObject.SetActive(false);
                    fertilizerObject.SetActive(!coinObject.activeSelf);
                    messageText.text = cornFieldMessage;
                    SetButtonAction(closeButton, okButton);
                    break;
                case RewardType.IncreaseCustomer:
                    normalWindow.transform.SetLocalScale(0);
                    narrowWindow.SetActive(true);
                    narrowMessageText.text = increaseCustomerMessage;
                    SetButtonAction(narrowCloseButton, narrowOkButton);
                    break;
                case RewardType.PerfectPopcorn:
                    normalWindow.transform.SetLocalScale(0);
                    narrowWindow.SetActive(true);
                    narrowMessageText.text = perfectPopcornMessage;
                    SetButtonAction(narrowCloseButton, narrowOkButton);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void SetButtonAction(Button closeButton, Button okButton)
        {
            closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
            {
                transform.parent.SetLocalScale(0);
                backgroundAnimator.SetTrigger(CloseTrigger);
                this.CallWaitForSeconds(.25f, () =>
                {
                    TransitionManager.Instance.UnloadScene(GameScenes.WatchMovieCoin);
                });
            }).AddTo(this);
            okButton.OnClickAsObservable().Take(1).Subscribe(_ =>
            {
                if (AdManager.Instance.IsLoadedRewardVideo())
                {
                    AdManager.Instance.ShowRewardVideo(result =>
                    {
                        if (result)
                        {
                            LocalCacheManager.Load<Action>(OkCallbackTag, null)?.Invoke();
                            LocalCacheManager.Remove(OkCallbackTag);
                        }
                        TransitionManager.Instance.UnloadScene(GameScenes.WatchMovieCoin);
                    });
                }
                else
                {
                    TransitionManager.Instance.LoadSceneAdditive(GameScenes.WatchMovieFailure);
                    TransitionManager.Instance.UnloadScene(GameScenes.WatchMovieCoin);
                }
            }).AddTo(this);
        }

        private static void ShowDialog(Action okCallback, RewardType rewardType)
        {
            LocalCacheManager.Save(OkCallbackTag, okCallback);
            LocalCacheManager.Save(RewardTypeTag, rewardType);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.WatchMovieCoin);
        }

        public static void ShowCoinDialog(Action okCallback)
        {
            ShowDialog(okCallback, RewardType.Coin);
        }
        
        public static void ShowCornFieldDialog(Action okCallback)
        {
            ShowDialog(okCallback, RewardType.CornField);
        }
        
        public static void ShowIncreaseCustomerDialog(Action okCallback)
        {
            ShowDialog(okCallback, RewardType.IncreaseCustomer);
        }
        
        public static void ShowPerfectPopcornDialog(Action okCallback)
        {
            ShowDialog(okCallback, RewardType.PerfectPopcorn);
        }
    }
}