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

[RequireComponent(typeof(PlayerInput))]
public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;
    private Rigidbody2D rb;

    private bool right = false;

    [Header("Whipping")]
    [SerializeField]
    //RopeWhipAttack whipAttack;
    public float whipSmashSpeed = 2f;
    public float whipSmashDamageMult = 2f;

    [SerializeField]
    private float whipMoveSpeed = 25f;

    [SerializeField]
    private float maxWhipMoveSpeed = 30f;

    private IMoveData playerInput;

    private HealthComponent hp;
    private PlayerCollideAttack attack;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        hp = GetComponent<HealthComponent>();
        attack = GetComponent<PlayerCollideAttack>();

        // Try to get middleman first
        if (TryGetComponent(out ReconciliationPlayerControllerMiddleman middleman))
        {
            playerInput = middleman;
        }
        else
        {
            Debug.LogWarning("[Network][Movement] Could not find input middleman. Defaulting back to normal player input");
            playerInput = GetComponent<PlayerInput>();
        }

        playerInput.OnNewMoveData += PlayerInput_OnNewMoveData;

    }

    private void PlayerInput_OnNewMoveData(MoveData moveData)
    {
        rb.AddForce(moveData.Movement * moveSpeed);
    }

    void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.collider.gameObject.CompareTag("Enemy"))
        { // Other object is an enemy

            hp.TakeDamage(1f);

        }
    }
}