package me.qwq.doghouse.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

import me.qwq.doghouse.annotation.StaticAttribute;

@StaticAttribute
public class AgoUtils {
      
    public static String ago(Date date) {
        long now = Calendar.getInstance().getTimeInMillis();
        if(date == null) {
            return "未知时间";
        }
        long delTime = (now - date.getTime()) / 1000;
        if (delTime > 365 * 24 * 60 * 60) {
            return (int)(delTime / (365 * 24 * 60 * 60)) + " 年前";
        }
        else if (delTime > 24 * 60 * 60) {
            return (int)(delTime / (24 * 60 * 60)) + " 天前";
        }
        else if (delTime > 60 * 60) {
            return (int)(delTime / (60 * 60)) + " 小时前";
        }
        else if (delTime > 60) {
            return (int)(delTime / (60)) + " 分钟前";
        }
        else if (delTime > 1) {
            return delTime + " 秒前";
        }
        else {
            return "刚刚";
        }
    }

    public static String ago(LocalDateTime dateTime) {
        if (dateTime == null) {
            return "未知时间";
        }

        ZoneId zone = ZoneId.systemDefault();
        long now = Instant.now().getEpochSecond(); 
        long target = dateTime.atZone(zone).toInstant().getEpochSecond();

        long delTime = now - target;

        if (delTime > 365 * 24 * 60 * 60) {
            return (delTime / (365 * 24 * 60 * 60)) + " 年前";
        } else if (delTime > 24 * 60 * 60) {
            return (delTime / (24 * 60 * 60)) + " 天前";
        } else if (delTime > 60 * 60) {
            return (delTime / (60 * 60)) + " 小时前";
        } else if (delTime > 60) {
            return (delTime / 60) + " 分钟前";
        } else if (delTime > 1) {
            return delTime + " 秒前";
        } else {
            return "刚刚";
        }
    }
}
