using Otter;

namespace KellysMarvelousAdventure.entities
{
    class RedWagon : Entity
    {
        private Spritemap<string> _spriteMap;

        public RedWagon(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(20, 20, (int)Global.Type.WAGON);
            Collidable = false;
            _spriteMap = new Spritemap<string>(Assets.REDWAGON, 20, 20);
            _spriteMap.Add("right", new int[] { 0 }, new float[] { 3, 3 });
            _spriteMap.Add("down", new int[] { 0 }, new float[] { 3, 3 });
            _spriteMap.Add("guardDown", new int[] { 4 }, new float[] { 3, 3 });
            _spriteMap.Add("guardLeft", new int[] { 1 }, new float[] { 3, 3 });
            _spriteMap.Add("guardUp", new int[] { 2 }, new float[] { 3, 3 });
            _spriteMap.Add("guardRight", new int[] { 3 }, new float[] { 3, 3 });
            _spriteMap.Add("left", new int[] { 4 }, new float[] { 3, 3 });
            _spriteMap.Add("up", new int[] { 4 }, new float[] { 3, 3 });
            _spriteMap.Play("right");
            Graphic = _spriteMap;
            Layer = 1;
        }
        public override void Update()
        {
            base.Update();
            if (Global.doesPlayerHaveWagon)
            {
                var cn = Global.PlayerSession.Controller;
                if (cn.Button("RightMouseClick").Down)
                {
                    Collider.X = -7;
                    Collider.Y = -7;
                    Collider.Width = 25;
                    Collider.Height = 25;
                    Collidable = true;
                    if (_spriteMap.CurrentAnim == "right")
                    {
                        ChangeAnimTo("guardRight");
                        X = Global.PlayerX;
                        Y = Global.PlayerY-10;
                    }
                    else if (_spriteMap.CurrentAnim == "left")
                    {
                        ChangeAnimTo("guardLeft");
                        X = Global.PlayerX - 13;
                        Y = Global.PlayerY-9;
                    }
                    else if (_spriteMap.CurrentAnim == "up")
                    {
                        ChangeAnimTo("guardUp");
                        X = Global.PlayerX - 5;
                        Y = Global.PlayerY - 15;
                    }
                    else if (_spriteMap.CurrentAnim == "down")
                    {
                        ChangeAnimTo("guardDown");
                        X = Global.PlayerX - 5;
                        Y = Global.PlayerY + 7;
                    }
                }
                else
                {
                    Collidable = false;
                    if (cn.Button("A").Pressed || _spriteMap.CurrentAnim == "guardLeft")
                        ChangeAnimTo("left");
                    if (cn.Button("W").Pressed || _spriteMap.CurrentAnim == "guardUp")
                        ChangeAnimTo("up" );
                    if (cn.Button("S").Pressed || _spriteMap.CurrentAnim == "guardDown")
                        ChangeAnimTo("down");
                    if (cn.Button("D").Pressed || _spriteMap.CurrentAnim == "guardRight")
                        ChangeAnimTo("right");
                    if (_spriteMap.CurrentAnim == "down")
                    {
                        X = Global.PlayerX - 25;
                        Y = Global.PlayerY;
                    }
                    else if (_spriteMap.CurrentAnim == "left")
                    {
                        X = Global.PlayerX + 5;
                        Y = Global.PlayerY;
                    }
                    else if (_spriteMap.CurrentAnim == "up")
                    {
                        X = Global.PlayerX + 5;
                        Y = Global.PlayerY;
                    }
                    else if (_spriteMap.CurrentAnim == "right")
                    {
                        X = Global.PlayerX - 25;
                        Y = Global.PlayerY;
                    }
                }
            }
        }
        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }
        public override void Render()
        {
            base.Render();
            //Collider.Render();
        }
    }
}
