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

namespace Assets.Cities
{
    public class City
    {
        public List<MapSite> Sites { get; set; } = new List<MapSite>();

        public Graph<Point> Roads { get; set; } = new Graph<Point>();
        public Graph<Point> FieldBoundaries { get; set; } = new Graph<Point>();

        public List<CityField> Fields { get; set; } = new List<CityField>();

        public List<(int, int)> Edges = new List<(int, int)>();

        public City()
        {
        }
        
        public City(List<MapSite> sites) 
        {
            Sites = sites;
        }

        public void AddSite(MapSite site)
        {
            Sites.Add(site);
            FixBoundaryEdges(site);
        }

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

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