using NubLang.Syntax;

namespace NubLang.Ast;

public abstract class Node(List<Token> tokens)
{
    public List<Token> Tokens { get; } = tokens;

    public abstract IEnumerable<Node> Children();

    public IEnumerable<Node> Descendants()
    {
        foreach (var child in Children())
        {
            foreach (var descendant in child.DescendantsAndSelf())
            {
                yield return descendant;
            }
        }
    }

    public IEnumerable<Node> DescendantsAndSelf()
    {
        yield return this;
        foreach (var descendant in Descendants())
        {
            yield return descendant;
        }
    }
}

#region Definitions

public abstract class DefinitionNode(List<Token> tokens, string module, string name) : Node(tokens)
{
    public string Module { get; } = module;
    public string Name { get; } = name;
}

public class FuncParameterNode(List<Token> tokens, string name, NubType type) : Node(tokens)
{
    public string Name { get; } = name;
    public NubType Type { get; } = type;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class FuncPrototypeNode(List<Token> tokens, string module, string name, string? externSymbol, List<FuncParameterNode> parameters, NubType returnType) : Node(tokens)
{
    public string Module { get; } = module;
    public string Name { get; } = name;
    public string? ExternSymbol { get; } = externSymbol;
    public List<FuncParameterNode> Parameters { get; } = parameters;
    public NubType ReturnType { get; } = returnType;

    public override IEnumerable<Node> Children()
    {
        return Parameters;
    }
}

public class FuncNode(List<Token> tokens, FuncPrototypeNode prototype, BlockNode? body) : DefinitionNode(tokens, prototype.Module, prototype.Name)
{
    public FuncPrototypeNode Prototype { get; } = prototype;
    public BlockNode? Body { get; } = body;

    public override IEnumerable<Node> Children()
    {
        yield return Prototype;
        if (Body != null)
        {
            yield return Body;
        }
    }
}

#endregion

#region Statements

public abstract class StatementNode(List<Token> tokens) : Node(tokens);

public abstract class TerminalStatementNode(List<Token> tokens) : StatementNode(tokens);

public class BlockNode(List<Token> tokens, List<StatementNode> statements) : StatementNode(tokens)
{
    public List<StatementNode> Statements { get; } = statements;

    public override IEnumerable<Node> Children()
    {
        return Statements;
    }
}

public class StatementFuncCallNode(List<Token> tokens, FuncCallNode funcCall) : StatementNode(tokens)
{
    public FuncCallNode FuncCall { get; } = funcCall;

    public override IEnumerable<Node> Children()
    {
        yield return FuncCall;
    }
}

public class ReturnNode(List<Token> tokens, ExpressionNode? value) : TerminalStatementNode(tokens)
{
    public ExpressionNode? Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        if (Value != null) yield return Value;
    }
}

public class AssignmentNode(List<Token> tokens, LValueExpressionNode target, ExpressionNode value) : StatementNode(tokens)
{
    public LValueExpressionNode Target { get; } = target;
    public ExpressionNode Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
        yield return Value;
    }
}

public class IfNode(List<Token> tokens, ExpressionNode condition, BlockNode body, Variant<IfNode, BlockNode>? @else) : StatementNode(tokens)
{
    public ExpressionNode Condition { get; } = condition;
    public BlockNode Body { get; } = body;
    public Variant<IfNode, BlockNode>? Else { get; } = @else;

    public override IEnumerable<Node> Children()
    {
        yield return Condition;
        yield return Body;
        if (Else.HasValue)
        {
            yield return Else.Value.Match<Node>(x => x, x => x);
        }
    }
}

public class VariableDeclarationNode(List<Token> tokens, string name, ExpressionNode? assignment, NubType type) : StatementNode(tokens)
{
    public string Name { get; } = name;
    public ExpressionNode? Assignment { get; } = assignment;
    public NubType Type { get; } = type;

    public override IEnumerable<Node> Children()
    {
        if (Assignment != null) yield return Assignment;
    }
}

