package com.utic.center.common.utils;

import java.nio.charset.Charset;
import java.text.DecimalFormat;

public class LogUtils {

    private LogUtils() {}

    public static String repeatString(String str, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(str);
        }
        return sb.toString();
    }

    public static String adjustLogMsg(String logMsg, int targetBytes) {
        byte[] bytes = logMsg.getBytes(Charset.forName("EUC-KR"));

        if (bytes.length > targetBytes) {
            // 초과하는 경우 한글이 깨지지 않도록 문자열을 자름
            while (logMsg.getBytes(Charset.forName("EUC-KR")).length > targetBytes) {
                logMsg = logMsg.substring(0, logMsg.length() - 1);
            }
        } else {
            // 부족한 경우 앞쪽에 공백 추가
            int paddingSize = targetBytes - bytes.length;
            logMsg = repeatString(" ", paddingSize) + logMsg; // 공백을 반복하여 추가
        }
        return logMsg;
    }

    public static String elapsedLog(String logMsg, int count, long elapsedTime) {
        String adjustedMsg = adjustLogMsg(logMsg, 38);
        DecimalFormat df = new DecimalFormat("#,###");
        return String.format("%s: [%8s ms, %8s EA]", adjustedMsg, df.format(elapsedTime), df.format(count));
    }
    public static String elapsedLog(String logMsg) {
        String adjustedMsg = adjustLogMsg(logMsg, 38);
        return String.format("%s", adjustedMsg);
    }
    public static String elapsedLogLength(String logMsg, int length) {
        String adjustedMsg = adjustLogMsg(logMsg, 38-length);
        return String.format("%s", adjustedMsg);
    }
    public static String elapsedLog(String logMsg, long elapsedTime) {
        String adjustedMsg = adjustLogMsg(logMsg, 38);
        DecimalFormat df = new DecimalFormat("#,###");
        return String.format("%s: [%8s ms]", adjustedMsg, df.format(elapsedTime));
    }
    public static String elapsedLogLength(String logMsg, long elapsedTime, int length) {
        String adjustedMsg = adjustLogMsg(logMsg, 38-length);
        DecimalFormat df = new DecimalFormat("#,###");
        return String.format("%s: [%8s ms]", adjustedMsg, df.format(elapsedTime));
    }
    public static String numberLog(long nmbr) {
        DecimalFormat df = new DecimalFormat("#,###");
        return String.format("%8s", df.format(nmbr));
    }
    public static String numberLog(int nmbr) {
        DecimalFormat df = new DecimalFormat("#,###");
        return String.format("%8s", df.format(nmbr));
    }
}
