using System;
using Otter;

namespace KellysMarvelousAdventure.entities
{
    public class Oldman : 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 = 100;

        public Oldman(float x, float y, bool menu = false)
        {
            X = x; Y = y;
            SetHitbox(10, 30, (int)Global.Type.OLDMAN);
            Collider.X = -7;
            Collider.Y = -13;
            Axis axis = Axis.CreateArrowKeys();
            movement.Axis = axis;
            movement.Collider = Collider;
            movement.AddCollision((int)Global.Type.SOLID);//walls
            AddComponents(axis, movement);
            LoadCorrectSpritemap();
            _spriteMap.CenterOrigin();
            movement.Speed = Global.Player2Speed;
            if (!menu)
                Layer = 3;
        }
        public override void UpdateFirst()//this is so that when the player moves the camera, the UI will update after it moves. (but the player will still appear below the elements. (it's an update thing)
        {
            base.UpdateFirst();
            //#region Camera does not follow oldman
            //if (X > Scene.HalfWidth - xoffset && X < Global.MW - Scene.HalfWidth - xoffset)//this is now universal. haha!
            //    Scene.CameraX = X - Scene.HalfWidth + xoffset;//this does follow the player!
            //else if (X <= Scene.HalfWidth - xoffset)
            //    Scene.CameraX = 0;
            //else if (X >= Global.MW - (Scene.HalfWidth * 2))
            //    Scene.CameraX = Global.MW - (Scene.HalfWidth * 2);
            //if (Y > Scene.HalfHeight && Y < Global.MH - Scene.HalfHeight)
            //    Scene.CameraY = Y - Scene.HalfHeight;//this does follow the player!
            //else if (Y <= Scene.HalfHeight)
            //    Scene.CameraY = 0;
            //else if (Y >= Global.MH - Scene.HalfHeight * 2)
            //    Scene.CameraY = Global.MH - Scene.HalfHeight * 2;
            //#endregion
        }
        public override void Update()
        {
            base.Update();
            #region override edges
            if (X < 4)
                X = 4;
            if (X > Global.MW - 4)
                X = Global.MW - 4;
            if (Y < 4)
                Y = 4;
            if (Y > Global.MH - 4)
                Y = Global.MH - 4;
            #endregion
            var cn = Global.Player2Session.Controller;
            Global.Player2X = X;
            Global.Player2Y = Y;

            PressKeysForAnimation(cn, "walkLeft", "walkRight", "walkUp", "walkDown");

            #region Hmovement or Vmovement
            if (cn.Button("Left").Down)//LEFT
                _hMove = true;
            else if (cn.Button("Right").Down)//RIGHT
                _hMove = true;
            else
                _hMove = false;
            if (cn.Button("Up").Down)//UP
                _vMove = true;
            else if (cn.Button("Down").Down)//DOWN
                _vMove = true;
            else
                _vMove = false;
            #endregion
            StopWalkingAnimations();
        }

        public void StopWalkingAnimations()
        {
            if (!_hMove && !_vMove)
            {
                switch (_spriteMap.CurrentAnim)
                {
                    case ("walkLeft"):
                        _spriteMap.Play("standLeft");
                        break;
                    case ("walkRight"):
                        _spriteMap.Play("standRight");
                        break;
                    case ("walkUp"):
                        _spriteMap.Play("standUp");
                        break;
                    case ("walkDown"):
                        _spriteMap.Play("standDown");
                        break;
                    default:
                        break;
                }
            }
        }
        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }
        public void LoadCorrectSpritemap()
        {
            isSpritemapChanged = false;
            _spriteMap = null;
            _spriteMap = new Spritemap<string>(Assets.OLDMAN, 45, 50);
            AddNewSprites();
            _spriteMap.Update();
            _spriteMap.Play("standRight");
            Graphic = _spriteMap;
        }

        #region AddSprites
        public void AddNewSprites()
        {
            var hMs = 5f;//horizontal animation speed
            var vMs = 5f;//vertical animation speed
            _spriteMap.Add("standRight", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standLeft", new int[] { 1 }, 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[] { 0, 1 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkLeft", new int[] { 0, 1 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkUp", new int[] { 0, 1 }, new float[] { vMs, vMs });
            _spriteMap.Add("walkDown", new int[] { 0, 1 }, 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)
        {
            #region pressed Keys for animation
            if (cn.Button("Left").Pressed)//LEFT
                _spriteMap.Play(l);
            else if (cn.Button("Right").Pressed)//RIGHT
                _spriteMap.Play(r);
            else if (cn.Button("Up").Pressed)//UP
                _spriteMap.Play(u);
            else if (cn.Button("Down").Pressed)//DOWN
                _spriteMap.Play(d);
            #endregion

            #region released keys for animation
            if (cn.Button("Left").Released)//if i release left, updownright
            {
                if (cn.Button("Up").Down)//and i'm holding up
                    _spriteMap.Play(u);
                else if (cn.Button("Down").Down)//and i'm holding down
                    _spriteMap.Play(d);
                else if (cn.Button("Right").Down)//and i'm holding right
                    _spriteMap.Play(r);
            }
            else if (cn.Button("Right").Released)//if i release right, updownleft
            {
                if (cn.Button("Up").Down)//and i'm holding up
                    _spriteMap.Play(u);
                else if (cn.Button("Down").Down)//and i'm holding down
                    _spriteMap.Play(d);
                else if (cn.Button("Left").Down)//and i'm holding left
                    _spriteMap.Play(l);
            }
            else if (cn.Button("Up").Released)//if i release up, rightleftdown
            {
                if (cn.Button("Right").Down)//and i'm holding right
                    _spriteMap.Play(r);
                else if (cn.Button("Left").Down)//and i'm holding left
                    _spriteMap.Play(l);
                else if (cn.Button("Down").Down)//and i'm holding down
                    _spriteMap.Play(d);
            }
            else if (cn.Button("Down").Released)//if i release down, rightleftup
            {
                if (cn.Button("Right").Down)//and i'm holding right
                    _spriteMap.Play(r);
                else if (cn.Button("Left").Down)//and i'm holding left
                    _spriteMap.Play(l);
                else if (cn.Button("Up").Down)//and i'm holding up
                    _spriteMap.Play(u);
            }
            #endregion
        }

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

        public void Destroy()
        {
            RemoveSelf();
        }

    }
}

