using System.Collections.Generic;
using KitsuneCafe.UI;
using UnityEngine;
using UnityEngine.UIElements;

public class UserProfile
{
    public string Name { get; set; }
    public string Status { get; set; }
    public Color StatusColor { get; set; }

    public UserProfile(string name, string status, Color statusColor)
    {
        Name = name;
        Status = status;
        StatusColor = statusColor;
    }
}

public static class UserProfileItemBinder
{
    public static void Bind(VisualElement root, UserProfile profile)
    {
        var nameLabel = root.Q<Label>("user-name");
        var statusLabel = root.Q<Label>("user-status");

        if (nameLabel != null)
        {
            nameLabel.text = profile.Name;
        }

        if (statusLabel != null)
        {
            statusLabel.text = profile.Status;
            statusLabel.style.color = new StyleColor(profile.StatusColor);
        }
    }
}

public class UserProfileDataSource : ICollectionDataSource
{
    public int Length => profiles.Count;

    private readonly List<UserProfile> profiles;
    private readonly VisualTreeAsset itemUxml;

    public UserProfileDataSource(List<UserProfile> profiles)
    {
        this.profiles = profiles;

        itemUxml = Resources.Load<VisualTreeAsset>(UserProfileItemVisualElement.UxmlPath);
        if (itemUxml == null)
        {
            Debug.LogError($"Could not find UXML asset at path: {UserProfileItemVisualElement.UxmlPath}");
        }
    }

    public VisualElement CreateItem()
    {
        var root = itemUxml.Instantiate();
        var item = new UserProfileItemVisualElement();
        item.Add(root);
        return item;
    }

    public void BindItem(VisualElement element, int index)
    {
        UserProfileItemBinder.Bind(element, profiles[index]);
    }

    public void UnbindItem(VisualElement element, int index)
    {
    }
}
