using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Blindfold.Managers;

namespace Blindfold.Objects
{
    public class Monster : Obj
    {
        private Vector3 position;

        private float soundtimer;
        private bool leftFoot;

        private static Random rnd = new Random();

        private float speed;

        private int stage;
        private int growltimer;

        #region PROPERTIES
        public Vector3 POSITION
        {
            get
            {
                return position;
            }
        }
        #endregion

        public Monster(Vector3 pos, int Stage)
            : base()
        {
            position = pos;
            soundtimer = 600.0f;
            growltimer = rnd.Next(7000, 10000);
            stage = Stage;
        }

        public override void Update(GameTime gameTime)
        {
            Vector3 walkdir = ObjManager.GAME.PLAYER.POSITION - position;

            Vector2 walkdirection = new Vector2(walkdir.X, walkdir.Y);


            float dist = (float)(Math.Sqrt(walkdirection.X * walkdirection.X + walkdirection.Y * walkdirection.Y) / 50.0f);
            if (dist < 0.1f)
            {
                ObjManager.GAME.gameOver();
                return;
            }

            walkdirection.Normalize();

            float velocity = 0.005f + (stage * 0.001f);

            // Collision Detection
            Vector3 predictedPosition = position + ((new Vector3(walkdirection.X, 0, 0)) * velocity * gameTime.ElapsedGameTime.Milliseconds);
            List<Obj> list = ObjManager.GAME.OBJECTMANAGER.objectsOfType(typeof(Wall));
            bool collided = false;
            foreach (Wall w in list)
            {
                if ((new Rectangle((int)predictedPosition.X, (int)predictedPosition.Y, 16, 16).Intersects(new Rectangle((int)w.POSITION.X, (int)w.POSITION.Y, 16, 16))))
                {
                    collided = true;
                    break;
                }
            }
            if (!collided)
            {
                position = predictedPosition;
            }
            predictedPosition = position + ((new Vector3(0, walkdirection.Y, 0)) * velocity * gameTime.ElapsedGameTime.Milliseconds);
            foreach (Wall w in list)
            {
                if ((new Rectangle((int)predictedPosition.X, (int)predictedPosition.Y, 16, 16).Intersects(new Rectangle((int)w.POSITION.X, (int)w.POSITION.Y, 16, 16))))
                {
                    collided = true;
                    break;
                }
            }
            if (!collided)
            {
                position = predictedPosition;
            }

            speed = 0.0f;
            if (!walkdirection.Equals(Vector2.Zero))
            {
                speed = Math.Max(Math.Abs(walkdirection.X), Math.Abs(walkdirection.Y));
                speed *= gameTime.ElapsedGameTime.Milliseconds;
                soundtimer -= speed;
                growltimer = (int)((float)growltimer - speed);
            }

            if (soundtimer <= 0)
            {
                soundtimer = 1000.0f;
                //if (dist > 0 && dist <= 1)
                //{
                //    ObjManager.GAME.INPUTMANAGER.vibrateGamepad(0, 1.0f - dist);
                //}
                Vector3 footSound = new Vector3(position.X, position.Y, 0);
                if (leftFoot)
                {
                    ObjManager.GAME.SOUNDMANAGER.playSound("monsterstep_l", footSound);
                }
                else
                {
                    ObjManager.GAME.SOUNDMANAGER.playSound("monsterstep_r", footSound);
                }
                leftFoot = !leftFoot;
            }
            if (growltimer <= 0)
            {
                growltimer = rnd.Next(7000, 10000);
                int shout = rnd.Next(0, 3);
                if (stage == 1)
                {
                    ObjManager.GAME.SOUNDMANAGER.playSound("monster_" + shout, position);
                }
                else if (stage == 2)
                {
                    ObjManager.GAME.SOUNDMANAGER.playSound("monster_" + (shout+3), position);
                }
            }
            //else if (soundtimer < 950)
            //{
            //    ObjManager.GAME.INPUTMANAGER.vibrateGamepad(0, 0);
            //}

            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch sb)
        {
            sb.Draw(ObjManager.GAME.OBJTEX, new Rectangle(312 + (int)((position.X - ObjManager.GAME.PLAYER.POSITION.X) /* *10*/), 232 + (int)((position.Y - ObjManager.GAME.PLAYER.POSITION.Y) /* *10*/), 16, 16), Color.Red);
            base.Draw(sb);
        }
    }
}
