using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody2D rb;
    private Transform Gr;
    private Animator Ani;
    private Inputs Input;
    private InputAction Walk;
    private InputAction Jump;
    private InputAction Dash;
    private InputAction Aim;
    private ParticleSystem Fart;
    private float x;
    private int usedash;
    private bool jumpin;
    private bool dashin;
    public LayerMask g;
    public Vector2 dashvec;
    public float drag;
    public int dashes;
    public float Speed;
    public float JumpPwr;
    [System.NonSerialized] public Vector2 Overlap;
    private void Awake()
    {
        Overlap = new Vector2(6, 1);
        Input = new Inputs();
        rb = this.gameObject.GetComponent<Rigidbody2D>();
        Gr = this.transform.Find("GroundCheck").transform;
        Ani = this.gameObject.GetComponent<Animator>();
        Fart = this.transform.Find("Fart").GetComponent<ParticleSystem>();
    }
    private void OnEnable()
    {
        Walk = Input.Movement.wasd;
        Jump = Input.Movement.Jump;
        Dash = Input.Movement.Dash;
        Aim = Input.Movement.Aim;
        Walk.Enable();
        Jump.Enable();
        Dash.Enable();
        Aim.Enable();
    }
    private void OnDisable()
    {
        Walk.Disable();
        Jump.Disable();
        Dash.Disable();
        Aim.Disable();
    }
    private void Update()
    {
        int ani = 0;
        Vector2 Force = Walk.ReadValue<Vector2>();
        rb.velocity = new Vector2(x + Force.x * Speed, rb.velocity.y);
        if (Force.x < 0)
        {
            if (x > 0)
            {
                x = -x;
            }
            ani = 1;
            rb.transform.rotation = new Quaternion(0, 180, 0, 0);
        }
        else if (Force.x > 0)
        {
            if (x < 0)
            {
                x = -x;
            }
            ani = 1;
            rb.transform.rotation = new Quaternion(0, 0, 0, 0);
        }
        if (rb.velocity.y < 0 && !IsGrounded())
        {
            ani = 4;
            jumpin = false;
        }
        if (IsGrounded())
        {
            if (x > -0.01 && x < 0.01)
            {
                x = 0;
            }
            else
            {
                x = Mathf.Lerp(x, 0, Time.deltaTime * drag);
            }
            usedash = dashes;
            dashin = false;
        }
        if (Jump.WasPressedThisFrame() || jumpin)
        {
            jump();
            ani = 2;
        }
        if (Dash.WasPressedThisFrame() || dashin)
        {
            dash();
            if (!IsGrounded())
            {
                ani = 5;
            }
        }
        if (Aim.IsPressed())
        {
            ani = 6;
        }
        Ani.SetInteger("Anim", ani);
    }
    private bool IsGrounded()
    {
        return Physics2D.OverlapBox(Gr.position, Overlap, 0f, g);
    }
    private void jump()
    {
        if (IsGrounded())
        {
            jumpin = true;
            rb.velocity = new Vector2(rb.velocity.x, JumpPwr);
        }
    }
    private void dash()
    {
        if (!IsGrounded() && !Fart.isPlaying && usedash != 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + dashvec.y);
            x += dashvec.x;
            if (rb.transform.eulerAngles.y == 180)
            {
                x = -x;
            }
            Fart.Play();
            usedash--;
            dashin = true;
        }
    }
}