using Otter;
using Puppyups.objects;
using Puppyups.scenes;
using Puppyups.Utilities;
using System;
using System.Collections;

namespace Puppyups.entities
{
    class Player : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool _hMove = true;
        private bool _vMove = true;
        public static bool canPlayerCollide = true;
        public static bool isSpritemapChanged = true;
        private BasicMovement movement = new BasicMovement(400, 400, 400);//was 400,400,400
        //private float xoffset = 0;
        //private float yoffset = 20;
        private static int StuckTimerMax = 3;//was 30
        private int _stuckTimer = StuckTimerMax;
        private bool IsL;
        private bool IsR;
        private bool IsU;
        private bool IsD;
        private bool _isShooting;
        private double angleInRadians;
        private float _X;
        private float _Y;
        private float _grappleDragSpeed = 9f;
        private Vector2 velocity;

        public static bool OneTimeGrappleAngle = true;
        public static bool Pulled;

        public Player(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(9, 20, (int)Global.Type.PLAYER);
            Collider.X = -5;
            Collider.Y = -16;
            Axis axis = Axis.CreateWASD();
            movement.Axis = axis;
            movement.Collider = Collider;
            movement.AddCollision((int)Global.Type.SOLID);//walls
            AddComponents(axis, movement);
            LoadCorrectSpritemap();
            _spriteMap.CenterOrigin();
            movement.Speed = Global.PlayerSpeed;
        }

        public override void Update()
        {
            base.Update();
            var cn = Global.PlayerSession.Controller;
            Global.PlayerX = X;
            Global.PlayerY = Y;
            if (cn.Button("R").Down)
            {
                Scene.Add(new Dog(Scene.MouseX, Scene.MouseY));
            }
            if (cn.Button("E").Pressed)//testing!
            {
                //Global.GrappleX = Scene.MouseX;
                //Global.GrappleY = Scene.MouseY;
                //OneTimeGrappleAngle = true;
                //Pulled = true;
                //Global.PlayerHealth = 0;
                //Global.NumberOfShips = 0;
            }
            if (isSpritemapChanged)
                LoadCorrectSpritemap();
            var scenes = Global.GAME.Scenes;//get all scenes             
            foreach (var s in scenes)//find the scene that is active
            {//run checks on that one scene(none of the others)
                if (s == Global.GAME.GetScene<InLab>())
                {
                    if (X >= Global.MW)
                    {
                        Global.GAME.RemoveScene();
                        Global.GAME.AddScene(new InHall());
                    }
                }
                if (s == Global.GAME.GetScene<InHall>())
                {
                    if (X >= Global.MW && Global.IsPlayer1GunCollected && Global.IsPlayer2GunCollected)
                    {
                        Global.GAME.RemoveScene();
                        Global.GAME.AddScene(new Yard());
                    }
                }
            }
            //Console.WriteLine("pulled: {0}", Pulled);
            //Console.WriteLine("_isShooting: {0}", _isShooting);
            if (Pulled)
            {
                movement.Freeze = false;
                movement.SpeedScale = 25;
                /*if (X <= Global.GrappleX)//this is crude, but at least it gets you to the right place.
                    movement.Axis.ForceStateX(9f);//pushes right
                else if (X > Global.GrappleX)
                    movement.Axis.ForceStateX(-9f);//pushes left
                if (Y-5 <= Global.GrappleY)
                    movement.Axis.ForceStateY(9f);//pushes down
                else if (Y-5 > Global.GrappleY)
                    movement.Axis.ForceStateY(-9f);//pushes up
                    */
                
                if (OneTimeGrappleAngle)
                {
                    OneTimeGrappleAngle = false;
                    GetDirection((int)Global.GrappleX, (int)Global.GrappleY, (int)X, (int)Y);//this sets the angle in radians.
                }
                GetVelocity(angleInRadians);
                movement.Axis.ForceState(velocity);

                var gr = Collider.Collide(X, Y, Global.Type.GRAPPLE);
                if (gr != null)
                {
                    Grapple gpl = (Grapple)gr.Entity;
                    gpl.Destroy();
                    Pulled = false;
                    _isShooting = false;
                    Player1Aimer.IsActive = false;
                    movement.Axis.ReleaseState();
                }
                if (cn.Button("J").Pressed)//use this to cancel the pull.
                {
                    var grapples = GetEntities<Grapple>();
                    foreach (var item in grapples)//kill all grapples.
                    {
                        item.Destroy();
                    }
                    Pulled = false;
                    _isShooting = false;
                    Player1Aimer.IsActive = false;
                    movement.Axis.ReleaseState();

                }
            }
            else if (cn.Button("J").Pressed && Player1Aimer.IsActive)//use this to cancel the pull.
                {
                    var grapples = GetEntities<Grapple>();
                    foreach (var item in grapples)//kill all grapples.
                    {
                        item.Destroy();
                    }
                    Pulled = false;
                    _isShooting = false;
                    Player1Aimer.IsActive = false;
                    movement.Axis.ReleaseState();
                
            }
            else
            {
                movement.SpeedScale = 100;
                PressKeysForAnimation(cn, "walkLeft", "walkRight", "walkUp", "walkDown");

                #region Hmovement or Vmovement
                if (cn.Button("A").Down)//LEFT
                    _hMove = true;
                else if (cn.Button("D").Down)//RIGHT
                    _hMove = true;
                else
                    _hMove = false;
                if (cn.Button("W").Down)//UP
                    _vMove = true;
                else if (cn.Button("S").Down)//DOWN
                    _vMove = true;
                else
                    _vMove = false;
                #endregion

                StopWalkingAnimations();
            }
            //collisions:
            var labgun = Collider.Collide(X, Y, (int)Global.Type.LABGUN);
            if (labgun != null)
            {
                Global.GAME.Coroutine.Start(GotGun());
                LabGun l = (LabGun)labgun.Entity;
                Global.IsPlayer1GunCollected = true;
                if (!Global.IsTwoPlayerActive)
                    Global.IsPlayer2GunCollected = true;
                l.Destroy();
            }
        }//end of update
        public static IEnumerator GotGun()
        {
            SoundManager.PlayRequestedSound("_aaaa");
            yield return Coroutine.Instance.WaitForFrames(170);
            SoundManager.PlayRequestedSound("_manwhatwas");
        }

