using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Fireball
{
    class Gangster : Sprite
    {
        const int GANGSTER_SPEED = 150;
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int MOVE_LEFT = -1;
        const int MOVE_RIGHT = 1;

        public int health;
        int timeSinceSwing;
        int walkCount;
        int stunCounter;
        int flyTime;
        bool rightSide;

        enum State
        {
            Flying,
            Walking,
            Stunned,
            Swinging
        }

        State mCurrentState;
        Vector2 mDirection = Vector2.Zero;
        Vector2 mSpeed = Vector2.Zero;

        private Texture2D mSpriteTexture2;
        private Texture2D mSpriteTexture3;
        private Texture2D mSpriteTexture4;
        private Texture2D theSpriteTexture;
        SpriteEffects mEffect;

        ContentManager mContentManager;

        public Gangster(string theAssetName, Vector2 initPos, bool side)
            : base(theAssetName, initPos)
        {
            rightSide = side;
        }

        public bool touchHero()
        {
            switch (mCurrentState)
            {
                case State.Swinging:
                    {
                        return true;
                    }
                default:
                    {
                        return false;
                    }
            }
        }

        public void stun()
        {
            mCurrentState = State.Stunned;
        }

        public bool notFlying()
        {
            if (mCurrentState == State.Flying)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void takeHit(int pushback, bool direction)
        {
            if (direction == true)
            {
                Position.X = Position.X - pushback;
            }
            else
            {
                Position.X = Position.X + pushback;
            }
        }

        new public void LoadContent(ContentManager theContentManager)
        {
            mContentManager = theContentManager;
            base.LoadContent(theContentManager);

            mSpriteTexture2 = theContentManager.Load<Texture2D>("Gangster 4.png");
            mSpriteTexture3 = theContentManager.Load<Texture2D>("Gangster 2.png");
            mSpriteTexture4 = theContentManager.Load<Texture2D>("Gangster 1.png");
            theSpriteTexture = mSpriteTexture;
            mCurrentState = State.Flying;
            mEffect = SpriteEffects.None;

            walkCount = 0;
            timeSinceSwing = 0;
            health = 3;
            stunCounter = 0;
            flyTime = 0;
        }

        public void Update(GameTime theGameTime, Sprite knight)
        {

            mSpeed = new Vector2(0,0);

            switch (mCurrentState)
            {
                case State.Flying:
                    {
                        if (rightSide)
                        {
                            Position.X -= 150 * (float)theGameTime.ElapsedGameTime.TotalSeconds;
                            mEffect = SpriteEffects.FlipHorizontally;
                        }
                        else
                        {
                            Position.X += 150 * (float)theGameTime.ElapsedGameTime.TotalSeconds;
                        }
                        flyTime++;

                        if (flyTime >= 80)
                        {
                            mCurrentState = State.Stunned;
                        }
                        break;
                    }
                case State.Stunned:
                    {
                        stunCounter++;
                        if (stunCounter > 50) 
                        {
                            mCurrentState = State.Walking;
                            stunCounter = 0;
                        }
                        break;
                    }

                case State.Walking:
                    {
                        UpdateMovement(knight);
                        WalkingUpdate(theGameTime, knight);
                        break;
                    }

                case State.Swinging:
                    {
                        SwingUpdate(theGameTime);
                        break;
                    }
            }
            base.Update(theGameTime, mSpeed, mDirection);
        }

        private void UpdateMovement(Sprite knight)
        {
            if (true)
            {
                mSpeed = Vector2.Zero;
                mDirection = Vector2.Zero;

                if (Position.X <= -10)
                {
                    Position.X = -9;
                }

                if (Position.X >= 765)
                {
                    Position.X = 764;
                }

                if (Position.X > knight.Position.X && (Position.X - knight.Position.X > 45))
                {
                    mSpeed.X = GANGSTER_SPEED;
                    mDirection.X = MOVE_LEFT;
                    mEffect = SpriteEffects.FlipHorizontally;
                    walkCount++;
                    if (walkCount > 9)
                    {
                        theSpriteTexture = mSpriteTexture3;
                    }
                    if (walkCount > 18)
                    {
                        theSpriteTexture = mSpriteTexture;
                        walkCount = 0;
                    }
                }

                else if (Position.X < knight.Position.X && (knight.Position.X - Position.X > 40))
                {
                    mSpeed.X = GANGSTER_SPEED;
                    mDirection.X = MOVE_RIGHT;
                    mEffect = SpriteEffects.None;
                    walkCount++;
                    if (walkCount > 9)
                    {
                        theSpriteTexture = mSpriteTexture3;
                    }
                    if (walkCount > 18)
                    {
                        theSpriteTexture = mSpriteTexture;
                        walkCount = 0;
                    }
                }

                if (Position.Y > knight.Position.Y && (Position.Y - knight.Position.Y > 45))
                {
                    mSpeed.Y = GANGSTER_SPEED;
                    mDirection.Y = MOVE_UP;

                    walkCount++;
                    if (walkCount > 9)
                    {
                        theSpriteTexture = mSpriteTexture3;
                    }
                    if (walkCount > 18)
                    {
                        theSpriteTexture = mSpriteTexture;
                        walkCount = 0;
                    }
                }

                else if (Position.Y < knight.Position.Y && (knight.Position.Y - Position.Y > 45))
                {
                    mSpeed.Y = GANGSTER_SPEED;
                    mDirection.Y = MOVE_DOWN;

                    walkCount++;
                    if (walkCount > 9)
                    {
                        theSpriteTexture = mSpriteTexture3;
                    }
                    if (walkCount > 18)
                    {
                        theSpriteTexture = mSpriteTexture;
                        walkCount = 0;
                    }
                }
            }
        }

        private void WalkingUpdate(GameTime theGameTime, Sprite knight)
        {
            timeSinceSwing++;
            Random random = new Random();
            int randomNumber = random.Next(0, 100);
            if (timeSinceSwing > 80 && knight.Position.Y - Position.Y < 100 && knight.Position.Y - Position.Y > -100
                && knight.Position.X - Position.X < 100 && knight.Position.X - Position.X > -100 && randomNumber >= 10)
            {
                mCurrentState = State.Swinging;
                Size = new Rectangle(0, 0, (int)(mSpriteTexture2.Width * Scale), (int)(mSpriteTexture2.Height * Scale));
                timeSinceSwing = 0;
            }
        }

        private void SwingUpdate(GameTime theGameTime)
        {
            timeSinceSwing++;
            if (timeSinceSwing > 25)
            {
                mCurrentState = State.Walking;
            }
        }

        public override void Draw(SpriteBatch theSpriteBatch)
        {
            switch (mCurrentState)
            {
                case State.Flying:
                    {
                        theSpriteBatch.Draw(mSpriteTexture4, Position, new Rectangle(0, 0, mSpriteTexture4.Width, mSpriteTexture4.Height),
                    Color.White, 0.0f, Vector2.Zero, Scale, mEffect, 0);
                        break;
                    }
                case State.Walking:
                    {
                        base.Draw(theSpriteBatch, theSpriteTexture, mEffect);
                        break;
                    }
                case State.Stunned:
                    {
                        base.Draw(theSpriteBatch, theSpriteTexture, mEffect);
                        break;
                    }
                case State.Swinging:
                    {
                        theSpriteBatch.Draw(mSpriteTexture2, Position, new Rectangle(0, 0, mSpriteTexture2.Width, mSpriteTexture2.Height),
                Color.White, 0.0f, Vector2.Zero, Scale, mEffect, 0);
                        break;
                    }
            }
        }
    }
}
