package top.gtb520.java.backend.apibackserver.utils;

import org.springframework.stereotype.Component;
import top.gtb520.java.backend.apibackserver.db.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class tools {

    // ANSI颜色代码
    public static final String RESET = "\u001B[0m";
    public static final String BLACK = "\u001B[30m";
    public static final String RED = "\u001B[31m";
    public static final String GREEN = "\u001B[32m";
    public static final String YELLOW = "\u001B[33m";
    public static final String BLUE = "\u001B[34m";
    public static final String PURPLE = "\u001B[35m";
    public static final String CYAN = "\u001B[36m";
    public static final String WHITE = "\u001B[37m";

    // 背景色
    public static final String BLACK_BACKGROUND = "\u001B[40m";
    public static final String RED_BACKGROUND = "\u001B[41m";
    public static final String GREEN_BACKGROUND = "\u001B[42m";
    public static final String YELLOW_BACKGROUND = "\u001B[43m";
    public static final String BLUE_BACKGROUND = "\u001B[44m";
    public static final String PURPLE_BACKGROUND = "\u001B[45m";
    public static final String CYAN_BACKGROUND = "\u001B[46m";
    public static final String WHITE_BACKGROUND = "\u001B[47m";

    /**
     * 打印带颜色的文本到终端
     * @param color 颜色代码
     * @param text 要打印的文本
     */
    public static void printColor(String color, String text) {
        System.out.println(color + text + RESET);
    }

    /**
     * 返回带颜色的文本字符串
     * @param color 颜色代码
     * @param text 要着色的文本
     * @return 带颜色的文本字符串
     */
    public static String getColorText(String color, String text) {
        return color + text + RESET;
    }
    public static void echo(String color, String message){
        System.out.println(color + message + RESET);
        System.out.flush();
    }

    /**
     * 测试数据库连接的简单方法
     */
    public static void testDatabaseConnection() {
        mysql db = new mysql();
        try {
            if (db.isConnected()) {
                System.out.println("数据库连接测试成功！");
                
                // 测试查询
                ResultSet rs = db.query("SELECT 1 as test");
                if (rs.next()) {
                    System.out.println("查询结果: " + rs.getInt("test"));
                }
            } else {
                System.out.println("数据库连接测试失败！");
            }
        } catch (SQLException e) {
            System.out.println("执行查询时出错: " + e.getMessage());
        } finally {
            db.close();
        }
    }
}
