using System.Collections.Generic;
using UnityEngine;

namespace ScrambleKit.Move
{
    [RequireComponent(typeof(MoveCore))]
    public class TopDownWalk : MonoBehaviour
    {
        public float MoveAcceleration = 40;
        public float StopDeceleration = 50;
        public float SpeedLimit = 6;

        private MoveCore core;
        private Vector2 dir;

        public void FixedUpdate()
        {
            CheckWalkDir();
            if (dir != Vector2.zero)
                Move();
            else
                Stop();
        }

        private void Move()
        {
            core.rb2.AccelTowardVelocityByAtMost(SpeedLimit * dir, MoveAcceleration);
        }

        private void Stop()
        {
            core.rb2.AccelTowardVelocityByAtMost(Vector2.zero, StopDeceleration);
        }

        private void CheckWalkDir()
        {
            dir = Vector2.zero;
            if (core.input.CheckHeld(InputNames.MOVELEFT))
                dir += Vector2.left;
            if (core.input.CheckHeld(InputNames.MOVERIGHT))
                dir += Vector2.right;
            if (core.input.CheckHeld(InputNames.MOVEUP))
                dir += Vector2.up;
            if (core.input.CheckHeld(InputNames.MOVEDOWN))
                dir += Vector2.down;

            dir = dir.normalized;
        }

        void AddBindingNames(HashSet<string> bindingNames)
        {
            bindingNames.Add(InputNames.MOVELEFT);
            bindingNames.Add(InputNames.MOVERIGHT);
            bindingNames.Add(InputNames.MOVEUP);
            bindingNames.Add(InputNames.MOVEDOWN);
        }

        void Start()
        {
            core = GetComponent<MoveCore>();
        }
    }
}
