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 Enemy
    {
        public int life;

        public Vector2 pos;
        float angle;
        int frameOffset;
        int cFrame;
        int sumTime;
        int size;
        Color shade;
        Vector2 Center;

        public bool poisoned;

        Texture2D tribbleTex;
        Texture2D skugs_chomp4;
        bool biting;

        //contructor
        public Enemy(Game1 cGame, int tileSize)
        {
            if (Program.game.rand.Next(0,10) >= 5)
                pos = new Vector2(896, Program.game.rand.Next(0, 640));
            else
                pos = new Vector2(Program.game.rand.Next(0, 896), 640);

            tribbleTex = Program.masterGame.Content.Load<Texture2D>("scurry01");
            skugs_chomp4 = Program.masterGame.Content.Load<Texture2D>("skugs_chomp4");

            sumTime = Program.game.rand.Next(0, 200);
            size = 14;
            poisoned = false;

            shade = Color.White;
            shade.R = (byte)(Program.game.rand.Next(160, 255));
            shade.G = (byte)(Program.game.rand.Next(160, 255));
            shade.B = (byte)(Program.game.rand.Next(160, 255));

            angle = (float)(Program.game.rand.NextDouble()*Math.PI*2);

            cFrame = Program.game.rand.Next(0, 12);

            life = 100;
            Center = new Vector2(size / 2, size / 2);
        }

        public void update(GameTime cTime, Vector2 playerPos)
        {
            RandomGood rand = Program.game.rand;
            sumTime += cTime.ElapsedGameTime.Milliseconds;//touch


            if (poisoned)
            {
                shade.R = (byte)(Program.game.rand.Next(80, 120));
                shade.G = (byte)(Program.game.rand.Next(200, 255));
                shade.B = (byte)(Program.game.rand.Next(80, 120));
            }

            // ------------------------------------ ALIVE!!!!! ------------------------------------
            
            if (life > 0)
            {
                if (sumTime > 50)////Do this every 50 ms
                {
                    {//Animate
                        sumTime -= 50;
                        cFrame++;
                        if (cFrame > 3)
                            cFrame = 0;
                    }
                }

                //AI
                float distSqrShip = Vector2.DistanceSquared(pos, Program.game.myPlayer.shipPosition);
                float distSqr = Vector2.DistanceSquared(pos, playerPos);
                
                biting = false;

                if (distSqrShip < 3600)//60 ^2
                {
                    Program.game.myPlayer.shipCorruptionCount++;
                    biting = true;
                }
                else if (distSqrShip < 25600)//160 ^2
                {
                    angle = (float)Math.Atan2((Program.game.myPlayer.shipPosition.Y - pos.Y), -(pos.X - Program.game.myPlayer.shipPosition.X));
                    this.angle = angle;
                }
                else if (distSqr < 100)//10 ^2
                {
                    Program.game.myPlayer.droneCorruptionCount++;
                    biting = true;
                }
                else if (distSqr < 4096)//64 ^2
                {
                    angle = (float)Math.Atan2((playerPos.Y - pos.Y), -(pos.X - playerPos.X));
                    this.angle = angle;
                }
                else//if player is furth than 64 ^2
                    angle += ((float)Program.game.rand.NextDouble() - 0.5f) / 5;
                //angle += f / 100f;

                //Move
                pos.X += 0.7f * (float)Math.Cos(angle);//touch
                pos.Y += 0.7f * (float)Math.Sin(angle);//touch

                //Colide with edges
                if (pos.X < -10)
                {
                    pos.X = -10;
                    if (angle < Math.PI) angle = (float)Math.PI - angle;
                    if (angle > Math.PI) angle = (float)(3 * Math.PI - angle);
                }
                else if (pos.X > 906)
                {
                    pos.X = 906;
                    if (angle < Math.PI) angle = (float)Math.PI - angle;
                    if (angle > Math.PI) angle = (float)(3 * Math.PI - angle);
                }
                if (pos.Y < -10)
                {
                    pos.Y = -10;
                    if (angle > Math.PI / 2 && angle < Math.PI * 3 / 2) angle = (float)(2 * Math.PI - angle);
                    else angle = (float)(2 * Math.PI - angle);
                }
                else if (pos.Y > 650)
                {
                    pos.Y = 650;
                    if (angle > Math.PI / 2 && angle < Math.PI * 3 / 2) angle = (float)(2 * Math.PI - angle);
                    else angle = (float)(2 * Math.PI - angle);
                }

            }
            else
            {
                if (sumTime > 50)////Do this every 50 ms
                {
                    {//Animate
                        sumTime -= 50;
                        cFrame++;
                        if (cFrame > 7)
                            cFrame = 7;
                    }
                }
            }

        }

        public void render(SpriteBatch spriteBatch)
        {
            Rectangle sourceRect = new Rectangle((cFrame % 8) * 20, (int)Math.Floor((double)cFrame / 8) * 20, 20, 20);
            
            if (biting)
                spriteBatch.Draw(skugs_chomp4, pos, sourceRect, shade, angle, Center, 0.5f, SpriteEffects.None, 0);
            else
                spriteBatch.Draw(tribbleTex, pos, sourceRect, shade, angle, Center, 0.5f, SpriteEffects.None, 0);
        }

    }
}
