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

namespace GhostArena6
{
    class AchievementsScreen : GameScreen
    {
        private List<AchievementDisplay> _achievements;
        private SpriteFont _titleFont;
        private Texture2D _buttonB;

        private SpriteFont _textFont;

        private Vector2 _iconLoc;
        private Vector2 _nameLoc;
        private Vector2 _pointsLoc;
        private Vector2 _dateLoc;

        private int _topIndex;

        private const int MaxAchievementsDisplayed = 6;

        public AchievementsScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            _iconLoc = new Vector2(10, 100);
            _nameLoc = new Vector2(90, 100);
            _pointsLoc = new Vector2(525, 100);
            _dateLoc = new Vector2(675, 100);

            _topIndex = 0;
        }

        public override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                _titleFont = ScreenManager.Content.Load<SpriteFont>("Content/scorestitle");
                _textFont = ScreenManager.Content.Load<SpriteFont>("Content/menufont"); 
                _buttonB = ScreenManager.Content.Load<Texture2D>("Content/ButtonB");
            }

            base.LoadGraphicsContent(loadAllContent);
        }

        public override void Initialize()
        {
            _achievements = new AchievementManager(ScreenManager.Game).Achievements();

            base.Initialize();
        }

        public override void HandleInput(InputState input, GameTime gameTime)
        {
            if (input.CurrentGamePadState.Buttons.B == Microsoft.Xna.Framework.Input.ButtonState.Pressed && input.LastGamePadState.Buttons.B == Microsoft.Xna.Framework.Input.ButtonState.Released)
                this.ExitScreen();

            //check for scrolling
            if (input.CurrentGamePadState.DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Pressed && input.LastGamePadState.DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Released)
            {
                _topIndex++;
                _topIndex = (int)MathHelper.Clamp(_topIndex, 0, _achievements.Count - 1 - MaxAchievementsDisplayed);
            }

            if (input.CurrentGamePadState.DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Pressed && input.LastGamePadState.DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Released)
            {
                _topIndex--;
                _topIndex = (int)MathHelper.Clamp(_topIndex, 0, _achievements.Count - 1 - MaxAchievementsDisplayed);
            }

            base.HandleInput(input, gameTime);
        }

        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();

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

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

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

            int numDisplayed = (int)MathHelper.Clamp(_achievements.Count, 0, MaxAchievementsDisplayed);
            
            //draw headers
            ScreenManager.SpriteBatch.DrawString(_textFont, "Name", _nameLoc, Color.White);
            ScreenManager.SpriteBatch.DrawString(_textFont, "Points", _pointsLoc, Color.White);
            ScreenManager.SpriteBatch.DrawString(_textFont, "Date Unlocked", _dateLoc, Color.White);
            
            //loop through list and draw each item
            for (int i = _topIndex; i < numDisplayed; i++)
            {
                ScreenManager.SpriteBatch.Draw(_achievements[i].Icon, _iconLoc + new Vector2(0, (i + 1) * 75), Color.White);
                ScreenManager.SpriteBatch.DrawString(_textFont, _achievements[i].Name, _nameLoc + new Vector2(0, (i + 1) * 75), Color.White);
                ScreenManager.SpriteBatch.DrawString(_textFont, _achievements[i].Points, _pointsLoc + new Vector2(0, (i + 1) * 75), Color.White);
                ScreenManager.SpriteBatch.DrawString(_textFont, _achievements[i].DateUnlocked, _dateLoc + new Vector2(0, (i + 1) * 75), Color.White);
            }

            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);
        }
    }
}
