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

        bool firing;
        Vector2 firingTar;
        int firingFrame;

        float sumTime;


        public RedGun(Game1 cGame)
        {
            weaponTex = Program.masterGame.Content.Load<Texture2D>("redgun");
            blastTex = Program.masterGame.Content.Load<Texture2D>("nuke_animation");
            firingSound = Program.masterGame.Content.Load<SoundEffect>("NukeExplosion");
            pos = new Vector2(135, 55);

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

            sumTime = 0;
        }

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

                    if (Vector2.DistanceSquared(tEnemy.pos, firingTar) < 96 *96 )
                    {
                        Program.game.effectLibrary.CreateBloodSplat(tEnemy.pos);

                        Program.game.enemies.Remove(tEnemy);
                        Program.game.enemyCount--;
                        i--;
                    }                    
                }
            }
        }

        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.LeftButton == ButtonState.Pressed && Program.game.myPlayer.ResourceRed > 2 && firing == false)
            {
                fire(new Vector2(cMouse.X, cMouse.Y));
                
            }

            if (sumTime > 150)////Do this every 50 ms
            {
                {//Animate
                    sumTime -= 150;
                    firingFrame++;
                    if (firingFrame > 5)
                    {
                        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);
            }
            
        }
    }
}
