using ECM.Controllers;
using Rewired;
using UnityEngine;

namespace GGJ2021
{
    public class PlayerMovementController : BaseCharacterController
    {
        private Player _player;
        private Transform _cameraT;

        public override void Awake()
        {
            base.Awake();
            _player = ReInput.players.GetPlayer(0);
            restoreVelocityOnResume = false;

#if !UNITY_EDITOR
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
#endif
        }

        private void Start()
        {
            _cameraT = Camera.main.transform;
        }

        public override void Update()
        {
            base.Update();

            if (_player.GetButtonDown("Quit"))
            {
#if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
            }
        }

        protected override void HandleInput()
        {
            moveDirection = new Vector3
            {
                x = _player.GetAxisRaw("Horizontal"),
                y = 0f,
                z = _player.GetAxisRaw("Vertical"),
            };
            moveDirection.Normalize();

            // Make input relative to camera
            moveDirection = _cameraT.TransformDirection(moveDirection);
            moveDirection = new Vector3(moveDirection.x, 0f, moveDirection.z);
        }

    }

}