public class ContinueNode(List<Token> tokens) : TerminalStatementNode(tokens)
{
    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class BreakNode(List<Token> tokens) : TerminalStatementNode(tokens)
{
    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class WhileNode(List<Token> tokens, ExpressionNode condition, BlockNode body) : StatementNode(tokens)
{
    public ExpressionNode Condition { get; } = condition;
    public BlockNode Body { get; } = body;

    public override IEnumerable<Node> Children()
    {
        yield return Condition;
        yield return Body;
    }
}

public class ForSliceNode(List<Token> tokens, string elementName, string? indexName, ExpressionNode target, BlockNode body) : StatementNode(tokens)
{
    public string ElementName { get; } = elementName;
    public string? IndexName { get; } = indexName;
    public ExpressionNode Target { get; } = target;
    public BlockNode Body { get; } = body;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
        yield return Body;
    }
}

public class ForConstArrayNode(List<Token> tokens, string elementName, string? indexName, ExpressionNode target, BlockNode body) : StatementNode(tokens)
{
    public string ElementName { get; } = elementName;
    public string? IndexName { get; } = indexName;
    public ExpressionNode Target { get; } = target;
    public BlockNode Body { get; } = body;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
        yield return Body;
    }
}

public class DeferNode(List<Token> tokens, StatementNode statement) : StatementNode(tokens)
{
    public StatementNode Statement { get; } = statement;

    public override IEnumerable<Node> Children()
    {
        yield return Statement;
    }
}

#endregion

#region Expressions

public enum UnaryOperator
{
    Negate,
    Invert
}

public enum BinaryOperator
{
    Equal,
    NotEqual,
    GreaterThan,
    GreaterThanOrEqual,
    LessThan,
    LessThanOrEqual,
    LogicalAnd,
    LogicalOr,
    Plus,
    Minus,
    Multiply,
    Divide,
    Modulo,
    LeftShift,
    RightShift,
    BitwiseAnd,
    BitwiseXor,
    BitwiseOr
}

public abstract class ExpressionNode(List<Token> tokens, NubType type) : Node(tokens)
{
    public NubType Type { get; } = type;
}

public abstract class LValueExpressionNode(List<Token> tokens, NubType type) : ExpressionNode(tokens, type);

public abstract class RValueExpressionNode(List<Token> tokens, NubType type) : ExpressionNode(tokens, type);

public abstract class IntermediateExpression(List<Token> tokens) : ExpressionNode(tokens, new NubVoidType());

