/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Other licenses:
 * -----------------------------------------------------------------------------
 * Commercial licenses for this work are available. These replace the above
 * Apache-2.0 license and offer limited warranties, support, maintenance, and
 * commercial database integrations.
 *
 * For more information, please visit: https://www.jooq.org/legal/licensing
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package org.jooq.impl;

// ...
// ...
// ...
import static org.jooq.SQLDialect.MARIADB;
// ...
import static org.jooq.SQLDialect.MYSQL;
import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.YUGABYTEDB;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.Tools.EMPTY_SORTFIELD;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.OrderField;
import org.jooq.SQLDialect;
import org.jooq.SortField;
import org.jooq.Table;
import org.jooq.impl.QOM.UNotYetImplemented;

/**
 * @author Lukas Eder
 */
class IndexImpl extends AbstractNamed implements Index, UNotYetImplemented {
    private static final Set<SQLDialect>     NO_SUPPORT_INDEX_QUALIFICATION = SQLDialect.supportedBy(MARIADB, MYSQL, POSTGRES, YUGABYTEDB);

    private final Table<?>                   table;
    private final SortField<?>[]             fields;
    private final Condition                  where;
    private final boolean                    unique;

    IndexImpl(Name name) {
        this(name, null, EMPTY_SORTFIELD, null, false);
    }

    IndexImpl(Name name, Table<?> table, OrderField<?>[] fields, Condition where, boolean unique) {
        super(name.empty() ? name : qualify(table, name), CommentImpl.NO_COMMENT);

        this.table = table;
        this.fields = Tools.sortFields(fields);
        this.where = where;
        this.unique = unique;
    }

    final SortField<?>[] $fields() { return fields; }
    final boolean        $unique() { return unique; }
    final Condition      $where()  { return where; }

    @Override
    public final void accept(Context<?> ctx) {
        if (NO_SUPPORT_INDEX_QUALIFICATION.contains(ctx.dialect()))
            ctx.visit(getUnqualifiedName());
        else if (getTable() == null)
            ctx.visit(new QualifiedImpl(getQualifiedName()));
        else
            ctx.visit(new QualifiedImpl(name(getTable().getQualifiedName().qualifier(), getUnqualifiedName())));
    }

    @Override
    public final Table<?> getTable() {
        return table;
    }

    @Override
    public final List<SortField<?>> getFields() {
        return Arrays.asList(fields);
    }

    @Override
    public final Condition getWhere() {
        return where;
    }

    @Override
    public boolean getUnique() {
        return unique;
    }

    // -------------------------------------------------------------------------
    // XXX: Query Object Model
    // -------------------------------------------------------------------------

    @Override
    public final Table<?> $table() {
        return table;
    }
}
