using Cirno.Scripts.Interactables;
using Godot;

[Tool]
public partial class DoorButtonVisualizer : Node2D
{
    // [Export] public NodePath TargetPath; // Expose the Target NodePath in the editor

    // public override void _Ready()
    // {
    //     
    // }

    private Interactable _parent;
    
    public override void _EnterTree()
    {
        var p = GetParent();
        if (p == null) return;
        if (p is Interactable parent)
        {
            _parent = parent;
        } 
        //if (_parent == null) return;


    }

    public override void _Draw()
    {
        //var parent = GetParent<Interactable>();
        if (_parent != null) return;

        //if (GetParent() is not Interactable parent) return;
        if (!Engine.IsEditorHint()) return;

        // if (TargetPath == null || !Engine.IsEditorHint())
        //     return;

        if (_parent is not Switch p) return;
        
        if (p.Target == null) return;

        // Get the target node
        // Node targetNode = GetNodeOrNull(TargetPath);
        // if (targetNode == null || !(targetNode is Node2D target))
        //     return;

        // Draw a line to the target
        var start = GlobalPosition;
        var end = p.Target.GlobalPosition;

        DrawLine(ToLocal(start), ToLocal(end), Colors.Green, 2);
    }

    public override void _Process(double delta)
    {
        // Update the visualization in the editor
        if (Engine.IsEditorHint())
        {
            QueueRedraw();
        }
    }
}