using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;

namespace GhostArena3D
{
   class HighScoreScreen : GameScreen
   {
      private HighScoreList _list;
      private SpriteFont _titleFont;
      private Texture2D _buttonB;

      public HighScoreScreen()
      {
         //load high score list
         _list = new HighScoreList();
         _list.Load();

         TransitionOnTime = TimeSpan.FromSeconds(1.5);
         TransitionOffTime = TimeSpan.FromSeconds(0.5);
      }


      public override void LoadContent()
      {
         _titleFont = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/scorestitle");
         _buttonB = ScreenManager.Game.Content.Load<Texture2D>("Images/ButtonB");

         base.LoadContent();
      }


      public override void HandleInput(InputState input)
      {
         if(SignedInGamer.SignedInGamers.Count == 0)
            return;

         if (input.CurrentGamePadStates[0].Buttons.B == Microsoft.Xna.Framework.Input.ButtonState.Pressed && input.LastGamePadStates[0].Buttons.B == Microsoft.Xna.Framework.Input.ButtonState.Released)
            this.ExitScreen();

         base.HandleInput(input);
      }

      public override void Draw(GameTime gameTime)
      {
         Vector2 textPosition;

         ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0);

         Color color = new Color(0, 255, 0, TransitionAlpha);

         ScreenManager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend,
         SpriteSortMode.Immediate,
         SaveStateMode.SaveState);

         //draw title
         Vector2 titleSize = _titleFont.MeasureString("High Scores");
         textPosition = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 - titleSize.X / 2, 5);

         ScreenManager.SpriteBatch.DrawString(_titleFont, "High Scores", textPosition, color);

         color = new Color(255, 255, 255, TransitionAlpha);

         //draw header
         textPosition = new Vector2(50, 100);
         ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "Date", textPosition, color);
         ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "Score", textPosition + new Vector2(250,0), color);
         ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "Level", textPosition + new Vector2(400, 0), color);

         int count = 0;

         if(_list != null && _list.Scores != null)
         {
            //loop through list and draw each item
            foreach (HighScore item in _list.Scores)
            {
               textPosition = new Vector2(50, 150 + 50 * count);

               ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, item.Date, textPosition, color);
               ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, item.Score.ToString(), textPosition + new Vector2(250, 0), color);
               ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, item.Level.ToString(), textPosition + new Vector2(400, 0), color);

               count++;
            }
         }
         else
         {
            textPosition = new Vector2(50, 200);
            ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "There are no high scores.", textPosition, color);
         }

         ScreenManager.SpriteBatch.Draw(_buttonB, new Rectangle(800, 700, 48, 48), Color.White);
         ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "Back", new Vector2(855, 700), Color.White);

         ScreenManager.SpriteBatch.End();

         if (TransitionPosition > 0)
            ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
      }
   }
}
