﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ScrollingTextThing2
{
    public class TextDisplay
    {
        public enum DisplayStates
        {
            TOP,
            MIDDLE,
            BOTTOM
        };

        public DisplayStates states;
        public List<string> strings;
        public string currentString;
        public int maxTime;
        private int maxTime2;
        private int curTime;
        private bool first;
        public float textSize;
        public SpriteFont font;
        public bool going;
       
        public Vector2 location;
        SpriteFont defaultFont;
        int cString;

        public TextDisplay(string initialText, Microsoft.Xna.Framework.Content.ContentManager content, GraphicsDevice device)
        {
            first = true;
            maxTime = 2000;
            maxTime2 = 2000;
            curTime = 0;
            going = false;
            cString = 0;
            defaultFont = content.Load<SpriteFont>("DefaultFont");
            currentString = "nil";
            states = DisplayStates.TOP;
            location = new Vector2(device.PresentationParameters.BackBufferWidth / 2, -100.0f);
        }
        public void Update(GameTime gameTime, GraphicsDevice device)
        {
            if (going)
            {
                switch (states)
                {
                    case DisplayStates.TOP:
                        location.Y = -100;
                        if (!first)
                        {
                            cString++;
                        }
                        first = false;
                        if (cString == strings.Count)
                        {
                            cString = 0;
                        }
                        currentString = strings[cString];

                        
                        states = DisplayStates.MIDDLE;
                        break;
                    case DisplayStates.MIDDLE:
                        location.Y = MathHelper.Lerp(location.Y, device.PresentationParameters.BackBufferHeight / 2, 0.05f);
                        curTime += gameTime.ElapsedGameTime.Milliseconds;
                        if (curTime >= maxTime)
                        {
                            curTime = 0;
                            states = DisplayStates.BOTTOM;
                        }
                        break;
                    case DisplayStates.BOTTOM:
                        location.Y = MathHelper.Lerp(location.Y, device.PresentationParameters.BackBufferHeight + 100, 0.05f);
                        curTime += gameTime.ElapsedGameTime.Milliseconds;
                        if (curTime >= maxTime2)
                        {
                            
                            curTime = 0;
                            states = DisplayStates.TOP;
                        }
                        break;
                }
            }
        }
        public void Render(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Vector2 size = defaultFont.MeasureString(currentString);
            Vector2 origin = size * 0.5f;

            spriteBatch.DrawString(defaultFont, currentString, location, Color.White, 0, origin, 1, SpriteEffects.None, 0);
        }
        public void Begin(List<string> strings, int t)
        {
            this.strings = strings;
            cString = 0;
            maxTime = t;
            states = DisplayStates.TOP;
            going = true;
        }
        public void End()
        {
            going = false;
        }
    }
}
