package org.dbsp.sqlCompiler.compiler.frontend.parser;

import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCreate;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.dbsp.util.Utilities;

import java.util.List;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

/** Parse tree for {@code CREATE INDEX} statement. */
public class SqlCreateIndex extends SqlCreate {
    public final SqlIdentifier name;
    public final SqlIdentifier indexed;
    public final SqlNodeList columns;

    private static final SqlOperator OPERATOR =
            new SqlSpecialOperator("CREATE INDEX", SqlKind.CREATE_INDEX) {
                @Override
                public SqlCall createCall(
                        @Nullable SqlLiteral functionQualifier, SqlParserPos pos, @Nullable SqlNode... operands) {
                    Utilities.enforce(operands.length == 5);
                    return new SqlCreateIndex(pos,
                            (SqlIdentifier) Objects.requireNonNull(operands[0]),
                            (SqlIdentifier) Objects.requireNonNull(operands[1]),
                            (SqlNodeList) Objects.requireNonNull(operands[2]),
                            ((SqlLiteral) requireNonNull(operands[3], "replace")).booleanValue(),
                            ((SqlLiteral) requireNonNull(operands[4], "ifNotExists")).booleanValue());
                }
            };

    public SqlCreateIndex(SqlParserPos pos, SqlIdentifier name, SqlIdentifier indexed, SqlNodeList columns,
                          boolean replace, boolean ifNotExists) {
        super(OPERATOR, pos, replace, ifNotExists);
        this.name = Objects.requireNonNull(name, "name");
        this.indexed = indexed;
        this.columns = columns;
    }

    @SuppressWarnings("nullness")
    @Override public List<SqlNode> getOperandList() {
        return ImmutableNullableList.of(
                this.name, this.indexed, this.columns,
                SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
                SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO));
    }

    @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
        writer.keyword("CREATE");
        writer.keyword("INDEX");
        this.name.unparse(writer, leftPrec, rightPrec);
        writer.keyword("ON");
        this.indexed.unparse(writer, leftPrec, rightPrec);
        {
            SqlWriter.Frame frame = writer.startList("(", ")");
            for (SqlNode c : this.columns) {
                writer.sep(",");
                c.unparse(writer, 0, 0);
            }
            writer.endList(frame);
        }
    }
}
