using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    public float velocity = 0.1f;
    private float originalVelocity;
    
    private Rigidbody2D rb;
    private Transform graphics;
    private Animator animator;
    private Vector2 input;
    private Vector2 lastInput;

    private bool facingRight;
    [HideInInspector] public bool canMove;
    [HideInInspector] public bool isSliding;
    
    [Header("Input Actions")]
    public InputActionReference moveAction;
    public InputActionReference escAction;

    private void OnEnable()
    {
        moveAction.action.Enable();
        escAction.action.Enable();
    }

    private void OnDisable()
    {
        moveAction.action.Disable();
        escAction.action.Disable();
    }
    
    void Awake()
    {
        // used to go back to this once sliding ends
        originalVelocity = velocity;
        
        rb = GetComponent<Rigidbody2D>();
        graphics = transform.GetChild(0);
        animator = graphics.GetComponent<Animator>();
        
        facingRight = true;
        canMove = true;
        isSliding = false;
    }

    // Update is called once per frame
    void Update()
    {
        HandleMenuInputs();
        
        input = moveAction.action.ReadValue<Vector2>();
        
        // if you can't move, don't do anything
        if (!canMove)
        {
            input = Vector2.zero;
        }
        // if you are sliding, maintain momentum
        if (isSliding)
        {
            input = lastInput;
        }
        
        if (input.sqrMagnitude > 1)
            input = input.normalized;
        
        if (input.x > 0 && !facingRight) {
            Flip();
        }
        else if (input.x < 0 && facingRight) {
            Flip();
        }
        
        // If not moving, don't play running animation
        bool isActuallyMoving = rb.linearVelocity.sqrMagnitude > 0;
        animator.SetBool("is_running", isActuallyMoving);
        
    }

    private void Flip()
    {
        facingRight = !facingRight;
        Vector3 tempLocal = graphics.localScale;
        tempLocal.x *= -1;
        graphics.localScale = tempLocal;
    }

    public void endSlide()
    {
        velocity = originalVelocity;
        rb.linearVelocity = Vector2.zero;
        isSliding = false;
    }

    private void FixedUpdate()
    {
        if (isSliding) velocity *= 0.95f;
        rb.linearVelocity = input * velocity;
    }

    private void LateUpdate()
    {
        lastInput = input;
    }

    private void HandleMenuInputs()
    {
        if (GameManager.Instance == null) return;
        //Escape is for pausing the Menu
        if ((GameManager.Instance.CurrentState == GameState.Playing || GameManager.Instance.CurrentState == GameState.Tutorial) && escAction.action.WasPressedThisFrame())
        {
            GameManager.Instance.PauseGame();
        } else if (GameManager.Instance.CurrentState == GameState.Paused && escAction.action.WasPressedThisFrame())
        {
            GameManager.Instance.ResumeGame();
        }
    }
}
