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

namespace Rokojori
{ 
  public class UndoGDMarker
  {
    public string name = "";
  }

  public class UndoGD
  {
    static UndoGDMarker undoActionMarker = null;

    #if TOOLS

    static EditorUndoRedoManager UndoManager()
    {
      return EditorInterface.Singleton.GetEditorUndoRedo();
    }

    #endif

    public static UndoGDMarker Start( string actionName )
    {
      #if TOOLS

      if ( undoActionMarker != null )
      {
        return null;
      }
      
      undoActionMarker = new UndoGDMarker();
      undoActionMarker.name = actionName;

      // UndoManager().CreateAction( actionName );

      return undoActionMarker;      

      #else

      return null;

      #endif
    } 


    public static void End( UndoGDMarker marker )
    {
      #if TOOLS

      if ( undoActionMarker != marker )
      {
        return;
      }
      
      // UndoManager().CommitAction();
      undoActionMarker = null;

      #else

      #endif
    }

    public static void Set<T>( GodotObject obj, string member, T value )
    {
      #if TOOLS

      try
      {
        if ( undoActionMarker == null )
        {
          RJLog.ErrorGD( obj, "No undo marker! ", member, ">>", value );
          return;
        }
        
        var currentValue = ReflectionHelper.GetValue<T>( obj, member );
        // UndoManager().AddUndoProperty( obj, member, (GodotObject)(object)currentValue );
        // UndoManager().AddDoProperty( obj, member, (GodotObject)(object)value );

      }
      catch( Exception e )
      {

      }

      ReflectionHelper.SetValue( obj, member, value );

      #else

      ReflectionHelper.SetValue( obj, member, value );

      #endif


    }
  }
}