﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Diagnostics;

namespace ba42.Core {
    public abstract class Scene {

        public string name;
        public Color clearingColor;
        public GraphicsDeviceManager graphics;
        public ContentManager contentManager;
        public SpriteBatch spriteBatch;
        public Dictionary<string, Entity> entities;

        public Scene() {
            clearingColor = Color.Black;
            entities = new Dictionary<string, Entity>();
        }

        public virtual void Initialize() {
            foreach (var e in entities) {
                e.Value.Initialize();
            }
        }

        public void LoadContent() {
            foreach (var e in entities) {
                e.Value.LoadContent();
            }
        }

        public T AddEntity<T>(string name, Vector2 position = new Vector2()) where T : Entity, new() {
            Debug.WriteLine("Adding entity: " + name);
            T newEntity = new T() {
                name = name,
                parentScene = this,
                contentManager = contentManager,
                transform = new Transform()
            };
            newEntity.transform.position = position;
            entities.Add(name, newEntity);
            return newEntity;
        }

        public void removeEntity(string name) {
            Debug.WriteLine("Removing entity: " + name);
            entities.Remove(name);
        }

        // Problably shouldn't need to be overriden, but leaving virtual for the time being
        public virtual void Update(GameTime gameTime) {
            foreach(var e in entities) {
                e.Value.Update(gameTime);
            }
        }

        public virtual void Draw(GameTime gameTime) {
            foreach (var e in entities) {
                e.Value.Draw(gameTime);
            }
        }
    }
}
