using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Horizontal movement
    private Rigidbody2D rb;
    public float speed = 10;
    public float maxSpeed = 10;
    public float horizontalDrag = 1f;
    private float h = 0f;
    public bool movingLeft = false;
    public bool movingRight = false;
    // Vertical movement
    public float jumpForce = 300;
    public float fallSpeed = 1f;
    public float maxFallSpeed = 10f;
    public Transform groundCheck;
    public bool grounded = false;
    private bool jump = false;
    // Animations
    private bool facingRight = true;

    // Testing
    public float YVelocity = 0f;

    public bool stop = false;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (stop) gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePosition;
        else
        {
            gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
            gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
        }

        grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
        if(Input.GetKeyDown("space") && grounded)
        {
            //jump = true;
            actionJump();
        }
    }

    private void FixedUpdate()
    {
        if (stop) gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePosition;
        else
        {
            gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
            gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
        }

        //float h = Input.GetAxis("Horizontal");

        if (movingLeft) h = -1;
        if (movingRight) h = 1;

        Vector2 movement = new Vector2(h * speed, 0);
        rb.AddForce(movement);

        if (h > 0 && !facingRight)
            Flip();
        else if (h < 0 && facingRight)
            Flip();

        if (jump)
        {
            rb.AddForce(new Vector2(0, jumpForce));
            jump = false;
        }

        // Tight controls
        // If it's falling
        if(rb.velocity.y < 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * fallSpeed);
        }
        if(rb.velocity.y <= -maxFallSpeed)
        {
            rb.velocity = new Vector2(rb.velocity.x, -maxFallSpeed);
        }
        // If decelerating from left
        //if (rb.velocity.x > 0 && !Input.GetKey("right"))
        if (rb.velocity.x > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x * (0.985f / horizontalDrag), rb.velocity.y);
        }
        // If decelerating from right
        //if (rb.velocity.x < 0 && !Input.GetKey("left"))
        if (rb.velocity.x < 0)
        {
            rb.velocity = new Vector2(rb.velocity.x * (0.985f / horizontalDrag), rb.velocity.y);
        }
        h = 0;
    }

    // Flips the sprite
    private void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }

    // Controller access
    public void actionJump()
    {
        if (grounded) jump = true;
    }

    public void actionMoveLeft()
    {
        movingLeft = true;
    }

    public void actionMoveLeftRelease()
    {
        movingLeft = false;
    }

    public void actionMoveRight()
    {
        movingRight = true;
    }

    public void actionMoveRightRelease()
    {
        movingRight = false;
    }

    public void stopPlayer()
    {
        stop = true;
    }
    public void unstopPlayer()
    {
        stop = false;
    }
}
