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

namespace MyGame.Scenes.marketing.Scripts
{
    public class ShopCustomizePurchaseDialog : MonoBehaviour
    {
        private static readonly string CustomizeDataTag = "ShopCustomizePurchaseDialogData";
        private static readonly string DetailModeTag = "ShopCustomizePurchaseDialogDetailMode";
        private static readonly string PurchaseCallbackTag = "ShopCustomizePurchaseDialogCallback";
        private static readonly string CloseCallbackTag = "ShopCustomizePurchaseDialogCloseCallback";
        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 ButtonOnOff purchaseButton;
        [SerializeField] private Text nameText;
        [SerializeField] private Text textText;
        [SerializeField] private Text priceText;
        [SerializeField] private Transform imageTransform;
        [SerializeField] private Transform detailImageTransform;
        [SerializeField] private GameObject purchaseObject;
        [SerializeField] private GameObject detailObject;

        private void Start()
        {
            var customizeData = LocalCacheManager.Load<ShopCustomizeData>(CustomizeDataTag);
            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.CustomizationPurchase);
                });
            }).AddTo(this);
            purchaseButton.OnClickObservable.Take(1).Subscribe(_ =>
            {
                LocalCacheManager.Load<Action>(PurchaseCallbackTag, null)?.Invoke();
                LocalCacheManager.Remove(PurchaseCallbackTag);
                TransitionManager.Instance.UnloadScene(GameScenes.CustomizationPurchase);
            }).AddTo(this);
            
            var detailMode = LocalCacheManager.Load<bool>(DetailModeTag);
            purchaseObject.SetActive(!detailMode);
            detailObject.SetActive(detailMode);
            purchaseButton.SetActive(GameDataManager.GameData.ShopCustomizeCoin >= customizeData.price);
            nameText.text = customizeData.Name;
            textText.text = customizeData.Text;
            priceText.text = customizeData.price.ToString();
            imageTransform.DestroyAllChildrens();
            Instantiate(customizeData.GetIconPrefab(), imageTransform);
            detailImageTransform.DestroyAllChildrens();
            Instantiate(customizeData.GetIconPrefab(), detailImageTransform);
        }
        
        public static void ShowDialog(ShopCustomizeData customizeData, bool detailMode, Action onPurchase = null, Action onClose = null){
            LocalCacheManager.Save(CustomizeDataTag, customizeData);
            LocalCacheManager.Save(DetailModeTag, detailMode);
            LocalCacheManager.Save(PurchaseCallbackTag, onPurchase);
            LocalCacheManager.Save(CloseCallbackTag, onClose);
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.CustomizationPurchase);
        }
    }
}