138 lines
4.6 KiB
C#
138 lines
4.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UIElements;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
|
|
class FurnitureFetcher : MonoBehaviour
|
|
{
|
|
public VisualTreeAsset itemTemplate;
|
|
public UIDocument uiDocument;
|
|
|
|
public static string API_URL = "http://100.123.31.103:7223/api/v1/FurnitureVariant/GetAll";
|
|
|
|
private static readonly Dictionary<string, string> CurrencySymbols = new()
|
|
{
|
|
{ "USD", "$" },
|
|
{ "EUR", "€" },
|
|
{ "GBP", "£" },
|
|
{ "JPY", "¥" },
|
|
{ "MXN", "$" },
|
|
{ "CAD", "$" },
|
|
{ "BRL", "R$" },
|
|
{ "ARS", "$" },
|
|
{ "CLP", "$" },
|
|
{ "COP", "$" },
|
|
{ "CNY", "¥" },
|
|
{ "INR", "₹" }
|
|
};
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
StartCoroutine(GetFurnitureData());
|
|
}
|
|
|
|
IEnumerator GetFurnitureData()
|
|
{
|
|
using UnityWebRequest request = UnityWebRequest.Get(API_URL);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
string rawResponse = request.downloadHandler.text;
|
|
//Debug.Log("📦 RAW RESPONSE:\n" + rawResponse);
|
|
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
string wrappedJson = "{\"items\":" + rawResponse + "}";
|
|
FurnitureVariantList list;
|
|
|
|
try
|
|
{
|
|
list = JsonConvert.DeserializeObject<FurnitureVariantList>(wrappedJson);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError("❌ Error al deserializar JSON: " + ex.Message);
|
|
yield break;
|
|
}
|
|
|
|
var root = uiDocument.rootVisualElement;
|
|
var scrollView = root.Q<ScrollView>("furniture-list");
|
|
|
|
scrollView.Clear();
|
|
|
|
foreach (var variant in list.items)
|
|
{
|
|
VisualElement item = itemTemplate.CloneTree();
|
|
item.Q<Label>("nameLabel").text = $"Name: {variant.name}";
|
|
item.Q<Label>("colorLabel").text = $"Color: {variant.color}";
|
|
item.Q<Label>("lineLabel").text = $"Line: {variant.line}";
|
|
|
|
string currencySymbol = CurrencySymbols.TryGetValue(variant.currency, out string symbol)
|
|
? symbol
|
|
: variant.currency;
|
|
|
|
string formattedPrice = $"{currencySymbol}{variant.price.ToString("N2", CultureInfo.InvariantCulture)} {variant.currency}";
|
|
item.Q<Label>("priceLabel").text = $"Price: {formattedPrice}";
|
|
|
|
item.Q<Label>("stockLabel").text = $"Stock: {variant.stock}";
|
|
|
|
var attributesContainer = item.Q<VisualElement>("attributesContainer");
|
|
if (variant.attributes != null)
|
|
{
|
|
string attributesText = "Attributes:\n";
|
|
foreach (var kvp in variant.attributes)
|
|
{
|
|
attributesText += $"{kvp.Key}: {kvp.Value}\n";
|
|
}
|
|
//Debug.Log(attributesText);
|
|
//Debug.Log($"--- ATRIBUTOS PARA: {variant.name} ---");
|
|
//Debug.Log($"Total keys: {variant.attributes.Count}");
|
|
|
|
int index = 0;
|
|
foreach (var kvp in variant.attributes)
|
|
{
|
|
//Debug.Log($"[{index}] {kvp.Key}: {kvp.Value}");
|
|
|
|
string formattedKey = FormatKey(kvp.Key);
|
|
Label attrLabel = new Label($"{formattedKey}: {kvp.Value}");
|
|
attrLabel.AddToClassList("furniture-attribute"); // estilo opcional
|
|
attrLabel.style.whiteSpace = WhiteSpace.Normal;
|
|
attributesContainer.Add(attrLabel);
|
|
|
|
index++;
|
|
}
|
|
|
|
if (index == 0)
|
|
Debug.Log("⚠️ El diccionario está vacío aunque no es null.");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"❌ 'attributes' es NULL para {variant.name}");
|
|
}
|
|
|
|
scrollView.Add(item);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Error: " + request.error);
|
|
}
|
|
}
|
|
|
|
private string FormatKey(string rawKey)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(rawKey))
|
|
return "";
|
|
|
|
// Inserta espacio antes de mayúsculas
|
|
string spaced = System.Text.RegularExpressions.Regex.Replace(rawKey, "([a-z])([A-Z])", "$1 $2");
|
|
|
|
// Capitaliza primera letra
|
|
return char.ToUpper(spaced[0]) + spaced.Substring(1);
|
|
}
|
|
} |