        public void LoadCorrectSpritemap()
        {
            isSpritemapChanged = false;
            if (_spriteMap != null)
                _spriteMap.Clear();
            //if no other spritemaps are needed, this will work fine.
            _spriteMap = null;
            _spriteMap = new Spritemap<string>(Assets.PLAYER, 50, 50);
            AddNewSprites();
            /* if (!Global.doesPlayerHaveWagon)
             {
                 _spriteMap = null;
                 _spriteMap = new Spritemap<string>(Assets.PLAYER, 45, 50);
                 AddNewSprites();
             }
             else if (Global.doesPlayerHaveWagon)
             {
                 _spriteMap = null;
                 _spriteMap = new Spritemap<string>(Assets.PLAYERPULLSWAGON, 45, 50);
                 AddPlayerWagonSprites();
             }*/
            _spriteMap.Update();
            _spriteMap.CenterOrigin();
            _spriteMap.Play("standDown");
            _spriteMap.FlippedX = false;
            Graphic = _spriteMap;
        }

        #region AddSprites
        public void AddNewSprites()
        {
            var hMs = 6f;//horizontal animation speed
            var vMs = 6f;//vertical animation speed
            _spriteMap.Add("standRight", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standLeft", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standUp", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standDown", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkRight", new int[] { 1, 2, 3, 4 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkLeft", new int[] { 1, 2, 3, 4 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkUp", new int[] { 1, 2, 3, 4 }, new float[] { vMs, vMs });
            _spriteMap.Add("walkDown", new int[] { 1, 2, 3, 4 }, new float[] { vMs, vMs });
            _spriteMap.Add("shootRight", new int[] { 5 }, new float[] { hMs, hMs });
            _spriteMap.Add("shootLeft", new int[] { 5 }, new float[] { hMs, hMs });
            _spriteMap.Add("shootUp", new int[] { 7 }, new float[] { hMs, hMs });
            _spriteMap.Add("shootDown", new int[] { 9 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkShootRight", new int[] { 5, 6 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkShootLeft", new int[] { 5, 6 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkShootUp", new int[] { 7, 8 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkShootDown", new int[] { 9, 10 }, new float[] { hMs, hMs });

            _spriteMap.Add("death", new int[] { 1, 5, 1, 7, 2, 0, 3, 5, 1, 6, 7 }, new float[] { 16, 16 });
        }
        /*public void AddPlayerWagonSprites()
        {
            var hMs = 1f;//horizontal animation speed
            var vMs = 1f;//vertical animation speed
            _spriteMap.Add("standRight", new int[] { 1 }, new float[] { hMs, hMs });
            _spriteMap.Add("standLeft", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standUp", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standDown", new int[] { 1 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkRight", new int[] { 12, 11, 10, 19, 18, 17, 16, 15 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkLeft", new int[] { 2, 3, 4, 5, 6, 7, 8, 9 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkUp", new int[] { 2, 3, 4, 5, 6, 7, 8, 9 }, new float[] { vMs, vMs });
            _spriteMap.Add("walkDown", new int[] { 12, 11, 10, 19, 18, 17, 16, 15 }, new float[] { vMs, vMs });
            _spriteMap.Add("defendRight", new int[] { 14 }, new float[] { vMs, vMs });
            _spriteMap.Add("defendLeft", new int[] { 13 }, new float[] { vMs, vMs });

            _spriteMap.Add("pickup", new int[] { 16, 16, 16 }, new float[] { vMs, vMs });
            _spriteMap.Add("dropoff", new int[] { 16, 16, 16 }, new float[] { vMs, vMs });
            _spriteMap.Add("hit", new int[] { 1, 2, 6, 5 }, new float[] { vMs, vMs });
        }*/
        #endregion

        public void PressKeysForAnimation(Controller cn, string l, string r, string u, string d)
        {
            if (_isShooting)
                movement.Freeze = true;
            else
                movement.Freeze = false;
            #region pressed Keys for animation
            if (cn.Button("A").Pressed && Global.IsPlayer1GunCollected)//LEFT with gun
            {
                if (movement.Freeze)
                    Shared.ChangeAnimTo("shootLeft", _spriteMap);
                else
                    Shared.ChangeAnimTo("walkShootLeft", _spriteMap);
                _spriteMap.FlippedX = true;
                IsL = true; IsR = false; IsU = false; IsD = false;
            }
            else if (cn.Button("D").Pressed && Global.IsPlayer1GunCollected)//RIGHT with gun
            {
                if (movement.Freeze)
                    Shared.ChangeAnimTo("shootRight", _spriteMap);
                else
                    Shared.ChangeAnimTo("walkShootRight", _spriteMap);
                _spriteMap.FlippedX = false;
                IsL = false; IsR = true; IsU = false; IsD = false;
            }
            else if (cn.Button("W").Pressed && Global.IsPlayer1GunCollected)//UP with gun
            {
                if (movement.Freeze)
                    Shared.ChangeAnimTo("shootUp", _spriteMap);
                else
                    Shared.ChangeAnimTo("walkShootUp", _spriteMap);
                _spriteMap.FlippedX = false;
                IsL = false; IsR = false; IsU = true; IsD = false;
            }
            else if (cn.Button("S").Pressed && Global.IsPlayer1GunCollected)//DOWN with gun
            {
                if (movement.Freeze)
                    Shared.ChangeAnimTo("shootDown", _spriteMap);
                else
                    Shared.ChangeAnimTo("walkShootDown", _spriteMap);
                _spriteMap.FlippedX = false;
                IsL = false; IsR = false; IsU = false; IsD = true;
            }
            else if (cn.Button("A").Pressed)//LEFT
            {
                Shared.ChangeAnimTo(l, _spriteMap);
                _spriteMap.FlippedX = true;
                IsL = true; IsR = false; IsU = false; IsD = false;
            }
            else if (cn.Button("D").Pressed)//RIGHT
            {
                Shared.ChangeAnimTo(r, _spriteMap);
                _spriteMap.FlippedX = false;
                IsL = false; IsR = true; IsU = false; IsD = false;
            }
            else if (cn.Button("W").Pressed)//UP
            {
                Shared.ChangeAnimTo(u, _spriteMap);
                _spriteMap.FlippedX = true;
                IsL = false; IsR = false; IsU = true; IsD = false;
            }
            else if (cn.Button("S").Pressed)//DOWN
            {
                Shared.ChangeAnimTo(d, _spriteMap);
                _spriteMap.FlippedX = false;
                IsL = false; IsR = false; IsU = false; IsD = true;
            }

            #endregion
            #region released keys for animation
            if (cn.Button("A").Released && Global.IsPlayer1GunCollected)//if i release left, updownright
            {
                if (cn.Button("W").Down)//and i'm holding up
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootUp", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootUp", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("S").Down)//and i'm holding down
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootDown", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootDown", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("D").Down)//and i'm holding right
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootRight", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootRight", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            else if (cn.Button("D").Released && Global.IsPlayer1GunCollected)//if i release right, updownleft
            {
                if (cn.Button("W").Down)//and i'm holding up
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootUp", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootUp", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("S").Down)//and i'm holding down
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootDown", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootDown", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("A").Down)//and i'm holding left
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootLeft", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootLeft", _spriteMap);
                    _spriteMap.FlippedX = true;
                }
            }
            else if (cn.Button("W").Released && Global.IsPlayer1GunCollected)//if i release up, rightleftdown
            {
                if (cn.Button("D").Down)//and i'm holding right
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootRight", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootRight", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("A").Down)//and i'm holding left
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootLeft", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootLeft", _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else if (cn.Button("S").Down)//and i'm holding down
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootDown", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootDown", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            else if (cn.Button("S").Released && Global.IsPlayer1GunCollected)//if i release down, rightleftup
            {
                if (cn.Button("D").Down)//and i'm holding right
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootRight", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootRight", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("A").Down)//and i'm holding left
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootLeft", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootLeft", _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else if (cn.Button("W").Down)//and i'm holding up
                {
                    if (movement.Freeze)
                        Shared.ChangeAnimTo("shootUp", _spriteMap);
                    else
                        Shared.ChangeAnimTo("walkShootUp", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            else if (cn.Button("A").Released)//if i release left, updownright
            {
                if (cn.Button("W").Down)//and i'm holding up
                {
                    Shared.ChangeAnimTo(u, _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else if (cn.Button("S").Down)//and i'm holding down
                {
                    Shared.ChangeAnimTo(d, _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("D").Down)//and i'm holding right
                {
                    Shared.ChangeAnimTo(r, _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            else if (cn.Button("D").Released)//if i release right, updownleft
            {
                if (cn.Button("W").Down)//and i'm holding up
                {
                    Shared.ChangeAnimTo(u, _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else if (cn.Button("S").Down)//and i'm holding down
                {
                    Shared.ChangeAnimTo(d, _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("A").Down)//and i'm holding left
                {
                    Shared.ChangeAnimTo(l, _spriteMap);
                    _spriteMap.FlippedX = true;
                }
            }
            else if (cn.Button("W").Released)//if i release up, rightleftdown
            {
                if (cn.Button("D").Down)//and i'm holding right
                {
                    Shared.ChangeAnimTo(r, _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("A").Down)//and i'm holding left
                {
                    Shared.ChangeAnimTo(l, _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else if (cn.Button("S").Down)//and i'm holding down
                {
                    Shared.ChangeAnimTo(d, _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            else if (cn.Button("S").Released)//if i release down, rightleftup
            {
                if (cn.Button("D").Down)//and i'm holding right
                {
                    Shared.ChangeAnimTo(r, _spriteMap);
                    _spriteMap.FlippedX = false;
                }
                else if (cn.Button("A").Down)//and i'm holding left
                {
                    Shared.ChangeAnimTo(l, _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else if (cn.Button("W").Down)//and i'm holding up
                {
                    Shared.ChangeAnimTo(u, _spriteMap);
                    _spriteMap.FlippedX = true;
                }
            }
            #endregion

            if (Global.IsPlayer1GunCollected)
            {
                if (IsL && cn.Button("J").Pressed && !Player1Aimer.IsActive)//LEFT
                {
                    //Scene.Add(new Grapple(X, Y, "Left", this));
                    Scene.Add(new Player1Aimer("left"));
                    _isShooting = true;
                }
                if (IsR && cn.Button("J").Pressed && !Player1Aimer.IsActive)//RIGHT
                {
                    Scene.Add(new Player1Aimer("right"));
                    //Scene.Add(new Grapple(X, Y, "Right", this));
                    _isShooting = true;
                }
                if (IsU && cn.Button("J").Pressed && !Player1Aimer.IsActive)//UP
                {
                    Scene.Add(new Player1Aimer("up"));
                    //Scene.Add(new Grapple(X, Y, "Up", this));
                    _isShooting = true;
                }
                if (IsD && cn.Button("J").Pressed && !Player1Aimer.IsActive)//DOWN
                {
                    Scene.Add(new Player1Aimer("down"));
                    //Scene.Add(new Grapple(X, Y, "Down", this));
                    _isShooting = true;
                }
            }
        }

        public void StopWalkingAnimations()
        {
            if (!_hMove && !_vMove)
            {
                switch (_spriteMap.CurrentAnim)
                {
                    case ("walkLeft"):
                        _spriteMap.Play("standLeft");
                        _spriteMap.FlippedX = true;
                        break;
                    case ("walkRight"):
                        _spriteMap.Play("standRight");
                        _spriteMap.FlippedX = false;
                        break;
                    case ("walkUp"):
                        _spriteMap.Play("standUp");
                        _spriteMap.FlippedX = true;
                        break;
                    case ("walkDown"):
                        _spriteMap.Play("standDown");
                        _spriteMap.FlippedX = false;
                        break;
                    case ("walkShootLeft"):
                        _spriteMap.Play("shootLeft");
                        _spriteMap.FlippedX = true;
                        break;
                    case ("walkShootRight"):
                        _spriteMap.Play("shootRight");
                        _spriteMap.FlippedX = false;
                        break;
                    case ("walkShootUp"):
                        _spriteMap.Play("shootUp");
                        _spriteMap.FlippedX = false;
                        break;
                    case ("walkShootDown"):
                        _spriteMap.Play("shootDown");
                        _spriteMap.FlippedX = false;
                        break;
                    default:
                        break;
                }
            }
        }

        public void GetDirection(int destX, int destY, int sourceX, int sourceY)
        {
            var vector1 = new Point(sourceX, sourceY);
            var vector2 = new Point(destX, destY);
            angleInRadians = Math.Atan2(vector2.Y - vector1.Y, vector2.X - vector1.X);
            var angleInFrisbees = MathHelper.ToDegrees((float)angleInRadians);
            if (angleInFrisbees < 0)
            {
                angleInFrisbees += 360;
                angleInRadians = MathHelper.ToRadians(angleInFrisbees);
            }
        }

        private void GetVelocity(double originalAngle)//these speeds are not variable enough. :/
        {
            var angle = (int)MathHelper.ToDegrees((float)originalAngle);
            //Console.WriteLine("angle: {0}", angle);
            _X = _grappleDragSpeed * (float)Math.Cos(originalAngle);
            _Y = _grappleDragSpeed * (float)Math.Sin(originalAngle);
            velocity = new Vector2(_X, _Y);
        }

        public override void Render()//mainly for testing colliders :D
        {
            base.Render();
            //Collider.Render();
        }

        public void Destroy()
        {
            RemoveSelf();
        }
    }
}
