using System;
using System.Linq;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class ShopLevelUp : MonoBehaviour
{
    public static readonly string DataTag = "ShopLevelUpData";
    public static readonly string CallbackTag = "CallbackData";
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private TextMeshProUGUI shopLevelText;
    [SerializeField] private Text achievementText;
    [SerializeField] private Text recipeNameText;
    [SerializeField] private Button closeButton;
    [SerializeField] private GameObject summaryObject;

    private void Start()
    {
        var shopLevel = LocalCacheManager.Load<int>(DataTag);
        var scriptList = SpreadsheetDataManager.Instance.GetBaseDataList<BrotherScriptData>(Const.ScriptDataSheet);
        var targetScriptList = scriptList.Where(data => data.id == shopLevel).ToList();
        closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            transform.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.StoreLevel);
                if (targetScriptList.Count > 0)
                {
                    LocalCacheManager.Save(BrotherConversation.ScriptDataTag, targetScriptList);
                    LocalCacheManager.Save(BrotherConversation.CallbackTag, new Action(() =>
                    {
                        GameDataManager.GameData.ViewedShopLevel = shopLevel;
                        GameDataManager.GameData.OrderConditionCount = 0;
                        GameDataManager.SaveGameData();
                        HeartMeter.Instance.SetShopLevel(GameDataManager.GameData.ViewedShopLevel, true);
                        LocalCacheManager.Load<Action>(CallbackTag, null)?.Invoke();
                        LocalCacheManager.Remove(CallbackTag);
                    }));
                    TransitionManager.Instance.LoadSceneAdditive(GameScenes.Conversation);
                }
                else
                {
                    GameDataManager.GameData.ViewedShopLevel = shopLevel;
                    GameDataManager.GameData.OrderConditionCount = 0;
                    GameDataManager.SaveGameData();
                    HeartMeter.Instance.SetShopLevel(GameDataManager.GameData.ViewedShopLevel, true);
                    LocalCacheManager.Load<Action>(CallbackTag, null)?.Invoke();
                    LocalCacheManager.Remove(CallbackTag);
                }
            });
        }).AddTo(this);

        var productList = SpreadsheetDataManager.Instance.GetBaseDataList<ProductData>(Const.ProductDataSheet);
        var shopLevelList = SpreadsheetDataManager.Instance.GetBaseDataList<ShopLevelData>(Const.ShopLevelDataSheet);
        shopLevelText.text = shopLevel.ToString();
        achievementText.text = shopLevelList.First(data => data.shopLevel == shopLevel).achievement;
        recipeNameText.text = productList.First(data => data.shopLevel == shopLevel).name;

        summaryObject.SetActive(false);
        this.CallWaitForSeconds(1f, () =>
        {
            summaryObject.SetActive(true);
        });
    }
}
