/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *    http://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.
 */

package org.apache.seatunnel.datasource.plugin.cdc.sqlserver;

import org.apache.seatunnel.api.configuration.util.OptionRule;
import org.apache.seatunnel.datasource.plugin.api.DataSourceChannelExt;
import org.apache.seatunnel.datasource.plugin.api.DataSourcePluginException;
import org.apache.seatunnel.datasource.plugin.api.model.TableField;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import com.google.common.collect.Sets;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.*;

@Slf4j
public class SqlServerCDCDataSourceChannel implements DataSourceChannelExt {
    public static class Holder {
        private static final SqlServerCDCDataSourceChannel INSTANCE =
                new SqlServerCDCDataSourceChannel();
    }

    public static SqlServerCDCDataSourceChannel getInstance() {
        return Holder.INSTANCE;
    }

    public static final Set<String> MYSQL_SYSTEM_DATABASES =
            Sets.newHashSet("master", "tempdb", "model", "msdb");

    @Override
    public boolean canAbleGetSchema() {
        return true;
    }

    @Override
    public OptionRule getDataSourceOptions(@NonNull String pluginName) {
        return SqlServerCDCOptionRule.optionRule();
    }

    @Override
    public OptionRule getDatasourceMetadataFieldsByDataSourceName(@NonNull String pluginName) {
        return SqlServerCDCOptionRule.metadataRule();
    }

    @Override
    public List<String> getTables(
            String pluginName,
            Map<String, String> requestParams,
            String database,
            Map<String, String> options) {
        return this.getTableNames(requestParams, database);
    }

    @Override
    public List<String> getDatabases(String pluginName, Map<String, String> requestParams) {
        try {
            return this.getDataBaseNames(requestParams);
        } catch (SQLException e) {
            throw new DataSourcePluginException("get databases failed", e);
        }
    }

    @Override
    public boolean checkDataSourceConnectivity(
            String pluginName, Map<String, String> requestParams) {
        try (Connection connection = init(requestParams);
                PreparedStatement statement = connection.prepareStatement("SELECT 1");
                ResultSet rs = statement.executeQuery()) {
            return rs.next();
        } catch (SQLException e) {
            throw new DataSourcePluginException("connect datasource failed", e);
        }
    }

    @Override
    public List<TableField> getTableFields(
            String pluginName, Map<String, String> requestParams, String database, String table) {
        Pair<String, String> pair = parseSchemaAndTable(table);
        return getTableFields(requestParams, database, pair.getLeft(), pair.getRight());
    }

    @Override
    public Map<String, List<TableField>> getTableFields(
            String pluginName,
            Map<String, String> requestParams,
            String database,
            List<String> tables) {
        Map<String, List<TableField>> tableFields = new HashMap<>(tables.size());
        for (String table : tables) {
            tableFields.put(table, getTableFields(pluginName, requestParams, database, table));
        }
        return tableFields;
    }

    private Connection init(Map<String, String> requestParams) throws SQLException {
        if (null == requestParams.get(SqlServerCDCOptionRule.BASE_URL.key())) {
            throw new DataSourcePluginException("Jdbc url is null");
        }
        String url = requestParams.get(SqlServerCDCOptionRule.BASE_URL.key());
        if (null != requestParams.get(SqlServerCDCOptionRule.PASSWORD.key())
                && null != requestParams.get(SqlServerCDCOptionRule.USERNAME.key())) {
            String username = requestParams.get(SqlServerCDCOptionRule.USERNAME.key());
            String password = requestParams.get(SqlServerCDCOptionRule.PASSWORD.key());
            return DriverManager.getConnection(url, username, password);
        }
        return DriverManager.getConnection(url);
    }

    private List<String> getDataBaseNames(Map<String, String> requestParams) throws SQLException {
        List<String> dbNames = new ArrayList<>();
        try (Connection connection = init(requestParams);
                PreparedStatement statement =
                        connection.prepareStatement(
                                "SELECT NAME FROM SYS.DATABASES WHERE IS_CDC_ENABLED = 1;");
                ResultSet re = statement.executeQuery()) {
            // filter system databases
            while (re.next()) {
                String dbName = re.getString("NAME");
                if (StringUtils.isNotBlank(dbName) && isNotSystemDatabase(dbName)) {
                    dbNames.add(dbName);
                }
            }

            return dbNames;
        }
    }

