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
{
    public class Player
    {
        int numToKillShip = 50;

        int drones;

        public Vector2 shipPosition, dronePos;

        float droneSpeed;
        float droneMomentumCut;
        float droneMomentumAdd;
        float droneVelocityMax;
        public Vector2 droneVelocity;
        float droneAngle, droneScale;
        int shipSize;
        public int shipCorruptionCount, droneCorruptionCount;
        public int shipCorruptionNow;
        public float ResourceRed, ResourceGreen, ResourceBlue;
        bool Dead = false;
        public float DeathTimer = 3.0f;
        bool Win = false;
        public float WinTimer = 3.0f;

        SoundEffect DroneDead;
        SoundEffect ShipDead;

        //List of weapons

        //Resources

        Texture2D droneTex;
        Texture2D droneTrailTex;
        Texture2D shipTex;
        Texture2D weaponTex;
        Texture2D resourceRTex;
        Texture2D resourceGTex;
        Texture2D resourceBTex;
        Texture2D WinBlast;

        Vector2[] DroneTrail;

        public Player(Game1 cGame, int tileSize)
        {
            droneTex = Program.masterGame.Content.Load<Texture2D>("Drone");
            droneTrailTex = Program.masterGame.Content.Load<Texture2D>("DroneTrail");
            shipTex = Program.masterGame.Content.Load<Texture2D>("Base");
            resourceBTex = Program.masterGame.Content.Load<Texture2D>("bars_animation_blue");
            resourceGTex = Program.masterGame.Content.Load<Texture2D>("bars_animation_green");
            resourceRTex = Program.masterGame.Content.Load<Texture2D>("bars_animation_red");
            DroneDead = Program.masterGame.Content.Load<SoundEffect>("DroneDeath");
            ShipDead = Program.masterGame.Content.Load<SoundEffect>("LiftOff");
            WinBlast = Program.masterGame.Content.Load<Texture2D>("WinBlast");

            dronePos = new Vector2(48, 48);
            shipPosition = new Vector2(80, 80);
            shipSize = 20;
            droneAngle = 0;
            droneScale = 20f / 40f;

            drones = 5;

            droneMomentumCut = 0.1f;
            droneMomentumAdd = 0.05f;
            droneVelocityMax = 1.5f;

            ResourceRed = 10f;
            ResourceGreen = 10f;
            ResourceBlue = 10f;

            DroneTrail = new Vector2[10];
        }

        public void update(KeyboardState keybState, GameTime gameTime)
        {
            if (droneCorruptionCount > 7)
            {
                Program.game.effectLibrary.CreateRedBlobExplosion(dronePos);
                dronePos = new Vector2(48, 48);
                drones--;
                DroneDead.Play();
            }
            shipCorruptionNow = shipCorruptionCount;
            if (shipCorruptionCount > numToKillShip || Dead || drones == 0)
            {
                if (!Dead)
                    ShipDead.Play();
                DeathTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                Dead = true;
                float f = (float)Program.game.rand.NextDouble();

                if (f < 0.1f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition);
                else if (f < 0.2f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(50, 0));
                else if (f < 0.3f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(-50, 0));
                else if (f < 0.4f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(0, 50));
                else if (f < 0.5f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(0, -50));
                else if (f < 0.6f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(30, 30));
                else if (f < 0.7f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(-30, 30));
                else if (f < 0.8f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(-30, -30));
                else if (f < 0.9f)
                    Program.game.effectLibrary.CreateRedBlobExplosion(shipPosition + new Vector2(30, -30));
                else 
                    Program.game.effectLibrary.CreateHugeRedBlob(shipPosition);

                Program.game.effectLibrary.CreateRedBlobExplosion(dronePos);
                dronePos = new Vector2(48, 48);
                drones = 0;
            }

            if (ResourceRed > 75 && ResourceGreen > 75 && ResourceBlue > 75)
                Win = true;

            if (Win)
            {
                Program.game.enemyRate = 0;
                for(int i = Program.game.enemyCount-1; i>=0;i--)
                {
                    Enemy en = Program.game.enemies[i];
                    float dist = (en.pos - shipPosition).LengthSquared();
                    if (dist / 1000000f < 1.0f - WinTimer / 3f)
                    {
                        Program.game.effectLibrary.CreateBloodSplat(en.pos);
                        Program.game.enemyCount--;
                        Program.game.enemies.Remove(en);
                    }
                }
                WinTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            float droneVelocityMax = this.droneVelocityMax;
            droneVelocityMax -= droneCorruptionCount / 7.0f;

            bool move = false;
            if (keybState.IsKeyDown(Keys.Up) || keybState.IsKeyDown(Keys.W))
            {
                droneVelocity.Y -= droneMomentumAdd;
                move = true;
            }
            else if (keybState.IsKeyDown(Keys.Down) || keybState.IsKeyDown(Keys.S))
            {
                droneVelocity.Y += droneMomentumAdd;
                move = true;
            }
            else
            {
                if (Math.Abs(droneMomentumCut) > Math.Abs(droneVelocity.Y))
                    droneVelocity.Y = 0f;
                else 
                    droneVelocity.Y -= Math.Sign(droneVelocity.Y) * droneMomentumCut;
            }

            if (keybState.IsKeyDown(Keys.Right) || keybState.IsKeyDown(Keys.D))
            {
                droneVelocity.X += droneMomentumAdd;
                move = true;
                //droneAngle += 0.1f;
            }
            else if (keybState.IsKeyDown(Keys.Left) || keybState.IsKeyDown(Keys.A))
            {
                droneVelocity.X -= droneMomentumAdd;
                //droneAngle -= 0.1f;
            }
            else
            {
                if (Math.Abs(droneMomentumCut) > Math.Abs(droneVelocity.X))
                    droneVelocity.X = 0f;
                else
                    droneVelocity.X -= Math.Sign(droneVelocity.X) * droneMomentumCut;
            }

            if (Math.Abs(droneVelocity.X) > droneVelocityMax)
                droneVelocity.X = Math.Sign(droneVelocity.X) * droneVelocityMax;

            if (Math.Abs(droneVelocity.Y) > droneVelocityMax)
                droneVelocity.Y = Math.Sign(droneVelocity.Y) * droneVelocityMax;

            /*float speed = 1.0f;

            Tile[,] Map = Program.game.gameWorld.map;
            
            dronePos*/

            dronePos += droneVelocity;

            if (dronePos.X < 10) dronePos.X = 10;
            if (dronePos.X > 886) dronePos.X = 886;
            if (dronePos.Y < 10) dronePos.Y = 10;
            if (dronePos.Y > 630) dronePos.Y = 630;

            droneCorruptionCount = 0;
            shipCorruptionCount = 0;
        }
        byte teast = 0;
        bool Flash;
        void ResourceRender(float Resource, Texture2D image, SpriteBatch SpriteBatch)
        {
            Vector2 ResPos = new Vector2(shipPosition.X+1, shipPosition.Y - 15);

            if (Resource > 80.0f) Resource = 80.0f;
            int Step = (int)(Resource / 10.0f);
            if (Step > 10) Step = 10;

            Flash = !Flash;
            if (Resource <= 75.0f || Flash)
            {
                Color fade = Color.White;
                byte b = (byte)((Resource - Step * 10) * 25.0);
                fade.A = b;
                //fade.R = fade.G = fade.B = b;
                Step++;
                Rectangle rect = new Rectangle(((Step + 1) % 8) * (image.Width / 8), ((Step + 1) / 8) * (image.Height / 2), image.Width / 8, image.Height / 2);
                SpriteBatch.Draw(image, ResPos, rect, fade, 0, new Vector2(80, 80), 1.0f, SpriteEffects.None, 0);


                rect = new Rectangle((Step % 8) * (image.Width / 8), (Step / 8) * (image.Height / 2), image.Width / 8, image.Height / 2);
                SpriteBatch.Draw(image, ResPos, rect, Color.White, 0, new Vector2(80, 80), 1.0f, SpriteEffects.None, 0);
            }
        }

        float trailtime = 0f;
        
        public void render(SpriteBatch spriteBatch, GameTime cTime)
        {
            trailtime += (float)cTime.ElapsedGameTime.TotalSeconds;
            //Draw Drones
            for (int i = 0; i < drones-1; i++)
            {
                float x = (float)Math.Cos(i * 3.1415 / 2.5 + 0.2) * shipTex.Width / 5.8f, y = (float)Math.Sin(i * 3.1415 / 2.5 + 0.2) * shipTex.Width / 7.5f;
                spriteBatch.Draw(droneTex, new Vector2(shipPosition.X + x, shipPosition.Y + y), null, Color.Gray, droneAngle, new Vector2(droneTex.Width / 2, droneTex.Height / 2), droneScale, SpriteEffects.None, 0);
            }
            Color colorship = Color.White;
            colorship.G = colorship.B = (byte)(255 - shipCorruptionNow);
            spriteBatch.Draw(shipTex, shipPosition, null, Color.White, 0, new Vector2(shipTex.Width / 2, shipTex.Height / 2), 0.5f, SpriteEffects.None, 0);

            ResourceRender(ResourceBlue, resourceBTex, spriteBatch);
            ResourceRender(ResourceGreen, resourceGTex, spriteBatch);
            ResourceRender(ResourceRed, resourceRTex, spriteBatch);

            if (trailtime > 0.2f)
            {
                trailtime -= 0.2f;
                for (int i = 0; i < 9; i++)
                    DroneTrail[i] = DroneTrail[i + 1];
                DroneTrail[9] = dronePos;
            }

            if (Win)
            {
                spriteBatch.Draw(WinBlast, shipPosition, null, Color.White, 0, new Vector2(WinBlast.Width / 2, WinBlast.Height / 2), 2*(3.0f - WinTimer), SpriteEffects.None, 0);
            }
            for (int i = 9; i >= 0; i--)
                spriteBatch.Draw(droneTrailTex, DroneTrail[i], null, Color.White, 0, new Vector2(droneTrailTex.Width / 2, droneTrailTex.Height / 2), i / 10f * droneScale, SpriteEffects.None, 0);

            spriteBatch.Draw(droneTex, dronePos, null, Color.White, droneAngle, new Vector2(droneTex.Width / 2, droneTex.Height / 2), droneScale, SpriteEffects.None, 0);
        }
    }
}
