package com.wechat.common.utils;

import java.util.Date;

public class Time2Now {
	private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;
 
    private static final String ONE_SECOND_AGO = "秒前: second ago";
    private static final String ONE_SECONDS_AGO = "秒前: seconds ago";
    private static final String ONE_MINUTE_AGO = "分钟前: minute ago";
    private static final String ONE_MINUTES_AGO = "分钟前: minutes ago";
    private static final String ONE_HOUR_AGO = "小时前: hour ago";
    private static final String ONE_HOURS_AGO = "小时前: hours ago";
    private static final String YESTERDAY = "昨天: yesterday";
    private static final String ONE_DAY_AGO = "天前: day ago";
    private static final String ONE_DAYS_AGO = "天前: days ago";
    private static final String ONE_MONTH_AGO = "月前: month ago";
    private static final String ONE_MONTHS_AGO = "月前: months ago";
    private static final String ONE_YEAR_AGO = "年前: year ago";
    private static final String ONE_YEARS_AGO = "年前: years ago";
    
    public static String[] format(Date date,String lang) {
    	String[] result = new String[2];
        long delta = new Date().getTime() - date.getTime();
        String returnStr = "";
        long p = 0L;
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            p = (seconds <= 0 ? 1 : seconds);
            returnStr = (p==1 ?  ONE_SECOND_AGO : ONE_SECONDS_AGO);
        }else
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            p = (minutes <= 0 ? 1 : minutes);
            returnStr = (p==1 ?  ONE_MINUTE_AGO : ONE_MINUTES_AGO);
        }else
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            p = (hours <= 0 ? 1 : hours);
            returnStr = (p==1 ?  ONE_HOUR_AGO : ONE_HOURS_AGO);
        }else
        if (delta < 48L * ONE_HOUR) {
        	returnStr =  YESTERDAY;
        }else
        if (delta < 30L * ONE_DAY) {
            long days = toDays(delta);
            p = (days <= 0 ? 1 : days);
            returnStr = (p==1 ?  ONE_DAY_AGO : ONE_DAYS_AGO);
        }else
        if (delta < 12L * 4L * ONE_WEEK) {
            long months = toMonths(delta);
            p =  (months <= 0 ? 1 : months);
            returnStr = (p==1 ?  ONE_MONTH_AGO : ONE_MONTHS_AGO);
        } else {
            long years = toYears(delta);
            p = (years <= 0 ? 1 : years);
            returnStr = (p==1 ? ONE_YEAR_AGO :  ONE_YEARS_AGO);
        }
        
        if(StringsUtils.isEmpty(lang))
        {
        	result = returnStr.split(":");
        }
        if(p != 0) {
        	String[] ary = new String[] {p + result[0],p + result[1]};
        	result= ary;
        }
        return result;
    }
 
    private static long toSeconds(long date) {
        return date / 1000L;
    }
 
    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }
 
    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }
 
    private static long toDays(long date) {
        return toHours(date) / 24L;
    }
 
    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }
 
    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }


}