    private List<String> getTableNames(Map<String, String> requestParams, String dbName) {
        final String sql =
                String.format(
                        "SELECT SCHEMAS.NAME AS SCHEMA_NAME, TABLES.NAME AS TABLE_NAME"
                                + "    FROM %s.SYS.SCHEMAS AS SCHEMAS"
                                + "        JOIN %s.SYS.TABLES AS TABLES"
                                + "            ON SCHEMAS.SCHEMA_ID = TABLES.SCHEMA_ID"
                                + "                   AND TABLES.IS_TRACKED_BY_CDC = 1",
                        dbName, dbName);

        List<String> tableNames = new ArrayList<>();
        try (Connection connection = init(requestParams);
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(sql)) {
            while (resultSet.next()) {
                String schemaName = resultSet.getString("SCHEMA_NAME");
                String tableName = resultSet.getString("TABLE_NAME");
                tableNames.add(schemaName + "." + tableName);
            }
            return tableNames;
        } catch (SQLException e) {
            throw new DataSourcePluginException("get table names failed", e);
        }
    }

    private List<TableField> getTableFields(
            Map<String, String> requestParams, String dbName, String schemaName, String tableName) {
        List<TableField> tableFields = new ArrayList<>();
        try (Connection connection = init(requestParams); ) {
            DatabaseMetaData metaData = connection.getMetaData();
            String primaryKey = getPrimaryKey(metaData, dbName, schemaName, tableName);
            ResultSet resultSet = metaData.getColumns(dbName, schemaName, tableName, null);
            while (resultSet.next()) {
                TableField tableField = new TableField();
                String columnName = resultSet.getString("COLUMN_NAME");
                tableField.setPrimaryKey(false);
                if (StringUtils.isNotBlank(primaryKey) && primaryKey.equals(columnName)) {
                    tableField.setPrimaryKey(true);
                }
                tableField.setName(columnName);
                tableField.setType(resultSet.getString("TYPE_NAME"));
                tableField.setComment(resultSet.getString("REMARKS"));
                Object nullable = resultSet.getObject("IS_NULLABLE");
                boolean isNullable = convertToBoolean(nullable);
                tableField.setNullable(isNullable);
                tableFields.add(tableField);
            }
        } catch (SQLException e) {
            throw new DataSourcePluginException("get table fields failed", e);
        }
        return tableFields;
    }

    private String getPrimaryKey(
            DatabaseMetaData metaData, String dbName, String schemaName, String tableName)
            throws SQLException {
        ResultSet primaryKeysInfo = metaData.getPrimaryKeys(dbName, schemaName, tableName);
        while (primaryKeysInfo.next()) {
            return primaryKeysInfo.getString("COLUMN_NAME");
        }
        return null;
    }

    private boolean isNotSystemDatabase(String dbName) {
        return MYSQL_SYSTEM_DATABASES.stream()
                .noneMatch(systemDatabase -> StringUtils.equalsIgnoreCase(systemDatabase, dbName));
    }

    private boolean convertToBoolean(Object value) {
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        if (value instanceof String) {
            return value.equals("TRUE");
        }
        return false;
    }

    private Pair<String, String> parseSchemaAndTable(String tableName) {
        String[] schemaAndTable = tableName.split("\\.");
        if (schemaAndTable.length != 2) {
            throw new DataSourcePluginException("table name is invalid");
        }
        return Pair.of(schemaAndTable[0], schemaAndTable[1]);
    }

