using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Snake
{
    class StartLevelScreen : GameScreen
    {
        SpriteFont gameFont;
        ContentManager content;

        int num;
        public int Number
        {
            get { return num; }
            set { num = value; }
        }

        string title;
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        string description;
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public StartLevelScreen(int num, string title, string description)
        {
            this.num = num;
            this.title = title;
            this.description = description;
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            gameFont = content.Load<SpriteFont>("gamefont");
            base.LoadContent();
        }
        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            PlayerIndex playerIndex;
            if (input.IsPauseGame(ControllingPlayer))
            {
                ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }
            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                ExitScreen();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            spriteBatch.Begin();
            // Ужас, но работает, нет времени делать красиво без мусора.
            string[] strings = title.Split('|');
            int y = 300;
            foreach (string s in strings)
            {
                Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, y);
                Vector2 titleOrigin = font.MeasureString(s) / 2;
                Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
                float titleScale = 1.25f;
                titlePosition.Y -= transitionOffset * 100;

                spriteBatch.DrawString(font, s, titlePosition, titleColor, 0,
                                       titleOrigin, titleScale, SpriteEffects.None, 0);
                y += font.LineSpacing;
            }

            y = 500;
            strings = description.Split('|');
            foreach (string s in strings)
            {
                Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, y);
                Vector2 titleOrigin = gameFont.MeasureString(s) / 2;
                Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
                float titleScale = 1.0f;
                titlePosition.Y -= transitionOffset * 100;

                spriteBatch.DrawString(gameFont, s, titlePosition, titleColor, 0,
                                       titleOrigin, titleScale, SpriteEffects.None, 0);
                y += gameFont.LineSpacing;
            }

            string numTitle = string.Format("Уровень {0}", num + 1);
            if (num + 1 == Game1.LEVELS)
            {
                numTitle = "Финальный уровень";
            }            
            Vector2 pos = new Vector2(graphics.Viewport.Width / 2, 200);
            Vector2 origin = font.MeasureString(numTitle) / 2;
            Color color = new Color(192, 192, 192) * TransitionAlpha;
            float scale = 1.0f;
            pos.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, numTitle, pos, color, 0,
                                   origin, scale, SpriteEffects.None, 0);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
