unit Myc.Fmx.AstEditor.Visualizer;

interface

uses
    System.SysUtils,
    System.Classes,
    FMX.Controls,
    Myc.Data.Value,
    Myc.Ast.Nodes,
    Myc.Ast.Visitor,
    Myc.Fmx.AstEditor.Render, // Virtual Engine
    Myc.Fmx.AstEditor.Node, // View Nodes
    Myc.Fmx.AstEditor.Workspace; // Host

type
    // Implementation of the Factory/Visitor for the Virtual Tree
    TAstVisualizer = class(TAstVisitor<TAstViewNode>, IAstVisualizer)
    private
        FExprDepth: Integer;
        FParentNode: TVisualNode; // Changed from TControl
        FWorkspace: TWorkspace;

        function CallAccept(const Node: IAstNode): TAstViewNode;

        // IAstVisualizer Implementation
        function GetExprDepth: Integer;
        function GetParentNode: TVisualNode;
        function GetWorkspace: TWorkspace;

    protected
        // --- Visitor Implementations ---
        function VisitConstant(const Node: IConstantNode): TAstViewNode; override;
        function VisitIdentifier(const Node: IIdentifierNode): TAstViewNode; override;
        function VisitKeyword(const Node: IKeywordNode): TAstViewNode; override;

        // Lists
        function VisitParameterList(const Node: IParameterList): TAstViewNode; override;
        function VisitArgumentList(const Node: IArgumentList): TAstViewNode; override;
        function VisitExpressionList(const Node: IExpressionList): TAstViewNode; override;
        function VisitRecordFieldList(const Node: IRecordFieldList): TAstViewNode; override;
        function VisitRecordField(const Node: IRecordFieldNode): TAstViewNode; override;

        // Control Flow
        function VisitIfExpression(const Node: IIfExpressionNode): TAstViewNode; override;
        function VisitCondExpression(const Node: ICondExpressionNode): TAstViewNode; override; // Replaces Ternary
        function VisitBlockExpression(const Node: IBlockExpressionNode): TAstViewNode; override;
        function VisitRecurNode(const Node: IRecurNode): TAstViewNode; override;
        function VisitNop(const Node: INopNode): TAstViewNode; override;

        // Functions
        function VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstViewNode; override;
        function VisitFunctionCall(const Node: IFunctionCallNode): TAstViewNode; override;
        function VisitMacroExpansionNode(const Node: IMacroExpansionNode): TAstViewNode; override;

        // Declarations
        function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstViewNode; override;
        function VisitAssignment(const Node: IAssignmentNode): TAstViewNode; override;
        function VisitMacroDefinition(const Node: IMacroDefinitionNode): TAstViewNode; override;

        // Metaprogramming
        function VisitQuasiquote(const Node: IQuasiquoteNode): TAstViewNode; override;
        function VisitUnquote(const Node: IUnquoteNode): TAstViewNode; override;
        function VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TAstViewNode; override;

        // Data Access
        function VisitIndexer(const Node: IIndexerNode): TAstViewNode; override;
        function VisitMemberAccess(const Node: IMemberAccessNode): TAstViewNode; override;
        function VisitRecordLiteral(const Node: IRecordLiteralNode): TAstViewNode; override;

        // Series
        function VisitCreateSeries(const Node: ICreateSeriesNode): TAstViewNode; override;
        function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstViewNode; override;
        function VisitSeriesLength(const Node: ISeriesLengthNode): TAstViewNode; override;

    public
        constructor Create(AWorkspace: TWorkspace; AParentNode: TVisualNode; AExprDepth: Integer);
        destructor Destroy; override;

        function Clone(ParentNode: TVisualNode; ExprDepth: Integer): IAstVisualizer;

        property ExprDepth: Integer read GetExprDepth;
        property ParentNode: TVisualNode read GetParentNode;
        property Workspace: TWorkspace read GetWorkspace;
    end;

implementation

uses
    Myc.Fmx.AstEditor.Handlers,
    Myc.Fmx.AstEditor.Handlers.Data,
    Myc.Fmx.AstEditor.Handlers.Control,
    Myc.Fmx.AstEditor.Handlers.Primitives,
    Myc.Fmx.AstEditor.Handlers.Lists;

// Helper to identify standard operators
function IsStandardOperator(const Name: string): Boolean;
begin
    // You can extend this list based on your RTL
    Result :=
        (Name = '+')
            or (Name = '-')
            or (Name = '*')
            or (Name = '/')
            or (Name = 'div')
            or (Name = 'mod')
            or (Name = '=')
            or (Name = '<>')
            or (Name = '<')
            or (Name = '>')
            or (Name = '<=')
            or (Name = '>=')
            or (Name = 'and')
            or (Name = 'or')
            or (Name = 'xor')
            or (Name = 'not')
            or (Name = 'shl')
            or (Name = 'shr');
end;

{ TAstVisualizer }

