using Microsoft.Xna.Framework;
using MonoGame.FourEngine.Entities;
using MonoGame.FourEngine.Interfaces;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

namespace MonoGame.FourEngine.Tiled
{
    internal class ObjectLayer : LayerBase
    {
        public List<TiledObject> Objects { get; private set; }
        public Point Offset { get; private set; }

        public new static ObjectLayer Parse(XElement xElement)
        {
            var xObjects = xElement.Descendants("object");
            var objects = xObjects.Select(TiledObject.Parse).ToList();
            var offset = new Point(
                int.Parse(xElement.AttributeOrZero("offsetx").Value, CultureInfo.InvariantCulture),
                int.Parse(xElement.AttributeOrZero("offsety").Value, CultureInfo.InvariantCulture));
            return new ObjectLayer
            {
                Objects = objects,
                Offset = offset
            };
        }

        public new static IEnumerable<ObjectLayer> Parse(IEnumerable<XElement> xElements)
            => xElements.Select(x => Parse(x));

        public override IEnumerable<Entity> GetEntities(List<Tileset> tilesets, int layer, MapOrientation orientation = MapOrientation.Orthogonal)
        {
            foreach (var item in Objects)
            {
                if (!ServiceLocator.Types.TryGetValue(item.Type, out var type))
                {
                    item.Position += Offset;
                    yield return item;
                    continue;
                }
                var @object = Activator.CreateInstance(type.AsType());
                var entity = @object as Entity;
                if (entity == null)
                {
                    yield return null;
                    continue;
                }
                
                switch (entity)
                {
                    case BasicEntity basicEntity:
                        FillBasicEntity(basicEntity, item);
                        break;
                    case IHaveSizeAndPosition entityWithSizeAndPosition:
                        FillEntityWithSizeAndPosition(entityWithSizeAndPosition, item);
                        break;
                }
                FillExtraProperties(entity, item);
                yield return entity;
            }
        }

        private void FillBasicEntity(BasicEntity basicEntity, TiledObject item)
        {
            basicEntity.Position = item.Position + Offset;
        }

        private void FillEntityWithSizeAndPosition(IHaveSizeAndPosition entityWithSizeAndPosition, TiledObject item)
        {
            entityWithSizeAndPosition.Position = item.Position + Offset;
            entityWithSizeAndPosition.Width = item.Width;
            entityWithSizeAndPosition.Height = item.Height;
        }

        private void FillExtraProperties(Entity entity, TiledObject item)
        {
            var type = entity.GetType();
            foreach (var property in item.IntProperties)
            {
                var prop = type.GetRuntimeProperty(property.Key);
                prop?.SetValue(entity, property.Value);
            }
            foreach (var property in item.StringProperties)
            {
                var prop = type.GetRuntimeProperty(property.Key);
                prop?.SetValue(entity, property.Value);
            }
        }
    }
}