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

namespace KitsuneCafe.UI
{
  public class TestRecycleView : MonoBehaviour
  {
    [SerializeField]
    private UIDocument doc;

    private ListView listView;

    private void OnValidate()
    {
      if (doc == null)
      {
        doc = GetComponent<UIDocument>();
      }
    }

    private void Awake()
    {
      listView = doc.rootVisualElement.Q<ListView>();
      listView.Q<ScrollView>().verticalScrollerVisibility = ScrollerVisibility.Hidden;
      listView.makeItem = () => new Label();
      listView.bindItem = (visualElement, index) =>
      {
        Label label = visualElement as Label;
        label.text = listView.itemsSource[index].ToString();
      };

    }

    private void Start()
    {
      var list = new CycleList<string>() { "one", "two", "three" };
      listView.itemsSource = list;
      listView.RefreshItems();
    }

    private void OnEnable()
    {
      listView.itemsSourceChanged += ScrollToMiddle;
    }

    private void OnDisable()
    {
      listView.itemsSourceChanged -= ScrollToMiddle;
    }

    private void ScrollToMiddle()
    {
      listView.ScrollToItem(listView.itemsSource.Count / 2);
    }
  }
}
