/**
 *
 */
package org.springblade.datasource.transform.mysql;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springblade.datasource.transform.ColumnEnum;

import java.util.stream.Stream;

import static java.sql.Types.*;

/**
 * @author wookvn
 *
 * 显示的字段类型
 */
@Getter
@AllArgsConstructor
public enum SQLColumnTypeEnum {
    /**
     * 字符串
     */
    STRING(ColumnEnum.STRING, new Integer[]{CHAR, VARCHAR, LONGVARCHAR, BINARY, VARBINARY, LONGVARBINARY, BLOB, CLOB, NCHAR, NVARCHAR, LONGNVARCHAR, NCLOB, ARRAY}, new String[]{"CHAR", "VARCHAR", "LONGVARCHAR", "BINARY", "VARBINARY", "LONGVARBINARY", "BLOB", "CLOB", "NCHAR", "NVARCHAR", "LONGNVARCHAR", "NCLOB", "ARRAY", "VARCHAR2"}),

    /**
     * 日期
     */
    DATETIME(ColumnEnum.DATE, new Integer[]{DATE, TIME, TIMESTAMP}, new String[]{"DATE", "TIME", "TIMESTAMP", "DATETIME"}),

    /**
     * 整数
     */
    INT(ColumnEnum.INTEGER, new Integer[]{TINYINT, SMALLINT, INTEGER}, new String[]{"TINYINT", "SMALLINT", "INTEGER", "INT", "Int32"}),

    /**
     *
     */
    LONG(ColumnEnum.LONG, new Integer[]{BIGINT}, new String[]{"BIGINT", "Int64"}),

    /**
     * 浮点数
     */
    BIG_DECIMAL(ColumnEnum.DECIMAL, new Integer[]{FLOAT, REAL, DOUBLE, NUMERIC, DECIMAL}, new String[]{"FLOAT", "REAL", "DOUBLE", "NUMERIC", "DECIMAL"}),
    /**
     * 布尔
     */
    BOOL(ColumnEnum.BOOL, new Integer[]{BIT, BOOLEAN}, new String[]{"BIT", "BOOLEAN"});
    @EnumValue
    private ColumnEnum columnEnum;
    private Integer[] types;
    private String[] typeName;


    public static SQLColumnTypeEnum resolve(Integer type) {
        SQLColumnTypeEnum[] values = SQLColumnTypeEnum.values();
        for (SQLColumnTypeEnum columnType : values) {
            if (Stream.of(columnType.getTypes()).anyMatch(f -> type != null && f.intValue() == type)) {
                return columnType;
            }
        }
        return SQLColumnTypeEnum.STRING;
    }

    public static SQLColumnTypeEnum resolve(String typeName) {
        SQLColumnTypeEnum[] values = SQLColumnTypeEnum.values();
        for (SQLColumnTypeEnum columnType : values) {
            if (Stream.of(columnType.getTypeName()).anyMatch(f -> typeName != null && f.equalsIgnoreCase(typeName))) {
                return columnType;
            }
        }
        return null;
    }

    public static SQLColumnTypeEnum getByName(String typeName) {
        SQLColumnTypeEnum[] values = SQLColumnTypeEnum.values();
        for (SQLColumnTypeEnum columnType : values) {
            if (columnType.name().equalsIgnoreCase(typeName)) {
                return columnType;
            }
        }
        return null;
    }
}
