using System.Collections;
using UnityEditor.Search;
using UnityEngine;
using UnityEngine.InputSystem;

public class Player_Movement : MonoBehaviour
{
[Header("RigidBody")]
[SerializeField]
private Rigidbody2D RB;

[Header("Movement")]
[SerializeField]
private float Player_Speed;

private float horizontal;

[Header("Groundcheckpoint")]
[SerializeField]
private LayerMask GroundLayer;

[SerializeField]
private Transform GroundCheck;

[Header("Player_HitBox")]
[SerializeField]
private LayerMask Hit_Box_Layer;

[SerializeField]
private Transform Hit_Box_Transform;

[Header("Jump")]
[SerializeField]
private float Air_Jump = 0;
[SerializeField]
private float Air_Jump_Remain;
[SerializeField]
private float Jump_Power;

[Header("Dash")]
[SerializeField]
private bool Can_Dash = true;
[SerializeField]
private bool Is_Dash = false;
[SerializeField]
private float Dash_Power = 10f;
[SerializeField]
private float Dash_Time = 0.4f;
[SerializeField]
private float Dash_Cooldown = 0.4f;

// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
    RB = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    if (Hazard() == true)
    {
        transform.localPosition = new Vector2(0, 0);
    }
    if (IsGround() == true)
    {
        Air_Jump_Remain = Air_Jump;
    }
    Debug.Log(transform.localScale);
    StartCoroutine(DASH());
}

private void FixedUpdate()
{
    RB.linearVelocity = new Vector2(horizontal * Player_Speed, RB.linearVelocity.y);
    if (Is_Dash)
    {
        return;
    }
}

public void Move(InputAction.CallbackContext context)
{
    horizontal = context.ReadValue<Vector2>().x;
}

public void Jump(InputAction.CallbackContext context)
{
    //Collider2D collider = Physics2D.OverlapCapsule(GroundCheck.position, new Vector2(1f, 01f), CapsuleDirection2D.Horizontal, 0, GroundLayer);
    if (context.performed && (Air_Jump_Remain >= 1 || IsGround() == true))
    {
        RB.linearVelocity = new Vector2(RB.linearVelocity.x, Jump_Power);
        Air_Jump_Remain -= 1;
    }
}

public void Dash(InputAction.CallbackContext context)
{
    Debug.Log("pressing for dash");
    if (context.performed && Can_Dash == true)
    {
        Debug.Log("StartCoroutine for Dash");
        StartCoroutine(DASH());
    }
}

private bool IsGround()
{
    return Physics2D.OverlapCapsule(GroundCheck.position, new Vector2(1f, 0.1f), CapsuleDirection2D.Horizontal, 0, GroundLayer);
}

private bool Hazard()
{
    return Physics2D.OverlapBox(Hit_Box_Transform.position, new Vector2(0.8f, 0.8f), 0, Hit_Box_Layer);
}

IEnumerator DASH()
{
    Dash_Power = 10f;
    Debug.Log("EInumerator dash 1");
    Can_Dash = false;
    Is_Dash = true;
    float Original_Gravity = RB.gravityScale;
    RB.gravityScale = 0f;
    RB.linearVelocity = new Vector2(transform.localScale.x * Dash_Power, 0f);
    yield return new WaitForSeconds(Dash_Time);
    Debug.Log("EInumerator dash 2");
    Is_Dash = false;
    RB.gravityScale = Original_Gravity;
    yield return new WaitForSeconds(Dash_Cooldown);
    Can_Dash = true;
    Debug.Log("EInumerator dash 3");
}

}