using System.Collections.Generic;
using System.Linq;

namespace Igdc
{
    public class MapDto
    {
        public MapElementDictionary Elements { get; set; } = new MapElementDictionary();

        public struct MapDtoElement
        {
            public int X;
            public int Y;

            public override bool Equals(object obj)
            {
                if (!(obj is MapDtoElement)) return false;
                var dto = (MapDtoElement)obj;
                return X == dto.X && Y == dto.Y;
            }

            public override int GetHashCode() => X ^ Y;
        }

        public class MapElementDictionary : List<KeyValuePair<MapDtoElement, MapDtoElement>>
        {
            public void Add(MapDtoElement key, MapDtoElement value)
            {
                RemoveAll(k => k.Key.Equals(key));
                Add(new KeyValuePair<MapDtoElement, MapDtoElement>(key, value));
            }

            public MapDtoElement this[MapDtoElement key]
            {
                get { return this.FirstOrDefault(p => p.Key.Equals(key)).Value; }
                set { Add(key, value); }
            }

            public bool ContainsKey(MapDtoElement e) => Keys.Contains(e);

            public IEnumerable<MapDtoElement> Keys => this.Select(p => p.Key);

            public IEnumerable<MapDtoElement> Values => this.Select(p => p.Value);
        }
    }
}