using System;
using System.Collections.Generic;
using NodeCanvas.Framework;
using ParadoxNotion;
using ParadoxNotion.Design;
using UnityEngine;
using UnityEngine.InputSystem;

namespace NodeCanvas.Tasks.Conditions {
	[Category("Reset/Input")]
	[Description("Check if input condition was matched this frame by phase.")]
	public class CheckInput : ConditionTask<Transform>{
		public BBParameter<string> actionName;
		public BBParameter<InputActionPhase> actionPhase;
		
		private SignalDefinition signalDefinition;
		protected override string info {
			get { return $"Input <b>{actionName.value}</b> was <b>{actionPhase.value}</b>"; }
		}
		
		protected override string OnInit(){
			try {
				signalDefinition = Resources.Load<SignalDefinition>("InputSignal");
			} catch (Exception e) {
				Debug.LogError($"Error finding the Input Signal defintion: {e.Message}");
				throw;
			}
			
			return null;
		}

		protected override void OnEnable() {
			signalDefinition.onInvoke -= OnSignalInvoke;
			signalDefinition.onInvoke += OnSignalInvoke;
		}

		protected override void OnDisable() {
			signalDefinition.onInvoke -= OnSignalInvoke;
		}

		void OnSignalInvoke(Transform sender, Transform receiver, bool isGlobal, params object[] args){
			// Take the input arguments as objects and convert them to InputAction & Phases
			InputAction thisAction = (InputAction)args[0];
			InputActionPhase thisActionPhase = (InputActionPhase)args[1];

			if (actionName.value == thisAction.name && actionPhase.value == thisActionPhase) {
				YieldReturn(true);
			}
		}

		protected override bool OnCheck() { return false; }
	}
}

