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 MudslideTrap : BaseTrap
    {
        static Vector2 alertOffset = new Vector2(60, 50);
        static Vector2 alertSize = new Vector2(5, 80);
        static Vector2 payloadSpeed = new Vector2(0, 10);
        static Vector2 payloadSize = new Vector2(82, 120);
        static Vector2 payloadSpawnOffset = new Vector2(0, -150);
        static float payloadTravelDist = 200;

        const int TrapWidth = 82;
        const int TrapHeight = 16;

        bool primed;
        TimeSpan fallDownAt = TimeSpan.MinValue;
        bool falling;
        Vector2 payloadOffset;

        public MudslideTrap(int x, int y) : this(new Vector2(x,y)) { }

        public MudslideTrap(Vector2 loc)
            : base(loc)
        {
            primed = false;
            base.type = TrapType.Mudslide;
            payloadOffset = Vector2.Zero;
        }

        public override void Update(GameTime gameTime)
        {
            if (primed && !falling && gameTime.TotalGameTime > fallDownAt)
            {
                primed = false;
                falling = true;
            }

            if (falling)
            {
                // Done falling?
                if (payloadOffset.Y > payloadTravelDist)
                {
                    falling = false;
                }
                else
                {
                    payloadOffset += payloadSpeed;
                }
            }

            base.Update(gameTime);
        }

        protected override void CreateBuddiAlerts(Vector2 pos)
        {
            int leftXLoc = (int)(pos.X - alertOffset.X);
            int leftYLoc = (int)(pos.Y + alertOffset.Y);
            buddiAlerts.Add(new BoundingBox(new Vector3(leftXLoc, leftYLoc, 0),
                                             new Vector3(leftXLoc + alertSize.X, leftYLoc + alertSize.Y, 0)));

            int rightXLoc = (int)(pos.X + TrapWidth + alertOffset.X);
            int rightYLoc = (int)(pos.Y + alertOffset.Y);
            buddiAlerts.Add(new BoundingBox(new Vector3(rightXLoc, rightYLoc, 0),
                                            new Vector3(rightXLoc + alertSize.X, rightYLoc + alertSize.Y, 0)));
        }

        public void Activate(GameTime gameTime)
        {
            if (!primed)
            {
                payloadOffset = Vector2.Zero + payloadSpawnOffset;
                primed = true;
                fallDownAt = gameTime.TotalGameTime + TimeSpan.FromMilliseconds(500);
            }

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(TextureManager.Textures["mudslidespawner"], location, Color.White);

            if (falling)
                spriteBatch.Draw(TextureManager.Textures["mudslide"], location + payloadOffset, Color.White);

            base.Draw(spriteBatch);
        }

        public override BoundingBox FatalArea
        {
            get 
            {
                if (falling)
                {
                    return new BoundingBox(new Vector3(location.X, location.Y + payloadOffset.Y, 0),
                                           new Vector3(location.X + payloadSize.X, location.Y + + payloadOffset.Y + payloadSize.Y , 0));

                }

                return new BoundingBox(); 
            }
        }
    }
}
