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

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

        private Texture2D _background;
        private Texture2D _overlay;
        private Texture2D _healthBarElement;
        private Texture2D _playerTexture;
        private Dictionary<int, Texture2D> _enemyTextures;
        private Texture2D _projectile;

        private BattlePlayer _player;
        private List<BattleEnemy> _enemies;
        private List<BattleBullet> _bullets;
        private bool[,] _pressMatrix;
        private bool _pressMark;

        private Action _winCallback { get; set; }
        private Action _loseCallback { get; set; }

        public BattleScene(GraphicsDeviceManager manager, GraphicsDevice device, BattlePackage package) : base(manager, device, package)
        {
            _flat = IgdcGame.Current.FlatTexture;
            _font = IgdcGame.Current.BasicFont;

            _background = device.ReadTexture2D($"BattleBackground{package.Background}");
            _overlay = device.ReadTexture2D($"BattleOverlay{package.Background}");
            _playerTexture = device.ReadTexture2D("BattlePlayer");
            _healthBarElement = device.ReadTexture2D("HealthBarElement");
            _projectile = device.ReadTexture2D("Projectile");

            _player = package.Player;
            _enemies = new List<BattleEnemy>(package.Enemies);

            _enemyTextures = new Dictionary<int, Texture2D>();
            foreach (var enemy in _enemies)
            {
                if (!_enemyTextures.ContainsKey(enemy.Texture))
                    _enemyTextures.Add(enemy.Texture, device.ReadTexture2D($"BattleEnemy{enemy.Texture}"));
            }

            _bullets = new List<BattleBullet>();
            _pressMatrix = new bool[3, 2];

            _winCallback = package.WinCallback;
            _loseCallback = package.LoseCallback;
        }

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