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

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

        private static Random rnd = new Random();

        private float soundtimer;
        private bool leftFoot;

        private int shouttimer;

        private Checkpoint currentCP;
        private float currentDist;

        private float speed;

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

        public Guider(Vector3 pos)
            : base()
        {
            position = pos;
            soundtimer = 500.0f;
        }

        public override void Update(GameTime gameTime)
        {
            float cpdist;
            if (!ObjManager.GAME.CPList.Contains(currentCP))
            {
                currentCP = null;
            }
            if (ObjManager.GAME.CPList.Count > 0)
            {
                foreach (Checkpoint c in ObjManager.GAME.CPList)
                {
                    Vector3 a = c.POSITION - position;
                    cpdist = (float)(Math.Sqrt(a.X * a.X + a.Y * a.Y) / 50.0f);
                    if (currentCP == null || cpdist < currentDist)
                    {
                        currentCP = c;
                        currentDist = cpdist;
                    }
                }
            }
            else
            {
                Checkpoint c = ObjManager.GAME.FINISH;
                Vector3 a = c.POSITION - position;
                cpdist = (float)(Math.Sqrt(a.X * a.X + a.Y * a.Y) / 50.0f);
                if (currentCP == null || cpdist < currentDist)
                {
                    currentCP = c;
                    currentDist = cpdist;
                }
            }

            if (currentDist < 0.2)
            {
                Vector3 a = ObjManager.GAME.PLAYER.POSITION - position;
                float pldist = (float)(Math.Sqrt(a.X * a.X + a.Y * a.Y) / 50.0f);
                if (pldist < 0.3f)
                {
                    ObjManager.GAME.CPList.Remove(currentCP);
                    if (currentCP.Equals(ObjManager.GAME.FINISH))
                    {
                        ObjManager.GAME.nextLevel();
                    }
                }
            }
            else
            {
                Vector3 walkdir = currentCP.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);

                walkdirection.Normalize();

                System.Diagnostics.Debug.WriteLine(dist);

                position += (new Vector3(walkdirection, 0)) * 0.008f * gameTime.ElapsedGameTime.Milliseconds;

                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;
                }

                if (soundtimer <= 0)
                {
                    soundtimer = 500.0f;
                    Vector3 footSound = new Vector3(position.X, position.Y, 0);
                    if (leftFoot)
                    {
                        ObjManager.GAME.SOUNDMANAGER.playSound("step_l", footSound);
                    }
                    else
                    {
                        ObjManager.GAME.SOUNDMANAGER.playSound("step_r", footSound);
                    }
                    leftFoot = !leftFoot;
                }
            }

            if (shouttimer > 0)
            {
                shouttimer -= gameTime.ElapsedGameTime.Milliseconds;
            }
            else
            {
                if (ObjManager.GAME.INPUTMANAGER.buttonHit(Buttons.LeftShoulder, 0) || ObjManager.GAME.INPUTMANAGER.buttonHit(Buttons.LeftTrigger, 0))
                {
                    shouttimer = 3000;
                    int shout = rnd.Next(0, 6);
                    ObjManager.GAME.SOUNDMANAGER.playSound("kat_follow_" + shout, position);
                }
                else
                {
                    Vector3 monsterdir = ObjManager.GAME.PLAYER.POSITION - ObjManager.GAME.MONSTER.POSITION;
                    Vector2 monsterdirection = new Vector2(monsterdir.X, monsterdir.Y);
                    float monsterdist = (float)(Math.Sqrt(monsterdirection.X * monsterdirection.X + monsterdirection.Y * monsterdirection.Y) / 50.0f);
                    System.Diagnostics.Debug.WriteLine("MonsterDist: " + monsterdist);
                    if (monsterdist < 0.35)
                    {
                        shouttimer = 3000;
                        ObjManager.GAME.SOUNDMANAGER.playSound("kat_run", position);
                    }
                }
            }

            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.Blue);
            base.Draw(sb);
        }
    }
}
