package cn.iocoder.yudao.module.iot.controller.admin.device.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * 数据类型解析工具类（静态工具类）
 *
 * 支持 TDengine / MQTT / OPC / JDBC 数据类型转换
 */
@Slf4j
public final class DataTypeParseUtil {

    /**
     * 私有构造函数，防止实例化
     */
    private DataTypeParseUtil() {
        throw new UnsupportedOperationException("Utility class cannot be instantiated");
    }

    /**
     * 根据 dataType 解析 value
     *
     * @param value 原始值
     * @param dataType 数据类型
     * @return 转换后的值
     */
    public static Object parse(Object value, String dataType) {

        if (value == null || dataType == null) {
            return null;
        }

        try {

            switch (dataType.toLowerCase()) {

                case "int":
                case "integer":
                    return toInt(value);

                case "long":
                    return toLong(value);

                case "double":
                    return toDouble(value);

                case "float":
                    return toFloat(value);

                case "short":
                    return toShort(value);

                case "boolean":
                    return toBoolean(value);

                case "string":
                case "varchar":
                case "text":
                    return value.toString();

                case "bigdecimal":
                    return toBigDecimal(value);

                case "timestamp":
                case "datetime":
                    return toLocalDateTime(value);

                default:
                    return value;
            }

        } catch (Exception e) {

            log.warn("数据类型解析失败 value={}, dataType={}", value, dataType);

            return value;
        }
    }

    public static Integer toInt(Object value) {

        if (value instanceof Number) {
            return ((Number) value).intValue();
        }

        return Integer.parseInt(value.toString());
    }

    public static Long toLong(Object value) {

        if (value instanceof Number) {
            return ((Number) value).longValue();
        }

        return Long.parseLong(value.toString());
    }

    public static Double toDouble(Object value) {

        if (value instanceof Number) {
            return ((Number) value).doubleValue();
        }

        return Double.parseDouble(value.toString());
    }

    public static Float toFloat(Object value) {

        if (value instanceof Number) {
            return ((Number) value).floatValue();
        }

        return Float.parseFloat(value.toString());
    }

    public static Short toShort(Object value) {

        if (value instanceof Number) {
            return ((Number) value).shortValue();
        }

        return Short.parseShort(value.toString());
    }

    public static Boolean toBoolean(Object value) {

        if (value instanceof Boolean) {
            return (Boolean) value;
        }

        String str = value.toString().toLowerCase();

        return "true".equals(str) || "1".equals(str);
    }

    public static BigDecimal toBigDecimal(Object value) {

        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }

        return new BigDecimal(value.toString());
    }

    public static LocalDateTime toLocalDateTime(Object value) {

        if (value instanceof Timestamp) {
            return ((Timestamp) value).toLocalDateTime();
        }

        if (value instanceof LocalDateTime) {
            return (LocalDateTime) value;
        }

        return LocalDateTime.parse(value.toString());
    }

}
