using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LD20_Game
{
    class GlenGamesScene :BaseScene
    {
        const int BarWidth = 22;
        const int BarHeight = 3;

        private TimeSpan timeOver = TimeSpan.MinValue;
        public GlenGamesScene() { }

        TimeSpan sceneStartedAt = TimeSpan.MinValue;
        TimeSpan startBlackout = TimeSpan.FromSeconds(0.25);
        TimeSpan endBlackout = TimeSpan.FromSeconds(1.3);

        public override void InitalizeScene()
        {
            
        }

        public override void UpdateScene(GameTime gameTime)
        {
            if (sceneStartedAt == TimeSpan.MinValue)
            {
                sceneStartedAt = gameTime.TotalGameTime;
                startBlackout += sceneStartedAt;
                endBlackout += sceneStartedAt;
            }

            if (timeOver == TimeSpan.MinValue)
                timeOver = gameTime.TotalGameTime + TimeSpan.FromSeconds(3);

            if (timeOver < gameTime.TotalGameTime)
                SceneManager.SwitchTo(new TitleScene());

            base.UpdateScene(gameTime);
        }

        public override void DrawScene(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

            spriteBatch.Draw(TextureManager.Textures["glengames"], Vector2.Zero, Color.White);

            if (!InBlackout(gameTime))
            {
                SoundManager.Play("tick");
                DrawAllBars(spriteBatch);
            }

            spriteBatch.End();
        }

        private bool InBlackout(GameTime gameTime)
        {
            if (gameTime.TotalGameTime < startBlackout)
                return false;

            if (gameTime.TotalGameTime > endBlackout)
                return false;

            return true;
        }

        Random rand = new Random();
        int chancePerSquare = 0;
        private void DrawAllBars(SpriteBatch spriteBatch)
        {
            chancePerSquare = rand.Next(0, 10);

            int columns = (spriteBatch.GraphicsDevice.Viewport.Width / BarWidth);
            int rows = (spriteBatch.GraphicsDevice.Viewport.Height / BarHeight);

            for (int y = 0; y < columns; y++)
            {
                for (int x = 0; x < rows; x++)
                {
                    if (PlaceBarHere())
                    {
                        DrawOneBar(new Vector2(y * BarWidth, x * BarHeight), spriteBatch);
                    } 
                }
            }
        }

        private bool PlaceBarHere()
        {
        
            int result = rand.Next(0, 1000);

            if (result <= chancePerSquare)
                return true;
            else
                return false;
        }

        private void DrawOneBar(Vector2 position, SpriteBatch spriteBatch)
        {
            Rectangle drawAtRectangle = new Rectangle((int)Math.Floor(position.X), (int)Math.Floor(position.Y), BarWidth, BarHeight);
            spriteBatch.Draw(TextureManager.Textures["onepixel"], drawAtRectangle, Color.Black);
        }
    }
}