    @Override
    public List<Map> getTableAlls(
            @NonNull String pluginName,
            Map<String, String> requestParams,
            String database,
            String tableName,
            Map<String, String> options) {
        List<Map> tableNames = new ArrayList<>();
        try (Connection connection = getConnection(pluginName, requestParams)) {

            ResultSet resultSet = null;

            if (StringUtils.isNotBlank(database)) {

                StringBuilder sql =
                        new StringBuilder(
                                "select ROW_NUMBER() OVER (ORDER BY a.name) AS No, a.name TABLE_NAME    ,g.class_desc,g.major_id,g.class,g.value  TABLE_COMMENT  from "
                                        + " sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0) where 1=1 ");

                if (StringUtils.isNotBlank(tableName)) {
                    sql.append(" and  a.name='" + tableName + "'");
                }

                Statement statement = connection.createStatement();
                statement.execute("use " + database);
                resultSet = statement.executeQuery(sql.toString());
                fillTableNames(tableNames, resultSet);
            } else {

                String sqlDatabases = "SELECT * FROM sys.sysdatabases";
                Statement statement = connection.createStatement();
                resultSet = statement.executeQuery(sqlDatabases.toString());
                while (resultSet.next()) {

                    //                    ResultSetMetaData metaData = resultSet.getMetaData();
                    //                    //  获取表列数
                    //                    int columnCount = metaData.getColumnCount();
                    //                    for (int i = 1; i <= columnCount; i++) {
                    //                        tables.put(metaData.getColumnName(i),
                    // resultSet.getObject(i));
                    //                    }

                    String databaseName = resultSet.getString("name");
                    if (StringUtils.isNotBlank(databaseName)
                            && !Arrays.asList("master", "tempdb", "model", "msdb")
                                    .contains(databaseName)) {
                        StringBuilder sql =
                                new StringBuilder(
                                        "select ROW_NUMBER() OVER (ORDER BY a.name) AS No, a.name TABLE_NAME    ,g.class_desc,g.major_id,g.class,g.value  TABLE_COMMENT  from "
                                                + " sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0) where 1=1 ");

                        if (StringUtils.isNotBlank(tableName)) {
                            sql.append(" and  a.name='" + tableName + "'");
                        }

                        statement.execute("use " + database);
                        resultSet = statement.executeQuery(sql.toString());
                        fillTableNames(tableNames, resultSet);
                    }
                }
            }

            return tableNames;
        } catch (SQLException e) {
            throw new DataSourcePluginException("get table names failed", e);
        }
    }

    private static void fillTableNames(List<Map> tableNames, ResultSet resultSet)
            throws SQLException {
        if (Objects.nonNull(resultSet)) {
            while (resultSet.next()) {
                Map tables = new HashMap();

                ResultSetMetaData metaData = resultSet.getMetaData();
                //  获取表列数
                int columnCount = metaData.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    tables.put(metaData.getColumnName(i), resultSet.getObject(i));
                }

                String tableNameOld = resultSet.getString("TABLE_NAME");
                if (StringUtils.isNotBlank(tableNameOld)) {
                    tables.put("TABLE_NAME", tableNameOld);
                }

                String tableComment = resultSet.getString("TABLE_COMMENT");

                if (StringUtils.isNotBlank(tableComment)) {
                    tables.put("TABLE_COMMENT", tableComment);
                }
                tableNames.add(tables);
            }
        }
    }

    @Override
    public Map getTableName(
            @NonNull String pluginName,
            Map<String, String> requestParams,
            String database,
            Map<String, String> options,
            String tableName) {

        try (Connection connection = getConnection(pluginName, requestParams)) {
            String sql =
                    "SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'["
                            + database
                            + "].[dbo].["
                            + tableName
                            + "]') AND type IN ('U')";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            // 字段的个数
            int count = metaData.getColumnCount();
            // 创建List集合
            List<Map> list = new ArrayList<>();
            // 处理结果集
            while (resultSet.next()) {
                // 创建Map集合
                Map map = new HashMap<>();
                // 根据字段的个数, 循环
                for (int i = 0; i < count; i++) {
                    // 获取字段的名字
                    String columnName = metaData.getColumnName(i + 1);
                    // 获取字段对应的值
                    Object object = resultSet.getObject(columnName);
                    // 将字段名和字段值, 存入map集合中
                    map.put(columnName, object);
                }
                // 将map集合添加到List集合中
                list.add(map);
            }
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
        } catch (SQLException e) {
            throw new DataSourcePluginException("get table fields failed", e);
        }

        return null;
    }
}
