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.Input;

namespace LD20_Game
{
    class Buddi
    {
        static Vector2 alertBubbleOffset = new Vector2(-24, -48);
        static Vector2 offsetFromHead = new Vector2(-35, -30);
        Vector2 location;
        Vector2 hoverOffset = Vector2.Zero;
        AlertBubble alert;

        float pathSpeed = 40;
        float pathLength = 20;

        public TimeSpan nextAlertAt = TimeSpan.MinValue;

        public void HoverOver(Vector2 loc)
        {
            location = loc;
        }

        public void Update(GameTime gameTime)
        {
            int moved = GetBuddiMovementFactor(gameTime);

            if (moved > pathLength - 1)
                hoverOffset.Y = pathLength - (moved - pathLength);
            else
                hoverOffset.Y = moved;

            if (alert != null && alert.ExpiresAt < gameTime.TotalGameTime)
                alert = null;

            if (alert != null)
                alert.Location = location + hoverOffset + offsetFromHead + alertBubbleOffset;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Vector2 buddiLoc = location + hoverOffset + offsetFromHead;
            spriteBatch.Draw(TextureManager.Textures["buddi"], buddiLoc, Color.White);

            if (alert != null)
                alert.Draw(spriteBatch);
        }

        public void RaiseAlert(TrapType type, GameTime gameTime)
        {
            nextAlertAt = gameTime.TotalGameTime + TimeSpan.FromSeconds(3);

            Vector2 alertLoc = location + hoverOffset + offsetFromHead + alertBubbleOffset;
            alert = new AlertBubble(alertLoc, type, nextAlertAt);
        }

        private int GetBuddiMovementFactor(GameTime gameTime)
        {
            double temp = gameTime.TotalGameTime.TotalMilliseconds % (pathSpeed * (pathLength * 2));

            return (int)(temp / pathSpeed);
        }
    }
}
