/*
 * 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import org.jooq.Catalog;
import org.jooq.Configuration;
import org.jooq.DDLExportConfiguration;
import org.jooq.Queries;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
import org.jooq.UniqueKey;
import org.jooq.exception.DataAccessException;
import org.jooq.util.xml.jaxb.InformationSchema;

/**
 * @author Lukas Eder
 */
final class TableMetaImpl extends AbstractMeta {

    private final Table<?>[] tables;

    TableMetaImpl(Configuration configuration, Table<?>[] tables) {
        super(configuration);

        this.tables = tables;
    }

    @Override
    final AbstractMeta filtered0(Predicate<? super Catalog> catalogFilter, Predicate<? super Schema> schemaFilter) {
        return this;
    }

    @Override
    final List<Catalog> getCatalogs0() {
        Set<Catalog> result = new LinkedHashSet<>();

        for (Table<?> table : tables)
            if (table.getSchema() != null)
                if (table.getSchema().getCatalog() != null)
                    result.add(table.getSchema().getCatalog());

        return new ArrayList<>(result);
    }

    @Override
    final List<Schema> getSchemas0() {
        Set<Schema> result = new LinkedHashSet<>();

        for (Table<?> table : tables)
            if (table.getSchema() != null)
                result.add(table.getSchema());

        return new ArrayList<>(result);
    }

    @Override
    final List<Table<?>> getTables0() {
        return Collections.unmodifiableList(Arrays.asList(tables));
    }

    @Override
    final List<UniqueKey<?>> getPrimaryKeys0() {
        List<UniqueKey<?>> result = new ArrayList<>();

        for (Table<?> table : tables)
            if (table.getPrimaryKey() != null)
                result.add(table.getPrimaryKey());

        return result;
    }

    @Override
    public Queries ddl(DDLExportConfiguration exportConfiguration) throws DataAccessException {
        return new DDL(dsl(), exportConfiguration).queries(tables);
    }

    @Override
    public InformationSchema informationSchema() throws DataAccessException {
        return InformationSchemaExport.exportTables(configuration(), getTables());
    }
}
