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 GreenGun:Weapon
    {
        Texture2D weaponTex, blastTex;
        Vector2 pos;
        float angle;

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

        float sumTime;

        public GreenGun(Game1 cGame)
        {
            weaponTex = Program.masterGame.Content.Load<Texture2D>("greengun");
            blastTex = Program.masterGame.Content.Load<Texture2D>("virus_animation");
            firingSound = Program.masterGame.Content.Load<SoundEffect>("PoisonGas");
            pos = new Vector2(25, 55);
            firing = false;
        }

        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.ResourceGreen = Program.game.myPlayer.ResourceGreen - 3;
                for (int i = 0; i < Program.game.enemies.Count; i++)
                {
                    Enemy tEnemy = Program.game.enemies[i];

                    if (Vector2.DistanceSquared(tEnemy.pos, firingTar) < 20 * 20)
                    {
                        Program.game.enemies[i].poisoned = true;
                    }
                }
            }
        }

        public override void update(MouseState cMouse, GameTime cTime)
        {
            sumTime += cTime.ElapsedGameTime.Milliseconds;//touch
            angle = (float)Math.Atan2((cMouse.X-pos.X),-(cMouse.Y-pos.Y));

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

            if (sumTime > 150)////Do this every 50 ms
            {
                {//Animate
                    sumTime -= 150;
                    firingFrame++;
                    if (firingFrame > 15)
                    {
                        firingFrame = 0;
                        firing = false;
                    }
                }
            }
        }

        public override void render(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(weaponTex, pos, null, Color.White, angle, new Vector2(weaponTex.Width / 2, weaponTex.Height / 2), 0.5f, SpriteEffects.None, 0);


            if (firing)
            {
                spriteBatch.Draw(blastTex,
                    new Rectangle((int)firingTar.X-50, (int)firingTar.Y-50, 100, 100),
                    new Rectangle((blastTex.Width / 8) * (firingFrame % 6), 0, (blastTex.Width / 8), (blastTex.Width / 8)),
                    Color.White);
            }
        }
    }
}
