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 TribbleExtinction
{
    class BlueGun:Weapon
    {
        Texture2D activeTex, blastTex1, blastTex2;
        Vector2 pos;

        bool firing;
        Vector2 firingTar;
        //int firingFrame;
        SoundEffect firingSound;

        float sumTime, animTime;


        public BlueGun(Game1 cGame, Vector2 npos)
        {
            activeTex = Program.masterGame.Content.Load<Texture2D>("shield1");
            blastTex1 = Program.masterGame.Content.Load<Texture2D>("shield1");
            blastTex2 = Program.masterGame.Content.Load<Texture2D>("shield2");
            firingSound = Program.masterGame.Content.Load<SoundEffect>("electricshield");
            pos = npos;

            firing = false;
            firingTar = new Vector2(0, 0);

            sumTime = 0;
        }

        public override void fire(Vector2 nLoc)
        {
            
            firingTar = nLoc;
            if (Vector2.DistanceSquared(Program.game.myPlayer.shipPosition, firingTar) < 100 * 100)
            {
                firing = true;
                firingSound.Play();
                Program.game.myPlayer.ResourceBlue = Program.game.myPlayer.ResourceBlue - 3;
                for (int i = 0; i < Program.game.enemies.Count; i++)
                {
                    Enemy tEnemy = Program.game.enemies[i];

                    if (Vector2.DistanceSquared(tEnemy.pos, pos) < 200 * 200)
                    {
                        Program.game.enemies.Remove(tEnemy);
                        Program.game.enemyCount--;
                        i--;
                    }
                }
            }
        }

        public override void update(MouseState cMouse, GameTime cTime)
        {
            sumTime += cTime.ElapsedGameTime.Milliseconds;//touch
            animTime += cTime.ElapsedGameTime.Milliseconds;

            if (cMouse.LeftButton == ButtonState.Pressed && Program.game.myPlayer.ResourceBlue > 2 && firing == false)
            {
                fire(new Vector2(cMouse.X, cMouse.Y));
                
            }

            if (sumTime > 1000)////Do this every 50 ms
            {
                  sumTime = 0;
                  firing = false;

                  
            }

            if (animTime > 100)
            {
                animTime = 0;
                if (activeTex == blastTex1)
                    activeTex = blastTex2;
                else
                    activeTex = blastTex1;
            }            
        }

        public override void render(SpriteBatch spriteBatch)
        {
            if (firing)
            {
                spriteBatch.Draw(activeTex, new Rectangle((int)pos.X - 100, (int)pos.Y - 100, 200, 200), Color.White);                
            }
        }
    }
}
