using Microsoft.Xna.Framework;
using Igdc.Extensions;

namespace Igdc.Scenes
{
    public partial class MapScene
    {
        protected override void Draw(SceneIterationArgument e)
        {
            for (var x = 0; x <= _map.GetUpperBound(0); x++)
                for (var y = 0; y <= _map.GetUpperBound(1); y++)
                {
                    var point = _map[x, y];
                    var pointLocation = new Point(x * 16, y * 16).ApplyCamera(Camera);
                    var source = new Point(point.X * 16, point.Y * 16);
                    e.Batch.Draw(_background,
                        new Rectangle(pointLocation, new Point(16)),
                        new Rectangle(source, new Point(16)),
                        Color.White);
                }

            foreach (var enemy in _enemies)
            {
                var enemyLocation = new Point(enemy.X * enemy.Size.X, enemy.Y * enemy.Size.Y).ApplyCamera(Camera);
                var animationFrame = enemy.GetAnimationFrame();
                var source = new Point(animationFrame.X * enemy.Size.X, animationFrame.Y * enemy.Size.Y);
                e.Batch.Draw(_enemyTextures[enemy.Texture],
                    new Rectangle(enemyLocation, enemy.Size),
                    new Rectangle(source, enemy.Size),
                    Color.White);
            }

            foreach (var ally in _allies)
            {
                var allyLocation = new Point(ally.X * ally.Size.X, ally.Y * ally.Size.Y).ApplyCamera(Camera);
                var animationFrame = ally.GetAnimationFrame();
                var source = new Point(animationFrame.X * ally.Size.X, animationFrame.Y * ally.Size.Y);
                e.Batch.Draw(_allyTextures[ally.Texture],
                    new Rectangle(allyLocation, ally.Size),
                    new Rectangle(source, ally.Size),
                    Color.White);
            }

            DrawPlayer(e);
        }

        private void DrawPlayer(SceneIterationArgument e)
        {
            var playerLocation = _player.Interpolate(new Point(_player.X * _player.Size.X, _player.Y * _player.Size.Y)).ApplyCamera(Camera);
            var animationFrame = _player.GetAnimationFrame();
            var source = new Point(animationFrame.X * _player.Size.X, animationFrame.Y * _player.Size.Y);
            e.Batch.Draw(_playerTexture,
                new Rectangle(playerLocation, _player.Size),
                new Rectangle(source, _player.Size),
                Color.White);
        }
    }
}