
using Godot;


namespace Rokojori
{  
  [GlobalClass, Tool ]
  public partial class SetVector3FromNodePosition : Action
  {
    [Export]
    public Node3D source;

    [Export]
    public bool global = true;

    [Export]
    public bool convertToView = false;

    [Export]
    public Resource target;

    [Export]
    public string member;

    [Export]
    public Camera3D camera;
    
    [Export]
    public Vector3 scale = Vector3.One;


    protected override void _OnTrigger()
    {
      if ( target == null || source == null )
      {
        return;
      }

      var value = source.GetPosition( global );

      if ( convertToView )
      {
        if ( Engine.IsEditorHint() )
        {
          #if TOOLS

          var editorCamera = EditorInterface.Singleton.GetEditorViewport3D().GetCamera3D();
          value = editorCamera.ToLocal( value );

          #endif
        }
        else
        {
          if ( camera != null )
          {
            value = camera.ToLocal( value );
          }
        }
      }

      value = value * scale;
      // this.LogInfo( "Set", member, ">>", value );
      ReflectionHelper.SetValue( target, member, value );

    }
  }
}