using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public sealed class EquipmentListPanel : MonoBehaviour
{
    [SerializeField]
    private GameObject NoItemsLabel = null;
    [SerializeField]
    private BuyEquipmentButton[] buttons = null;

    public void Show(EquipmentBlueprintParams[] equipmentList = null, int selectedId = 0, Action<int> callback = null)
    {
        if (equipmentList == null || equipmentList.Length == 0) {
            NoItemsLabel.SetActive(true);
            foreach (var button in buttons) {
                button.gameObject.SetActive(false);
            }
            return;
        }
        NoItemsLabel.SetActive(false);
        for (int i = 0; i < buttons.Length; i++) {
            if (i < equipmentList.Length)
            {
                var item = equipmentList[i];
                var id = i;
                Action action = () => { callback?.Invoke(id); };
                buttons[i].gameObject.SetActive(true);
                buttons[i].Show(item.BuyCost, item.Id, MergeParams(item.Params), i != selectedId ? action : null);
            }
            else
            {
                buttons[i].gameObject.SetActive(false);
            }
        }
    }

    private string MergeParams(Dictionary<string, int> parameters)
    {
        if (parameters == null)
            return string.Empty;
        return string.Join(", ", parameters.Select(param => $"{param.Key}: {param.Value}"));
    }
}
