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

namespace MyGame.Scenes.marketing.Scripts
{
    public class ShopCustomizeInfoDialog : MonoBehaviour
    {
        private static readonly string CustomizeDataTag = "ShopCustomizeInfoDialogCallback";
        private static readonly string CloseCallbackTag = "ShopCustomizeInfoDialogCloseCallback";
        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 levelText;
        [SerializeField] private Text pointText;
        [SerializeField] private Text salesBonusText;
        [SerializeField] private Text customerBonusText;
        [SerializeField] private Text adWalkerBonusText;

        private void Start()
        {
            closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
            {
                LocalCacheManager.Load<Action>(CloseCallbackTag, null)?.Invoke();
                LocalCacheManager.Remove(CustomizeDataTag);
                transform.parent.SetLocalScale(0);
                backgroundAnimator.SetTrigger(CloseTrigger);
                this.CallWaitForSeconds(.25f, () =>
                {
                    TransitionManager.Instance.UnloadScene(GameScenes.CustomizationDetails);
                });
            }).AddTo(this);

            var gameData = GameDataManager.GameData;
            levelText.text = $"Lv.{gameData.ShopCustomizeLevel}";
            var customizeLevelList = SpreadsheetDataManager.Instance.GetBaseDataList<ShopCustomizeLevelData>(Const.ShopCustomizeLevelDataSheet);
            if (customizeLevelList.FirstOrDefault(data => data.level == gameData.ShopCustomizeLevel + 1) is ShopCustomizeLevelData customizeLevelData)
            {
                pointText.text = $"{gameData.ShopCustomizePoint}/{customizeLevelData.point}";
            }
            else
            {
                pointText.text = "--";
            }
            var list = ShopCustomize.GetBonusList(gameData.ShopCustomizeLevel);
            var sales = list[ShopCustomizeBonusCategory.Sales].value;
            var customer = list[ShopCustomizeBonusCategory.Customer].bonusLevel;
            var adWalker = list[ShopCustomizeBonusCategory.AdWalker].bonusLevel;
            salesBonusText.text = sales == 0 ? "--" : $"+{sales}";
            customerBonusText.text = customer == 0 ? "--" : $"Lv.{customer}";
            adWalkerBonusText.text = adWalker == 0 ? "--" : $"Lv.{adWalker}";


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