using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using WindowsGame1.Screen;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1.Scene
{
    public class GameSceneManager : DrawableGameComponent
    {
        private Stack<GameScene> SceneStack = new Stack<GameScene>();
        private GameScene NowGameScene;

        public SpriteBatch Sprite;

        public GameSceneManager(Game game)
            : base(game)
        {
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (SceneStack.Count == 0)
            {
                Game.Exit();
            }
            base.Update(gameTime);
        }

        public void AddScene(GameScene scene)
        {
            scene.Sprite = Sprite;
            SceneStack.Push(scene);
            scene.Initialize();
            Game.Components.Add(scene);
            Game.Components.Remove(NowGameScene);
            NowGameScene = scene;
        }

        public void RemoveScene()
        {
            Game.Components.Remove(SceneStack.Pop());
            NowGameScene = SceneStack.Peek();
            Game.Components.Add(NowGameScene);
        }

        public void Back(string name)
        {
            while (SceneStack.Count > 0)
            {
                if (SceneStack.Peek().Name.Equals(name))
                {
                    Game.Components.Remove(NowGameScene);
                    NowGameScene = SceneStack.Peek();
                    Game.Components.Add(NowGameScene);
                    break;
                }
                SceneStack.Pop();
            }
        }
    }
}