public class StringLiteralNode(List<Token> tokens, string value) : RValueExpressionNode(tokens, new NubStringType())
{
    public string Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class CStringLiteralNode(List<Token> tokens, string value) : RValueExpressionNode(tokens, new NubPointerType(new NubIntType(true, 8)))
{
    public string Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class I8LiteralNode(List<Token> tokens, sbyte value) : RValueExpressionNode(tokens, new NubIntType(true, 8))
{
    public sbyte Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class I16LiteralNode(List<Token> tokens, short value) : RValueExpressionNode(tokens, new NubIntType(true, 16))
{
    public short Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class I32LiteralNode(List<Token> tokens, int value) : RValueExpressionNode(tokens, new NubIntType(true, 32))
{
    public int Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class I64LiteralNode(List<Token> tokens, long value) : RValueExpressionNode(tokens, new NubIntType(true, 64))
{
    public long Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class U8LiteralNode(List<Token> tokens, byte value) : RValueExpressionNode(tokens, new NubIntType(false, 8))
{
    public byte Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class U16LiteralNode(List<Token> tokens, ushort value) : RValueExpressionNode(tokens, new NubIntType(false, 16))
{
    public ushort Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class U32LiteralNode(List<Token> tokens, uint value) : RValueExpressionNode(tokens, new NubIntType(false, 32))
{
    public uint Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class U64LiteralNode(List<Token> tokens, ulong value) : RValueExpressionNode(tokens, new NubIntType(false, 64))
{
    public ulong Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class Float32LiteralNode(List<Token> tokens, float value) : RValueExpressionNode(tokens, new NubFloatType(32))
{
    public float Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class Float64LiteralNode(List<Token> tokens, double value) : RValueExpressionNode(tokens, new NubFloatType(64))
{
    public double Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class BoolLiteralNode(List<Token> tokens, NubType type, bool value) : RValueExpressionNode(tokens, type)
{
    public bool Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class BinaryExpressionNode(List<Token> tokens, NubType type, ExpressionNode left, BinaryOperator @operator, ExpressionNode right) : RValueExpressionNode(tokens, type)
{
    public ExpressionNode Left { get; } = left;
    public BinaryOperator Operator { get; } = @operator;
    public ExpressionNode Right { get; } = right;

    public override IEnumerable<Node> Children()
    {
        yield return Left;
        yield return Right;
    }
}

public class UnaryExpressionNode(List<Token> tokens, NubType type, UnaryOperator @operator, ExpressionNode operand) : RValueExpressionNode(tokens, type)
{
    public UnaryOperator Operator { get; } = @operator;
    public ExpressionNode Operand { get; } = operand;

    public override IEnumerable<Node> Children()
    {
        yield return Operand;
    }
}

public class FuncCallNode(List<Token> tokens, NubType type, ExpressionNode expression, List<ExpressionNode> parameters) : RValueExpressionNode(tokens, type)
{
    public ExpressionNode Expression { get; } = expression;
    public List<ExpressionNode> Parameters { get; } = parameters;

    public override IEnumerable<Node> Children()
    {
        yield return Expression;
        foreach (var expressionNode in Parameters)
        {
            yield return expressionNode;
        }
    }
}

public class VariableIdentifierNode(List<Token> tokens, NubType type, string name) : LValueExpressionNode(tokens, type)
{
    public string Name { get; } = name;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class FuncIdentifierNode(List<Token> tokens, NubType type, string module, string name, string? externSymbol) : RValueExpressionNode(tokens, type)
{
    public string Module { get; } = module;
    public string Name { get; } = name;
    public string? ExternSymbol { get; } = externSymbol;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class ArrayInitializerNode(List<Token> tokens, NubType type, List<ExpressionNode> values) : RValueExpressionNode(tokens, type)
{
    public List<ExpressionNode> Values { get; } = values;

    public override IEnumerable<Node> Children()
    {
        return Values;
    }
}

public class ConstArrayInitializerNode(List<Token> tokens, NubType type, List<ExpressionNode> values) : RValueExpressionNode(tokens, type)
{
    public List<ExpressionNode> Values { get; } = values;

    public override IEnumerable<Node> Children()
    {
        return Values;
    }
}

public class ArrayIndexAccessNode(List<Token> tokens, NubType type, ExpressionNode target, ExpressionNode index) : LValueExpressionNode(tokens, type)
{
    public ExpressionNode Target { get; } = target;
    public ExpressionNode Index { get; } = index;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
        yield return Index;
    }
}

public class ConstArrayIndexAccessNode(List<Token> tokens, NubType type, ExpressionNode target, ExpressionNode index) : LValueExpressionNode(tokens, type)
{
    public ExpressionNode Target { get; } = target;
    public ExpressionNode Index { get; } = index;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
        yield return Index;
    }
}

public class SliceIndexAccessNode(List<Token> tokens, NubType type, ExpressionNode target, ExpressionNode index) : LValueExpressionNode(tokens, type)
{
    public ExpressionNode Target { get; } = target;
    public ExpressionNode Index { get; } = index;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
        yield return Index;
    }
}

public class AddressOfNode(List<Token> tokens, NubType type, LValueExpressionNode lValue) : RValueExpressionNode(tokens, type)
{
    public LValueExpressionNode LValue { get; } = lValue;

    public override IEnumerable<Node> Children()
    {
        yield return LValue;
    }
}

public class StructFieldAccessNode(List<Token> tokens, NubType type, ExpressionNode target, string field) : LValueExpressionNode(tokens, type)
{
    public ExpressionNode Target { get; } = target;
    public string Field { get; } = field;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
    }
}

public class StructInitializerNode(List<Token> tokens, NubType type, Dictionary<string, ExpressionNode> initializers) : RValueExpressionNode(tokens, type)
{
    public Dictionary<string, ExpressionNode> Initializers { get; } = initializers;

    public override IEnumerable<Node> Children()
    {
        foreach (var initializer in Initializers)
        {
            yield return initializer.Value;
        }
    }
}

public class DereferenceNode(List<Token> tokens, NubType type, ExpressionNode target) : LValueExpressionNode(tokens, type)
{
    public ExpressionNode Target { get; } = target;

    public override IEnumerable<Node> Children()
    {
        yield return Target;
    }
}

public class SizeNode(List<Token> tokens, NubType TargetType) : RValueExpressionNode(tokens, new NubIntType(false, 64))
{
    public NubType TargetType { get; } = TargetType;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

public class CastNode(List<Token> tokens, NubType type, ExpressionNode value) : RValueExpressionNode(tokens, type)
{
    public ExpressionNode Value { get; } = value;

    public override IEnumerable<Node> Children()
    {
        yield return Value;
    }
}

public class EnumReferenceIntermediateNode(List<Token> tokens, string module, string name) : IntermediateExpression(tokens)
{
    public string Module { get; } = module;
    public string Name { get; } = name;

    public override IEnumerable<Node> Children()
    {
        return [];
    }
}

#endregion