package com.ruoyi.common.utils.poi;

import java.util.Arrays;

public class TimeDataHander implements ExcelHandlerAdapter{

    @Override
    public Object format(Object value, String[] args) {
        String language = "";
        if (value != null) {
            long millisValue = (long) value;
            if (args != null && args.length > 0) {
                if (Arrays.stream(args).anyMatch("seconds"::equals)) {
                    millisValue = (long) value * 1000;
                }
                if (Arrays.stream(args).anyMatch("en"::equals)) {
                    language = "en";
                }
            }
            //return convertMillisToTime(millisValue, language);
            return convertMillisToMinutes(millisValue);
        } else {
            return "";
        }
    }

    public String convertMillisToTime(long millis, String language) {
        String dayLabel = "天";
        String hourLabel = "小时";
        String minuteLabel = "分钟";
        String secondLabel = "秒";

        if ("en".equals(language)) {
            dayLabel = " d ";
            hourLabel = " h ";
            minuteLabel = " m ";
            secondLabel = " s ";
        }


        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;

        seconds %= 60;
        minutes %= 60;
        hours %= 24;

        StringBuilder timeString = new StringBuilder();

        if (days > 0) {
            timeString.append(days).append(dayLabel);
        }

        if (hours > 0) {
            timeString.append(hours).append(hourLabel);
        }

        if (minutes > 0) {
            timeString.append(minutes).append(minuteLabel);
        }

        if (seconds > 0 || timeString.length() == 0) { // 即使所有值为0时，显示0秒
            timeString.append(seconds).append(secondLabel);
        }

        return timeString.toString();
    }

    public String convertMillisToMinutes(long millis) {
        long minutes = millis / (1000 * 60); // 计算总分钟数
        return minutes + " 分";
    }

}
