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

namespace Assets.Common
{
    public class Location : IHasMetadata
    {
        public List<MapSite> Sites = new List<MapSite>();

        public IEnumerable<int> Points => Sites.SelectMany(site => site.Boundary);
        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 Point Center => PointUtils.Mean(Sites.Select(s => s.Center));
        
        public LocationType Type;

        public List<Point> DetailedEdge = new List<Point>();

        public void AddSite(MapSite site)
        {
            Sites.Add(site);
            
            FixBoundaryEdges(site);
        }
        
        private void FixBoundaryEdges(MapSite site)
        {
            var a = BoundaryEdges;
            var b = site.Edges.Select(x => x.Item1 < x.Item2 ? x : (x.Item2, x.Item1));

            BoundaryEdges = a.Union(b).Except(a.Intersect(b)).ToList();
        }

        public Metadata Metadata { get; } = new Metadata();
    }
}