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

namespace MyGame.Scripts
{
    public class GetRewardDialog : MonoBehaviour
    {
        private enum RewardType
        {
            Coin,
            CornField,
            IncreaseCustomer,
            PerfectPopcorn,
            VipCustomer
        }
        private static readonly string OkCallbackTag = "GetRewardDialogOkCallbackTag";
        private static readonly string CloseCallbackTag = "GetRewardDialogCloseCallbackTag";
        private static readonly string RewardTypeTag = "GetRewardDialogRewardTypeTag";

        private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
        private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");

        [Space]
        [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 movieObject;
        [SerializeField] private GameObject coinObject;
        [SerializeField] private GameObject fertilizerObject;
        [SerializeField] private GameObject publicityObject;
        [SerializeField] private GameObject vipObject;

        private void Start()
        {
            narrowWindow.SetActive(false);
            movieObject.SetActive(false);
            coinObject.SetActive(false);
            fertilizerObject.SetActive(false);
            publicityObject.SetActive(false);
            vipObject.SetActive(false);
            switch (LocalCacheManager.Load<RewardType>(RewardTypeTag))
            {
                case RewardType.Coin:
                    normalWindow.SetActive(true);
                    movieObject.SetActive(true);
                    coinObject.SetActive(true);
                    messageText.text = ScriptLocalization.System.CoinMainMessage;
                    SetButtonAction(closeButton, okButton, AdManager.AD_PLACEMENT_COIN);
                    break;
                case RewardType.CornField:
                    normalWindow.SetActive(true);
                    movieObject.SetActive(true);
                    fertilizerObject.SetActive(true);
                    messageText.text = ScriptLocalization.System.CornFieldMessage;
                    SetButtonAction(closeButton, okButton, AdManager.AD_PLACEMENT_CORNFIELD);
                    break;
                case RewardType.IncreaseCustomer:
                    normalWindow.SetActive(true);
                    movieObject.SetActive(true);
                    publicityObject.SetActive(true);
                    messageText.text = ScriptLocalization.System.IncreaseCustomerMessage;
                    SetButtonAction(closeButton, okButton, AdManager.AD_PLACEMENT_CUSTOMER);
                    break;
                case RewardType.PerfectPopcorn:
                    normalWindow.SetActive(true);
                    movieObject.SetActive(true);
                    messageText.text = ScriptLocalization.System.PerfectPopcornMessage;
                    SetButtonAction(closeButton, okButton, AdManager.AD_PLACEMENT_PERFECT);
                    break;
                case RewardType.VipCustomer:
                    normalWindow.SetActive(true);
                    vipObject.SetActive(true);
                    messageText.text = ScriptLocalization.System.VipCustomerMessage;
                    SetButtonAction(closeButton, okButton, AdManager.AD_PLACEMENT_VIP);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

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

        public static void ShowRewardVideo(GameScenes unloadScene, string placement, Action successCallback)
        {
            if (AdManager.Instance.IsLoadedRewardVideo())
            {
                AdManager.Instance.ShowRewardVideo(result =>
                {
                    if (result)
                    {
                        successCallback?.Invoke();
                        GameDataManager.GameData.AddAdCount();
                    }
                    TransitionManager.Instance.UnloadScene(unloadScene);
                }, placement);
            }
            else
            {
                TransitionManager.Instance.LoadSceneAdditive(GameScenes.WatchMovieFailure);
                TransitionManager.Instance.UnloadScene(unloadScene);
            }
        }

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

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