using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Hover
{
    class Bomb : Sprite
    {
        public bool Exploded = false;
        public Vector2 Velocity;

        public Bomb()
        {
            SourceRec = new Rectangle(64, 48, 16, 16);
            Origin = new Vector2(8, 8);
        }

        int c = 0;
        public override void Update()
        {
            c++;
            if (c >= 3)
            {
                c = 0;
                Rotation += MathHelper.PiOver2;
                Rotation %= MathHelper.TwoPi;
            }

            if (Velocity.Y < 1)
                Velocity.Y += 0.02f;

            Position += Velocity * 5;

            int yindex = (int)((Position.Y - Level.S.Rows[0].PosY) / Engine.TileSize);
            if (yindex >= Level.S.Rows.Count)
            {
                Explode();
                return;
            }
            if (yindex > 0)
            {
                var x = (int)(Position.X / Engine.TileSize);
                if (x < 0 || x >= Level.S.Rows[0].Blocks.Length)
                {
                    Explode();
                    return;
                }
                var block = Level.S.Rows[yindex].Blocks[x];
                if (block != null && block.Type == BlockType.Block)
                {
                    Explode();
                    return;
                }
            }
            
            base.Update();
        }

        public void Explode()
        {
            if (Exploded) return;
            Exploded = true;

            Engine.Explode(Position - new Vector2(8, 8));

            int yindex = (int)((Position.Y - Level.S.Rows[0].PosY) / Engine.TileSize);
            var xindex = (int)(Position.X / Engine.TileSize);

            for (int x = -1; x < 2; x++)
                for (int y = -1; y < 2; y++)
                    tryRemoveBlock(xindex + x, yindex + y);

            for (int i = -2; i < 3; i++)
            {
                int y = yindex + i;
                if (y < 1 || y >= Level.S.Rows.Count) continue;
                Level.S.Rows[y].CalcTiles();
            }

            if (Engine.S.friend && Engine.S.chatIndex < Engine.helpTexts.Length)
            {
                var dist = Vector2.DistanceSquared(Position, Engine.S.friendPos);
                if (dist < 40 * 40)
                {
                    Engine.S.friend = false;
                    Engine.UnlockArtifact(11);
                }
            }
        }

        private void tryRemoveBlock(int x, int y)
        {
            //y -= Level.S.Rows[0].RowNum;

            if (x < 0 || x >= Level.S.Rows[0].Blocks.Length) return;
            if (y < 2 || y >= Level.S.Rows.Count) return;

            Level.S.Rows[y].Blocks[x] = null;
        }
    }
}
