package top.baogutang.admin.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: nikooh
 * @date: 2024/08/22 : 10:42
 */
@Slf4j
public class OkCoinKLineUtil {

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getCoinMarkdownContent(String instId, List<String[]> candleDataList) {
        if (CollectionUtils.isEmpty(candleDataList)) {
            return null;
        }
        StringBuilder markdownBuilder = new StringBuilder();

        // 添加标题
        markdownBuilder.append("## ")
                .append(instId)
                .append("市场价格数据\n\n");

        // 添加表头
        markdownBuilder.append("|             时间               |           开盘价           |           最高价           |           最低价           |           收盘价           |\n");
        markdownBuilder.append("|--------------------------------|----------------------------|---------------------------|---------------------------|---------------------------|\n");

        // 添加每一行数据
        for (String[] candle : candleDataList) {
            markdownBuilder.append("| ")
                    .append(DATE_FORMAT.format(new Date(Long.parseLong(candle[0])))).append(" | ")
                    .append(String.format("%-30f", Double.parseDouble(candle[1]))).append(" | ")
                    .append(String.format("%-24f", Double.parseDouble(candle[2]))).append(" | ")
                    .append(String.format("%-24f", Double.parseDouble(candle[3]))).append(" | ")
                    .append(String.format("%-24f", Double.parseDouble(candle[4]))).append(" |\n");
        }

        return markdownBuilder.toString();
    }


}
