using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

namespace Game200
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;

        LevelEditor levelEditor;
        Map map;
        List<Source> sources;
        Button start, youWon, exit, explain;

        enum GameState
        {
            Menu,
            CreateLevel,
            Play,
            Won
        }
        GameState gameState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = C.screenDim.X;
            graphics.PreferredBackBufferHeight = C.screenDim.Y;
            graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

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

        protected override void LoadContent()
        {
            C.Content = Content;
            C.spriteBatch = new SpriteBatch(GraphicsDevice);
            C.graphicsDevice = graphics.GraphicsDevice;

            gameState = GameState.Menu;
            levelEditor = new LevelEditor(C.screenDim.Y / C.length, C.screenDim.X / C.length);
            map = new Map();
            //map = new Map(C.screenDim.Y / C.length, C.screenDim.X / C.length);
            sources = new List<Source>();
            sources.Add(new Source(new Vec(10, 10), new Color(255, 0, 0, 255)));
            start = new Button("start", C.screenDim.X / 2, C.screenDim.Y / 2 - 100);
            youWon = new Button("you_won", C.screenDim.X / 2, C.screenDim.Y / 2);
            exit = new Button("exit", C.screenDim.X / 2, C.screenDim.Y / 2 + 100);
            explain = new Button("explain", C.screenDim.X / 2, C.screenDim.Y / 2);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            switch (gameState)
            {
                case GameState.Menu:
                    start.Update();
                    exit.Update();
                    if (start.isPressed)
                        gameState = GameState.CreateLevel;
                    if (exit.isPressed)
                        Exit();
                    break;
                case GameState.CreateLevel:
                    levelEditor.Update(elapsed);
                    if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                    {
                        levelEditor.Export(map);
                        gameState = GameState.Play;
                        IsMouseVisible = false;
                    }
                    break;
                case GameState.Play:
                    map.Update();
                    foreach (Source s in sources)
                    {
                        s.Update();
                        s.ShedLight(map);
                    }

                    if (sources[sources.Count - 1].isFixed)
                    {
                        map.ShineRoom(sources[sources.Count - 1]);
                        if (map.IsDone())
                        {
                            IsMouseVisible = true;
                            gameState = GameState.Won;
                        }
                        sources.Add(new Source(C.MousePos(), new Color((sources.Count % 3 == 0) ? 255 : 0, (sources.Count % 3 == 1) ? 255 : 0, (sources.Count % 3 == 2) ? 255 : 0, 255)));
                    }
                    break;
                case GameState.Won:
                    youWon.Update();
                    if (youWon.isPressed)
                        gameState = GameState.Menu;
                    break;
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            switch (gameState)
            {
                case GameState.Menu:
                    GraphicsDevice.Clear(Color.CornflowerBlue);
                    C.spriteBatch.Begin();
                    start.Draw();
                    exit.Draw();
                    C.spriteBatch.End();
                    break;
                case GameState.CreateLevel:
                    GraphicsDevice.Clear(Color.CornflowerBlue);
                    C.spriteBatch.Begin();
                    levelEditor.Draw();
                    C.spriteBatch.End();
                    //explain.Draw();
                    break;
                case GameState.Play:
                    GraphicsDevice.Clear(Color.Black);
                    C.spriteBatch.Begin();
                    map.DrawBackground();
                    foreach (Source s in sources)
                        s.DrawRays();
                    foreach (Source s in sources)
                        s.Draw();
                    map.Draw();
                    C.spriteBatch.End();
                    break;
                case GameState.Won:
                    GraphicsDevice.Clear(Color.CornflowerBlue);
                    C.spriteBatch.Begin();
                    youWon.Draw();
                    C.spriteBatch.End();
                    break;
            }
            
            base.Draw(gameTime);
        }
    }
}
