using System;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using NodeCanvas;
using NodeCanvas.Framework;
using ParadoxNotion;
using Sirenix.OdinInspector;

public class PlayerControls : MonoBehaviour{
    // References
    private Player thisPlayer;

    private PlayerInput input;
    public SignalDefinition inputSignal;
    
    // TODO: Turn these into accessors
    public Vector2 rawMoveInput;
    public Vector2 rawLookInput;

    public GraphOwner graph;

    void Awake(){
        thisPlayer = GetComponent<Player>();
        graph = GetComponent<GraphOwner>();
        input = GetComponent<PlayerInput>();

        // Add the delegates for each method
        foreach (InputAction action in input.actions) {
            action.started += SendToGraph;
            action.canceled += SendToGraph;
            action.performed += SendToGraph;
        }
    }

    // Remove the delegates for each method
    void OnDisable(){
        foreach (InputAction action in input.actions) {
            action.started -= SendToGraph;
            action.canceled -= SendToGraph;
            action.performed -= SendToGraph;
        }
    }

    // This will call the OnSignalInvoke for this type of Signal Defintion. CheckInput is the recieving end.
    public void SendToGraph(InputAction.CallbackContext ctx){
        inputSignal.Invoke(transform, transform, false, new object[]{
            ctx.action,
            ctx.phase
        });
    }

    public void OnMove(InputValue value){
        rawMoveInput.x = value.Get<Vector2>().x;
        rawMoveInput.y = value.Get<Vector2>().y;
    }

    public void OnLook(InputValue value){
        rawLookInput.x = value.Get<Vector2>().x;
        rawLookInput.y = value.Get<Vector2>().y;
    }

    public void OnSprint(){
        graph.SendEvent<string>("InputEvent", "Sprint", null);
    }

    public void OnJump(){
        graph.SendEvent<string>("InputEvent", "Jump", null);
    }

    public void OnLockOn(){
        GetComponent<LockOnManager>().ChangeLockOnTarget();
        graph.SendEvent<string>("InputEvent", "LockOn", null);
    }

    public void OnCancelLockOn(){
        GetComponent<LockOnManager>().RemoveMainTarget();
        graph.SendEvent<string>("InputEvent", "CancelLockOn", null);
    }

    // public void OnGrapple(InputInteractionContext context){
    //     if (context.control.IsPressed()) {
    //         graph.SendEvent<string>("InputEvent", "GrappleDown", null);    
    //     } else {
    //         graph.SendEvent<string>("InputEvent", "GrappleUp", null);
    //     }
    // }
}
