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

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

namespace Fireball
{
    class Knight : Sprite
    {
        const int KNIGHT_SPEED = 160;
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int MOVE_LEFT = -1;
        const int MOVE_RIGHT = 1;

        int timeSinceSwing;
        int walkCount;

        enum State
        {
            Walking,
            Swinging
        }

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

        SoundEffect swingSound;
        SoundEffectInstance swingClip;

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

        ContentManager mContentManager;

        public Knight(string theAssetName, Vector2 initPos)
            : base(theAssetName, initPos)
        {
        }

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

        public void takeHit(int pushback, bool direction)
        {
            if (direction == false)
            {
                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>("Knight Stance 2.png");
            mSpriteTexture3 = theContentManager.Load<Texture2D>("Knight Stance 3.png");
            theSpriteTexture = mSpriteTexture;
            mCurrentState = State.Walking;
            mEffect = SpriteEffects.None;
            walkCount = 0;
            timeSinceSwing = 0;

            swingSound = theContentManager.Load<SoundEffect>("strike");
            swingClip = swingSound.CreateInstance();
            swingClip.IsLooped = false;
        }

        public void Update(GameTime theGameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();

            UpdateMovement(aCurrentKeyboardState);

            switch (mCurrentState)
            {
                case State.Walking:
                    {
                        UpdateSword(theGameTime, aCurrentKeyboardState);
                        break;
                    }

                case State.Swinging:
                    {
                        SwingSword(theGameTime, aCurrentKeyboardState);
                        break;
                    }
            }

            mPreviousKeyboardState = aCurrentKeyboardState;

            base.Update(theGameTime, mSpeed, mDirection);
        }

        private void UpdateMovement(KeyboardState aCurrentKeyboardState)
        {
            if (true)
            {
                mSpeed = Vector2.Zero;
                mDirection = Vector2.Zero;

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

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

                if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
                {
                    mSpeed.X = KNIGHT_SPEED;
                    mDirection.X = MOVE_LEFT;
                    mEffect = SpriteEffects.FlipHorizontally;
                    walkCount++;
                    if (walkCount > 9)
                    {
                        theSpriteTexture = mSpriteTexture3;
                    }
                    if (walkCount > 18)
                    {
                        theSpriteTexture = mSpriteTexture;
                        walkCount = 0;
                    }
                }

                else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
                {
                    mSpeed.X = KNIGHT_SPEED;
                    mDirection.X = MOVE_RIGHT;
                    mEffect = SpriteEffects.None;
                    walkCount++;
                    if (walkCount > 9)
                    {
                        theSpriteTexture = mSpriteTexture3;
                    }
                    if (walkCount > 18)
                    {
                        theSpriteTexture = mSpriteTexture;
                        walkCount = 0;
                    }
                }

                if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true && Position.Y > 250)
                {
                    mSpeed.Y = KNIGHT_SPEED;
                    mDirection.Y = MOVE_UP;

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

                else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true && Position.Y < 425)
                {
                    mSpeed.Y = KNIGHT_SPEED;
                    mDirection.Y = MOVE_DOWN;

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

        private void UpdateSword(GameTime theGameTime, KeyboardState aCurrentKeyboardState)
        {
            timeSinceSwing++;
            if (aCurrentKeyboardState.IsKeyDown(Keys.LeftControl) == true && timeSinceSwing > 80)
            {
                mCurrentState = State.Swinging;
                Size = new Rectangle(0, 0, (int)(mSpriteTexture2.Width * Scale), (int)(mSpriteTexture2.Height * Scale));
                timeSinceSwing = 0;
                swingClip.Play();
            }
        }

        private void SwingSword(GameTime theGameTime, KeyboardState aCurrentKeyboardState)
        {
            timeSinceSwing++;
            if (timeSinceSwing > 25)
            {
                mCurrentState = State.Walking;
            }
        }

        public override void Draw(SpriteBatch theSpriteBatch)
        {

            switch (mCurrentState) {
                case State.Walking:
                    {
                        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;
                    }
            }
        }

    }
}