using System.Collections.Generic;
using System.Drawing;
using UnityEngine;

namespace Cryville.EEW.Unity.Map.Element {
	class MaskedGroupElement : GroupElement {
		[SerializeField] Material m_maskMaterial;
		[SerializeField] PolygonRenderer m_polygonRendererPrefab;
		readonly List<PolygonRenderer> _polygonRenderers = new();

		public void SetMasks(IEnumerable<IEnumerable<IEnumerable<PointF>>> polygons) {
			foreach (var r in _polygonRenderers) {
				Destroy(r.gameObject);
			}
			_polygonRenderers.Clear();
			foreach (var polygon in polygons) {
				CreatePolygon(polygon);
			}
		}

		void CreatePolygon(IEnumerable<IEnumerable<PointF>> polygon) {
			var polygonRenderer = Instantiate(m_polygonRendererPrefab);
			polygonRenderer.SetPolygon(polygon);
			polygonRenderer.Material = m_maskMaterial;
			polygonRenderer.transform.SetParent(transform, false);
			_polygonRenderers.Add(polygonRenderer);
		}

		Transform _container;
		protected override Transform Container => _container;
		void Awake() {
			_container = new GameObject("_container").transform;
			_container.SetParent(transform, false);
		}
	}
}
