using Godot;
using System.Collections;
using System.Collections.Generic;
using Godot.Collections;

namespace Rokojori
{
  [Tool]
  [GlobalClass]
  public partial class CharacterController:Node
  {
    [Export]
    public CharacterBody3D body;

    public enum CharacterUpdateMode
    { 
      Process,
      Physics_Process
    }

    [Export]
    public CharacterUpdateMode characterUpdateMode = CharacterUpdateMode.Process;

    [Export]
    public Node actionsContainer;

    [Export]
    public Node3D graphics;

    
    [Export]
    public Smoothing rotationSmoothing;

    [Export]
    public Smoothing positionSmoothing;
    
    public float delta = 0;   


    public override void _Process( double delta )
    {
      if ( graphics == null || body == null )
      {
        return;
      }

      if ( CharacterUpdateMode.Process == characterUpdateMode )
      {
        this.delta = (float) delta;
        Nodes.ForEachDirectChild<CharacterControllerAction>( actionsContainer, Action.Trigger );
      }

      // positionSmoother.CopyPosition( graphics, body, rotationSmoothingDuration, delta );
      // rotationSmoother.CopyRotation( graphics, body, positionSmoothingDuration, delta );    

      graphics.GlobalPosition = Smoothing.Apply( positionSmoothing, body.GlobalPosition, this.delta );
      graphics.SetGlobalQuaternion( Smoothing.Apply( rotationSmoothing, body.GetGlobalQuaternion(), this.delta ) );
      // Pose.CopyTo( body, graphics );
    } 

    public override void _PhysicsProcess( double delta )
    {
      if ( CharacterUpdateMode.Physics_Process == characterUpdateMode )
      {
        this.delta = (float) delta;
        Nodes.ForEachDirectChild<CharacterControllerAction>( actionsContainer, Action.Trigger );
      }
    }
  }
}