package com.mini.capi.utils;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

public class vDate {


    private static final DateTimeFormatter DEFAULT_FMT =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

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

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.ENGLISH);

    public static String toStr(LocalDateTime dateTime) {
        return dateTime.format(DEFAULT_FMT);
    }

    /**
     * LocalDateTime → String（自定义格式）
     */
    public static String toStr(LocalDateTime dateTime, String pattern) {
        return dateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String getNow() {
        Date date = new Date();
        return DEFAULT_SDF.format(date);
    }


    public static String getDockerCreate(String createAt) {
        try {
            String cleaned = createAt.replace(" CST", "");
            // 定义格式
            Date date = sdf.parse(cleaned);
            return DEFAULT_SDF.format(date);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return getNow();
        }
    }


    public static String getRunTimes(long totalSeconds) {
        if (totalSeconds < 0) {
            throw new IllegalArgumentException("输入的秒数不能为负数");
        }
        // 定义时间单位换算关系
        long secondsPerMinute = 60;
        long secondsPerHour = secondsPerMinute * 60; // 3600秒/小时
        long secondsPerDay = secondsPerHour * 24;    // 86400秒/天

        // 计算各单位数值
        long days = totalSeconds / secondsPerDay;
        long remainingSecondsAfterDays = totalSeconds % secondsPerDay;

        long hours = remainingSecondsAfterDays / secondsPerHour;
        long remainingSecondsAfterHours = remainingSecondsAfterDays % secondsPerHour;

        long minutes = remainingSecondsAfterHours / secondsPerMinute;
        long seconds = remainingSecondsAfterHours % secondsPerMinute;
        // 拼接结果字符串
        StringBuilder result = new StringBuilder();
        if (days > 0) {
            result.append(days).append("天");
        }
        if (hours > 0) {
            result.append(hours).append("小时");
        }
        if (minutes > 0) {
            result.append(minutes).append("分");
        }
        // 确保至少显示秒数（即使其他单位都为0）
        result.append(seconds).append("秒");
        return result.toString();
    }
}
