/*
 * 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.connectors.seatunnel.jdbc.internal.dialect.sybase;

import lombok.extern.slf4j.Slf4j;
import org.apache.seatunnel.api.table.type.*;
import org.apache.seatunnel.common.exception.CommonErrorCode;
import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectTypeMapper;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

@Slf4j
public class SybaseTypeMapper implements JdbcDialectTypeMapper {

    // ============================data types=====================

    private static final String Sybase_UNKNOWN = "UNKNOWN";

    // -------------------------number----------------------------
    private static final String Sybase_BIT = "BIT";
    private static final String Sybase_TINYINT = "TINYINT";
    private static final String Sybase_SMALLINT = "SMALLINT";
    private static final String Sybase_INTEGER = "INTEGER";
    private static final String Sybase_INT = "INT";
    private static final String Sybase_BIGINT = "BIGINT";
    private static final String Sybase_DECIMAL = "DECIMAL";
    private static final String Sybase_FLOAT = "FLOAT";
    private static final String Sybase_REAL = "REAL";
    private static final String Sybase_NUMERIC = "NUMERIC";
    private static final String Sybase_MONEY = "MONEY";
    private static final String Sybase_SMALLMONEY = "SMALLMONEY";
    // -------------------------string----------------------------
    private static final String Sybase_CHAR = "CHAR";
    private static final String Sybase_VARCHAR = "VARCHAR";
    private static final String Sybase_NTEXT = "NTEXT";
    private static final String Sybase_NCHAR = "NCHAR";
    private static final String Sybase_NVARCHAR = "NVARCHAR";
    private static final String Sybase_TEXT = "TEXT";

    // ------------------------------time-------------------------
    private static final String Sybase_DATE = "DATE";
    private static final String Sybase_TIME = "TIME";
    private static final String Sybase_DATETIME = "DATETIME";
    private static final String Sybase_DATETIME2 = "DATETIME2";
    private static final String Sybase_SMALLDATETIME = "SMALLDATETIME";
    private static final String Sybase_DATETIMEOFFSET = "DATETIMEOFFSET";
    private static final String Sybase_TIMESTAMP = "TIMESTAMP";

    // ------------------------------blob-------------------------
    private static final String Sybase_BINARY = "BINARY";
    private static final String Sybase_VARBINARY = "VARBINARY";
    private static final String Sybase_IMAGE = "IMAGE";

    @SuppressWarnings("checkstyle:MagicNumber")
    @Override
    public SeaTunnelDataType<?> mapping(ResultSetMetaData metadata, int colIndex)
            throws SQLException {
        String SybaseType = metadata.getColumnTypeName(colIndex).toUpperCase();
        int precision = metadata.getPrecision(colIndex);
        int scale = metadata.getScale(colIndex);
        switch (SybaseType) {
            case Sybase_BIT:
                return BasicType.BOOLEAN_TYPE;
            case Sybase_TINYINT:
            case Sybase_SMALLINT:
                return BasicType.SHORT_TYPE;
            case Sybase_INTEGER:
            case Sybase_INT:
                return BasicType.INT_TYPE;
            case Sybase_BIGINT:
                return BasicType.LONG_TYPE;
            case Sybase_DECIMAL:
            case Sybase_NUMERIC:
            case Sybase_MONEY:
            case Sybase_SMALLMONEY:
                return new DecimalType(precision, scale);
            case Sybase_REAL:
                return BasicType.FLOAT_TYPE;
            case Sybase_FLOAT:
                return BasicType.DOUBLE_TYPE;
            case Sybase_CHAR:
            case Sybase_NCHAR:
            case Sybase_VARCHAR:
            case Sybase_NTEXT:
            case Sybase_NVARCHAR:
            case Sybase_TEXT:
                return BasicType.STRING_TYPE;
            case Sybase_DATE:
                return LocalTimeType.LOCAL_DATE_TYPE;
            case Sybase_TIME:
                return LocalTimeType.LOCAL_TIME_TYPE;
            case Sybase_DATETIME:
            case Sybase_DATETIME2:
            case Sybase_SMALLDATETIME:
            case Sybase_DATETIMEOFFSET:
                return LocalTimeType.LOCAL_DATE_TIME_TYPE;
            case Sybase_TIMESTAMP:
            case Sybase_BINARY:
            case Sybase_VARBINARY:
            case Sybase_IMAGE:
                return PrimitiveByteArrayType.INSTANCE;
                // Doesn't support yet
            case Sybase_UNKNOWN:
            default:
                final String jdbcColumnName = metadata.getColumnName(colIndex);
                throw new JdbcConnectorException(
                        CommonErrorCode.UNSUPPORTED_OPERATION,
                        String.format(
                                "Doesn't support Sybase type '%s' on column '%s'  yet.",
                                SybaseType, jdbcColumnName));
        }
    }
}
