using System;
using System.Collections.Generic;
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");
    private static readonly string totalStockFormat = "所持数：{0}";
    private static readonly string tankStockFormat = "タンク{0}：{1}";
    
    [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().Subscribe(_ =>
        {
            transform.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.PopcornDescription);
            });
        }).AddTo(this);
        
        tankNumber.text = $"{data.tankNum}";
        totalStock.text = string.Format(totalStockFormat, data.totalStock);
        tankStock.text = string.Format(tankStockFormat, 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);
    }
}
