using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;


namespace NodeCanvas.Tasks.Actions {

	[Category("Reset/Movement")]
	[Description("Unit flat ground movement")]
	public class CalculateGroundedLocomotion : ActionTask<CharacterController>{
		public BBParameter<float> moveSpeed;

		
		public BBParameter<Vector3> moveDirection;
		
		protected override string info {
			get{
				return string.Format($"<b>Set ground movement</b>, <i>{moveSpeed.value}</i> speed");
			}
		}
		
		private float sprintPower;
		private bool sprinting;

		//Use for initialization. This is called only once in the lifetime of the task.
		//Return null if init was successfull. Return an error string otherwise
		protected override string OnInit() {
			return null;
		}

		//This is called once each time the task is enabled.
		//Call EndAction() to mark the action as finished, either in success or failure.
		//EndAction can be called from anywhere.
		protected override void OnExecute() {
			
		}

		//Called once per frame while the action is active.
		protected override void OnUpdate() {
			// Get current input direction
			Vector3 flatSurfaceDirection = Vector3.zero;
        
			if (agent.isGrounded) {
				Vector2 rawInput = agent.transform.GetComponent<PlayerControls>().rawMoveInput;
				flatSurfaceDirection = new(rawInput.x, 0, rawInput.y);
			}
			
			float CalculateSpeed(){
				// Calculate sprinting speed
				float outputSpeed = 0f;

				// Add base speed
				outputSpeed += moveSpeed.value;
				
				return outputSpeed;
			}
        
			// Rotate input to forward direction for flat surface
			// flatSurfaceDirection = agent.transform.rotation * flatSurfaceDirection;
        
			// Finalize flat surface direction by adding speed
			flatSurfaceDirection *= CalculateSpeed();

			moveDirection.value = flatSurfaceDirection;
			EndAction(true);
		}

		//Called when the task is disabled.
		protected override void OnStop() {
			
		}

		//Called when the task is paused.
		protected override void OnPause() {
			
		}
	}
}