using Cryville.Common.Unity.UI;
using Cryville.EEW.Core;
using System.Collections.Generic;
using UnityEngine;

namespace Cryville.EEW.Unity.UI {
	[RequireComponent(typeof(RecyclerView))]
	class EventGroupListView : MonoBehaviour {
		RecyclerView _recyclerView;
		void Awake() {
			_recyclerView = GetComponent<RecyclerView>();
			_recyclerView.LoadItem = LoadItem;
		}
		void LoadItem(int index, GameObject gameObject) {
			gameObject.GetComponent<EventGroupView>().Set(_groups[^(index + 1)]);
		}

		readonly List<ReportGroup> _groups = new();
		public void UpdateGroup(ReportGroup e) {
			Remove(e);
			Add(e);
		}
		public void RemoveGroup(ReportGroup e) {
			Remove(e);
		}
		void Add(ReportGroup group) {
			_groups.Add(group);
			_recyclerView.ItemCount++;
			_recyclerView.InvalidateAll();
		}
		void Remove(ReportGroup group) {
			int index = _groups.LastIndexOf(group);
			if (index == -1) return;
			_groups.RemoveAt(index);

			--_recyclerView.ItemCount;
			_recyclerView.InvalidateAll();
		}
	}
}
