using System.Collections.Generic;
using System.Linq;
using Assets.Voronoi;

namespace Assets.Map
{
    public class Location
    {
        public List<LocationSite> Sites = new List<LocationSite>();

        public IEnumerable<int> Points => Sites.SelectMany(site => site.Site.Vertices);
        public IEnumerable<int> BoundaryPoints => BoundaryEdges.SelectMany(edge => new [] { edge.Item1, edge.Item2 }).Distinct();
        public IEnumerable<int> InsidePoints => Points.Except(BoundaryPoints);
        
        public List<(int, int)> BoundaryEdges = new List<(int, int)>();
        
        public LocationType Type;

        public void AddSite(LocationSite site)
        {
            site.Location = this;
            Sites.Add(site);
            
            FixBoundaryEdges(site);
        }
        
        private void FixBoundaryEdges(LocationSite site)
        {
            var a = BoundaryEdges;
            var b = site.Site.Edges;

            BoundaryEdges = a.Union(b).Except(a.Intersect(b)).ToList();
        }
    }
    
    public class LocationSite
    {
        public readonly Site Site;
        public Location Location;

        public LocationSite(Site site)
        {
            Site = site;
        }
    }
}