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

public class PlayerBehavior : MonoBehaviour
{

    [Header("Physics:")]
    public float moveSpeed;
    public float jumpSpeed;
    public float airSpeed;
    private float _hInput;
    private Rigidbody2D _rb;
    private int forward = 1;
    public PlayerInput playerInput;

    public LayerMask groundLayer;
    public Vector2 boxSize;
    public float maxDistanceFromGround;

    [Header("Tambourine:")]
    [SerializeField] private Launch launcher;
    [HideInInspector] public bool hasTambourine = true;
    GameObject tambourine;

    [Header("Grappling:")]
    [SerializeField] public Tutorial_GrapplingGun grapplingGun;
    [SerializeField] public Tutorial_GrapplingRope grapplingRope;
    private GameObject grappleSurface;

    [Header("Controllers:")]
    [SerializeField] private PlayerMovement playerController;
    [SerializeField] private StateController stateController;


    void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
        airSpeed = .5f * moveSpeed;
        stateController = GameObject.Find("StateController").GetComponent<StateController>();
    }

    void Update()
    {

        // throw tambourine
        // if (Input.GetKeyDown(KeyCode.K)) {
        if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
        {
            ThrowTambourine();
        }

        // grapple
        tambourine = GameObject.FindGameObjectWithTag("tambourine");
        // if (Input.GetKeyDown(KeyCode.L)) {
        if (playerInput.actions["Grapple"].WasPressedThisFrame())
        {
            AttemptGrapple();
        }
        // if (Input.GetKeyUp(KeyCode.L)) {
        if (playerInput.actions["Grapple"].WasReleasedThisFrame())
        {
            LetGoOfGrapple();
        }
    }

    void OnMove(InputValue value)
    {
        _hInput = value.Get<Vector2>().x;
        if (_hInput < 0)
        {
            forward = -1;
        }
        else if (_hInput > 0)
        {
            forward = 1;
        }
    }

    void ThrowTambourine() {
        if (hasTambourine && !grapplingRope.isGrappling)
            {
                launcher.ThrowTambourine(forward);
                hasTambourine = false;
            }
    }

    void AttemptGrapple() {
        if (tambourine != null)
        { // grapple to tambourine
            if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
            {
                grapplingGun.GrappleToTambourine(tambourine);
                grapplingRope.isGrappling = true;
            }
        }
        else
        {
            if (grappleSurface != null)
            {
                grapplingGun.GrappleToSurface(grappleSurface.transform.position);
                grapplingRope.isGrappling = true;
            }
        }
    }

    void LetGoOfGrapple() {
        if (tambourine != null && grapplingRope.isGrappling)
            {
                tambourine.GetComponent<TambourineBehavior>().DestroySelf();
            }
            grapplingGun.ReleaseGrapple();
    }
}
