using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    public float speed;
    public float groundDist;
    public LayerMask ground;
    public Rigidbody rb;
    public SpriteRenderer sr;
    public InputActionReference move;
    [SerializeField] private Animator animator;
    private bool hasMoved;
    private bool canMove;



    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();
        hasMoved = false;
        animator.SetBool("moving", false);
        canMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        Vector3 castPos = transform.position;
        castPos.y += 1;
        if(Physics.Raycast(castPos, -transform.up, out hit, Mathf.Infinity, ground))
        {
            if(hit.collider!=null)
            {
                Vector3 movePos = transform.position;
                movePos.y = hit.point.y + groundDist;
                transform.position = movePos;
            }
        }

        if(canMove)
        {
            float x = move.action.ReadValue<Vector2>().x;
            float y = move.action.ReadValue<Vector2>().y;
            Vector3 moveDir = new Vector3(x,0,y);
            rb.linearVelocity = moveDir * speed;

            if(rb.linearVelocity != new Vector3(0,0,0)) animator.SetBool("moving", true);
            else animator.SetBool("moving", false);


            if(x!=0 && x<0)
            {
                sr.flipX = true;
                if(!hasMoved)
                {
                    animator.SetBool("facingSide", true);
                    animator.SetBool("facingBack", false);
                    hasMoved = true;
                }
            }
            else if(x!=0 && x>0)
            {
                sr.flipX = false;
                if(!hasMoved)
                {
                    animator.SetBool("facingSide", true);
                    animator.SetBool("facingBack", false);
                    hasMoved = true;
                }
            }
            else if(x==0 && y!=0)
            {
                hasMoved = false;
                animator.SetBool("facingSide", false);
            }
            if(y!=0 && y<0)
            {
                if(!hasMoved)
                {
                    animator.SetBool("facingSide", false);
                    animator.SetBool("facingBack", false);
                    hasMoved = true;
                }
            }
            else if(y!=0 && y>0)
            {
                if(!hasMoved)
                {
                    animator.SetBool("facingSide", false);
                    animator.SetBool("facingBack", true);
                    hasMoved = true;
                }
            }
            else if(y==0)
            {
                hasMoved = false;
            }
        }

    }

    public void DisableControls()
    {
        canMove = false;
        Debug.Log("controls disabled");
    }
    public void EnableControls()
    {
        canMove = true;
        Debug.Log("controls enabled");
    }

    public void SetFacingBackwards()
    {
        animator.SetBool("facingSide", false);
        animator.SetBool("facingBack", true);
        animator.Play("IdleBack");
    }

    public void SetFacingSide(bool dir)
    {
        animator.SetBool("facingSide", true);
        animator.SetBool("facingBack", false);
        animator.Play("IdleSide");
        sr.flipX = dir;
    }

    public void GoToSandbox()
    {
        gameObject.transform.position = new Vector3(1.80900002f,0.75999999f,0.973999977f);
    }
}