constructor TAstVisualizer.Create(AWorkspace: TWorkspace; AParentNode: TVisualNode; AExprDepth: Integer);
begin
    inherited Create;
    FWorkspace := AWorkspace;
    FParentNode := AParentNode;
    FExprDepth := AExprDepth;
end;

destructor TAstVisualizer.Destroy;
begin
    inherited;
end;

function TAstVisualizer.CallAccept(const Node: IAstNode): TAstViewNode;
var
    dataValue: TDataValue;
begin
    if not Assigned(Node) then
        exit(nil);

    // Dynamic dispatch via Visitor pattern
    dataValue := Node.Accept(Self);

    if dataValue.Kind = TDataValueKind.vkGeneric then
        Result := dataValue.AsGeneric<TAstViewNode>
    else
        Result := nil;
end;

function TAstVisualizer.Clone(ParentNode: TVisualNode; ExprDepth: Integer): IAstVisualizer;
begin
    Result := TAstVisualizer.Create(FWorkspace, ParentNode, ExprDepth);
end;

function TAstVisualizer.GetExprDepth: Integer;
begin
    Result := FExprDepth;
end;

function TAstVisualizer.GetParentNode: TVisualNode;
begin
    Result := FParentNode;
end;

function TAstVisualizer.GetWorkspace: TWorkspace;
begin
    Result := FWorkspace;
end;

// --- Visitor Implementation Helpers ---

function TAstVisualizer.VisitConstant(const Node: IConstantNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TConstantNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitIdentifier(const Node: IIdentifierNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TIdentifierNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitKeyword(const Node: IKeywordNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TKeywordNodeHandler.Create(Node));
end;

// Lists

function TAstVisualizer.VisitParameterList(const Node: IParameterList): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TParameterListHandler.Create(Node));
end;

function TAstVisualizer.VisitArgumentList(const Node: IArgumentList): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TArgumentListHandler.Create(Node));
end;

function TAstVisualizer.VisitExpressionList(const Node: IExpressionList): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TExpressionListHandler.Create(Node));
end;

function TAstVisualizer.VisitRecordFieldList(const Node: IRecordFieldList): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TRecordFieldListHandler.Create(Node));
end;

function TAstVisualizer.VisitRecordField(const Node: IRecordFieldNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TRecordFieldHandler.Create(Node));
end;

// Control Flow

function TAstVisualizer.VisitBlockExpression(const Node: IBlockExpressionNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TBlockExpressionNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitIfExpression(const Node: IIfExpressionNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TIfExpressionNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitCondExpression(const Node: ICondExpressionNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TCondExpressionNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitRecurNode(const Node: IRecurNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TRecurNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitNop(const Node: INopNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TNopNodeHandler.Create(Node));
end;

// Functions

function TAstVisualizer.VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TLambdaExpressionNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitFunctionCall(const Node: IFunctionCallNode): TAstViewNode;
var
    calleeName: string;
    isOp: Boolean;
begin
    isOp := False;

    // Check if callee is a simple identifier or keyword that matches an operator
    if Node.Callee.Kind = akIdentifier then
    begin
        calleeName := Node.Callee.AsIdentifier.Name;
        isOp := IsStandardOperator(calleeName);
    end
    else if Node.Callee.Kind = akKeyword then
    begin
        calleeName := Node.Callee.AsKeyword.Value.Name;
        isOp := IsStandardOperator(calleeName);
    end;

    if isOp then
        Result := TAstViewNode.Create(Self, TOperatorCallHandler.Create(Node))
    else
        Result := TAstViewNode.Create(Self, TFunctionCallNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitMacroExpansionNode(const Node: IMacroExpansionNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TMacroExpansionNodeHandler.Create(Node));
end;

// Declarations

function TAstVisualizer.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TVariableDeclarationNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitAssignment(const Node: IAssignmentNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TAssignmentNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitMacroDefinition(const Node: IMacroDefinitionNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TMacroDefinitionNodeHandler.Create(Node));
end;

// Metaprogramming

function TAstVisualizer.VisitQuasiquote(const Node: IQuasiquoteNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TQuasiquoteNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitUnquote(const Node: IUnquoteNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TUnquoteNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TUnquoteSplicingNodeHandler.Create(Node));
end;

// Data Access

function TAstVisualizer.VisitIndexer(const Node: IIndexerNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TIndexerNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitMemberAccess(const Node: IMemberAccessNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TMemberAccessNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitRecordLiteral(const Node: IRecordLiteralNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TRecordLiteralNodeHandler.Create(Node));
end;

// Series

function TAstVisualizer.VisitCreateSeries(const Node: ICreateSeriesNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TCreateSeriesNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TAddSeriesItemNodeHandler.Create(Node));
end;

function TAstVisualizer.VisitSeriesLength(const Node: ISeriesLengthNode): TAstViewNode;
begin
    Result := TAstViewNode.Create(Self, TSeriesLengthNodeHandler.Create(Node));
end;

end.
