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

public class TankDetailView : MonoBehaviour
{
    public static readonly string TankDetailDataTag = "TankDetailData";
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private Text tankNumber;
    [SerializeField] private Text totalStock;
    [SerializeField] private Text tankStock;
    [SerializeField] private Text price;
    [SerializeField] private Text name;
    [SerializeField] private Text text;
    [SerializeField] private Transform popcornImageTarget;
    [SerializeField] private RaritySummaryView raritySummaryView;
    [SerializeField] private Button closeButton;

    private void Start()
    {
        var data = LocalCacheManager.Load<(int tankNum, int totalStock, StorageTank tankData, ProductData productData, List<(ProductRarity rarity, int price, int stock)> raritySummaryData)>(TankDetailDataTag);
        closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            transform.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.PopcornDescription);
            });
        }).AddTo(this);
        
        tankNumber.text = $"{data.tankNum}";
        totalStock.text = $"{ScriptLocalization.UI.RecipeChoiceMyStockPrefix}{data.totalStock}";
        tankStock.text = string.Format(ScriptLocalization.UI.ManagementTankTankNumber, data.tankNum, data.tankData.Stock);
        price.text = $"{data.productData.price}";
        name.text = $"{data.productData.Name}";
        text.text = data.productData.Text;
        popcornImageTarget.DestroyAllChildrens();
        Instantiate(data.productData.GetIconPrefab(), popcornImageTarget);
        raritySummaryView.SetHasRarity(data.raritySummaryData);
    }
}
