using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Igdc.Fonts;
using Igdc.Drawables;
using System.Collections.Generic;

namespace Igdc.Scenes
{
    public partial class MapScene : SceneBase<object>
    {
        private Texture2D _flat;
        private FontBase _font;

        private Texture2D _background;
        private Texture2D _playerTexture;
        private Dictionary<int, Texture2D> _enemyTextures;
        private Dictionary<int, Texture2D> _allyTextures;

        private Point[,] _map;
        private MapPlayer _player;
        private List<MapEnemy> _enemies;
        private List<MapAlly> _allies;
        private Point Camera;

        private MapPackage _package;

        public override int ButtonTimeout => 3;

        public MapScene(GraphicsDeviceManager manager, GraphicsDevice device, MapPackage package) : base(manager, device, package)
        {
            _package = package;

            _flat = IgdcGame.Current.FlatTexture;
            _font = IgdcGame.Current.BasicFont;
            _background = device.ReadTexture2D($"MapBackground{package.Background}");
            _playerTexture = device.ReadTexture2D("MapPlayer");
            _map = package.Map ?? device.ReadMap($"Map{package.MapLink}");
            _player = package.Player;
            
            _enemies = new List<MapEnemy>(package.Enemies ?? new MapEnemy[0]);
            _enemyTextures = new Dictionary<int, Texture2D>();
            foreach (var enemy in _enemies)
            {
                if (!_enemyTextures.ContainsKey(enemy.Texture))
                    _enemyTextures.Add(enemy.Texture, device.ReadTexture2D($"MapEnemy{enemy.Texture}"));
            }

            _allies = new List<MapAlly>(package.Allies ?? new MapAlly[0]);
            _allyTextures = new Dictionary<int, Texture2D>();
            foreach (var ally in _allies)
            {
                if (!_allyTextures.ContainsKey(ally.Texture))
                    _allyTextures.Add(ally.Texture, device.ReadTexture2D($"MapAlly{ally.Texture}"));
            }
        }

        public override void Dispose()
        {
            _background?.Dispose();
            _playerTexture?.Dispose();
            foreach (var texture in _enemyTextures.Values)
                texture?.Dispose();
        }
    }
}