using Cryville.EEW.Core;
using System.Collections.Generic;
using UnityEngine;

namespace Cryville.EEW.Unity.UI {
	class EventGroupListView : MonoBehaviour {
		[SerializeField]
		EventGroupView m_prefabEventGroupView;

		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);
			var child = Instantiate(m_prefabEventGroupView);
			child.Set(group);
			child.transform.SetParent(transform, false);
			child.transform.SetSiblingIndex(0);
		}
		void Remove(ReportGroup group) {
			int index = _groups.LastIndexOf(group);
			if (index == -1) return;
			_groups.RemoveAt(index);

			var child = transform.GetChild(_groups.Count - index);
			child.SetParent(null, false);
			Destroy(child.gameObject);
		}
	}
}
