package cn.momia.common.util;

import com.alibaba.fastjson.util.TypeUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {
    public static class TimeUnit {
        public static final int MONTH = 1;
        public static final int QUARTER = 2;
        public static final int YEAR = 3;
    }

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

    public static final DateFormat YEAR_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    public static final DateFormat MONTH_DATE_FORMAT = new SimpleDateFormat("MM-dd");
    public static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");

    private static final DateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
    private static final String[] WEEK_DAYS = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
    private static final String[] AM_PM = { "上午", "下午" };

    public static List<Date> castToDates(List<String> timeStrs) {
        List<Date> times = new ArrayList<Date>();
        for (String timeStr : timeStrs) {
            Date time = castToDate(timeStr);
            if (time != null) times.add(time);
        }

        return times;
    }

    public static Date castToDate(String timeStr) {
        try {
            return TypeUtils.castToDate(timeStr);
        } catch (Exception e) {
            return null;
        }
    }

    public static String getWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return WEEK_DAYS[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static boolean isSameDay(Date day1, Date day2) {
        return SHORT_DATE_FORMAT.format(day1).equals(SHORT_DATE_FORMAT.format(day2));
    }

    public static String getAmPm(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return AM_PM[calendar.get(Calendar.AM_PM)];
    }

    public static float getAge(Date birthday) {
        Calendar calendar = Calendar.getInstance();
        int yearNow = calendar.get(Calendar.YEAR);
        int monthNow = calendar.get(Calendar.MONTH);
        calendar.setTime(birthday);
        int yearBorn = calendar.get(Calendar.YEAR);
        int monthBorn = calendar.get(Calendar.MONTH);

        int year = yearNow - yearBorn;
        if (year >= 1) return year;

        int month = monthNow - monthBorn;
        if (month > 0 && month < 1) month = 1;
        return month / 12.0F;
    }

    public static String formatAge(Date birthday) {
        float age = getAge(birthday);

        if (age <= 0) {
            return "未出生";
        } else if (age > 0 && age < 1) {
            int month = (int) (age * 12);
            if (month == 0) month = 1;
            return month + "个月";
        } else {
            return ((int) age) + "岁";
        }
    }

    public static boolean isAdult(Date birthday) {
        return getAge(birthday) >= 18;
    }

    public static boolean isChild(Date birthday) {
        return getAge(birthday) < 18;
    }

    public static Date add(Date startTime, int time, int timeUnit) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);

        switch (timeUnit) {
            case TimeUnit.MONTH:
                calendar.add(Calendar.MONTH, time);
                break;
            case TimeUnit.QUARTER:
                calendar.add(Calendar.MONTH, time * 3);
                break;
            case TimeUnit.YEAR:
                calendar.add(Calendar.YEAR, time);
                break;
            default: break;
        }

        return calendar.getTime();
    }

    public static String formatAddTime(Date addTime) {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH);
        int curDay = calendar.get(Calendar.DATE);

        calendar.setTime(addTime);
        int addYear = calendar.get(Calendar.YEAR);
        int addMonth = calendar.get(Calendar.MONTH);
        int addDay = calendar.get(Calendar.DATE);

        if (addYear != curYear) return YEAR_DATE_FORMAT.format(addTime);
        if (addMonth != curMonth || addDay != curDay) return MONTH_DATE_FORMAT.format(addTime);
        return TIME_FORMAT.format(addTime);
    }
}

@@ -137,36 +137,8 @@
 at;%0A
-import java.util.ArrayList;%0A
 impo
@@ -186,31 +186,8 @@
 ate;
-%0Aimport java.util.List;
 %0A%0Apu
@@ -424,16 +424,21 @@
 TANDARD_
+DATE_
 FORMAT =
@@ -518,16 +518,22 @@
 eFormat 
+SHORT_
 DATE_FOR
@@ -611,24 +611,30 @@
 Format YEAR_
+MONTH_
 DATE_FORMAT 
@@ -842,98 +842,8 @@
 );%0A%0A
-    private static final DateFormat SHORT_DATE_FORMAT = new SimpleDateFormat(%22yyyyMMdd%22);%0A
     
@@ -993,298 +993,8 @@
 %7D;%0A%0A
-    public static List%3CDate%3E castToDates(List%3CString%3E timeStrs) %7B%0A        List%3CDate%3E times = new ArrayList%3CDate%3E();%0A        for (String timeStr : timeStrs) %7B%0A            Date time = castToDate(timeStr);%0A            if (time != null) times.add(time);%0A        %7D%0A%0A        return times;%0A    %7D%0A%0A
     
@@ -1747,16 +1747,385 @@
  static 
+String formatAge(Date birthday) %7B%0A        float age = getAge(birthday);%0A%0A        if (age %3C= 0) %7B%0A            return %22%E6%9C%AA%E5%87%BA%E7%94%9F%22;%0A        %7D else if (age %3E 0 && age %3C 1) %7B%0A            int month = (int) (age * 12);%0A            if (month == 0) month = 1;%0A            return month + %22%E4%B8%AA%E6%9C%88%22;%0A        %7D else %7B%0A            return ((int) age) + %22%E5%B2%81%22;%0A        %7D%0A    %7D%0A%0A    private static 
 float ge
@@ -2651,569 +2651,8 @@
  %7D%0A%0A
-    public static String formatAge(Date birthday) %7B%0A        float age = getAge(birthday);%0A%0A        if (age %3C= 0) %7B%0A            return %22%E6%9C%AA%E5%87%BA%E7%94%9F%22;%0A        %7D else if (age %3E 0 && age %3C 1) %7B%0A            int month = (int) (age * 12);%0A            if (month == 0) month = 1;%0A            return month + %22%E4%B8%AA%E6%9C%88%22;%0A        %7D else %7B%0A            return ((int) age) + %22%E5%B2%81%22;%0A        %7D%0A    %7D%0A%0A    public static boolean isAdult(Date birthday) %7B%0A        return getAge(birthday) %3E= 18;%0A    %7D%0A%0A    public static boolean isChild(Date birthday) %7B%0A        return getAge(birthday) %3C 18;%0A    %7D%0A%0A
     
@@ -3739,16 +3739,22 @@
 rn YEAR_
+MONTH_
 DATE_FOR

refactor TimeUtil