using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

using System.Windows.Forms;
using System.Threading;

namespace Slayter
{
 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static int MOVESPERDAY = 15000;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public MouseState oldstate;
        public GameScreen GameScreen;
        public CastleScreen CastleScreen;
        public ScreenOrigin ActiveScreen;
        public ScreenOrigin message;
        public ScreenOrigin savedScreen;
        public Player currentPlayer;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }



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

        protected override void LoadContent()
        {
            this.IsMouseVisible = true;
            message = null;
            graphics.PreferredBackBufferHeight = 650;//520;
            graphics.PreferredBackBufferWidth =640;
            graphics.ApplyChanges();
            
            GameScreen = new GameScreen(this,"MyMap",".\\Content\\giant.csv");
            //GameScreen = new GameScreen(this, "big_background", ".\\Content\\bigger_map_csv.txt");
            //CastleScreen = new CastleScreen(this, "CastleScreen");
            Player player1 = new Player(this);
            currentPlayer = player1;
            //Hero hero = new Hero(GameScreen.Bground,Content.Load<Texture2D>("arab"),this, this.GameScreen, 0, 0, MOVESPERDAY);
            GameScreen.AddPlayer(this,player1);
            //CastleScreen.AddPlayer(player1);
            ActiveScreen = GameScreen;
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                this.Exit();
            }
            MouseState state = Mouse.GetState();
            ActiveScreen.click(this,state);
            if (ActiveScreen == GameScreen)
            {
                GameScreen.updateKeyboard(this, Keyboard.GetState());
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Gray);
            spriteBatch.Begin();
            //if (message == null)
            //{
                ActiveScreen.draw(this, spriteBatch);
            //}
            //else
            //{
                //message.draw(this, spriteBatch);
            //}
            spriteBatch.End();
            base.Draw(gameTime);
        }

        /*public void changeScreen()
        {
            if (ActiveScreen == GameScreen)
            {
                ActiveScreen = CastleScreen;
                CastleScreen.UpdateText();
            }
            else
            {
                ActiveScreen = GameScreen;
                GameScreen.UpdateText();
            }
        }*/

        public void removeMessageScreen(ScreenOrigin scr)
        {
            ActiveScreen = scr.commingFrom;//message=null;
        }

        public void setMessageScreen(ScreenOrigin tempScreen)
        {
            //message = messageScreen;
            savedScreen = ActiveScreen;
            ActiveScreen = tempScreen;
        }

        internal void EndGame()
        {
            throw new NotImplementedException();
        }
    }
}
