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 {
    private static final DateFormat YEAR_DATE_FORMATTER = new SimpleDateFormat("yyyyMMdd");
    private static final DateFormat MONTH_DATE_FORMATTER = new SimpleDateFormat("M月d日");
    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 formatMonthDateWithWeekDay(Date time) {
        StringBuilder builder = new StringBuilder();
        builder.append(MONTH_DATE_FORMATTER.format(time))
                .append("(")
                .append(TimeUtil.getWeekDay(time))
                .append(")");

        return builder.toString();
    }

    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 YEAR_DATE_FORMATTER.format(day1).equals(YEAR_DATE_FORMATTER.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 String formatYearMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;

        if (month < currentMonth) return String.format("%d-%02d", currentYear + 1, month);
        return String.format("%d-%02d", currentYear, month);
    }

    public static String formatNextYearMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;

        int nextMonth = month + 1;
        nextMonth = nextMonth > 12 ? nextMonth - 12 : nextMonth;

        if (month < currentMonth || nextMonth < month) return String.format("%d-%02d", currentYear + 1, nextMonth);
        return String.format("%d-%02d", currentYear, nextMonth);
    }

    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) > 15;
    }

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

@@ -355,97 +355,8 @@
 %22);%0A
-    private static final DateFormat MONTH_DATE_FORMATTER = new SimpleDateFormat(%22M%E6%9C%88d%E6%97%A5%22);%0A
     
@@ -443,16 +443,16 @@
 %22%E5%91%A8%E5%85%AD%22 %7D;%0A
+
     priv
@@ -986,337 +986,8 @@
  %7D%0A%0A
-    public static String formatMonthDateWithWeekDay(Date time) %7B%0A        StringBuilder builder = new StringBuilder();%0A        builder.append(MONTH_DATE_FORMATTER.format(time))%0A                .append(%22(%22)%0A                .append(TimeUtil.getWeekDay(time))%0A                .append(%22)%22);%0A%0A        return builder.toString();%0A    %7D%0A%0A
     
@@ -1540,906 +1540,8 @@
  %7D%0A%0A
-    public static String formatYearMonth(int month) %7B%0A        Calendar calendar = Calendar.getInstance();%0A        int currentYear = calendar.get(Calendar.YEAR);%0A        int currentMonth = calendar.get(Calendar.MONTH) + 1;%0A%0A        if (month %3C currentMonth) return String.format(%22%25d-%2502d%22, currentYear + 1, month);%0A        return String.format(%22%25d-%2502d%22, currentYear, month);%0A    %7D%0A%0A    public static String formatNextYearMonth(int month) %7B%0A        Calendar calendar = Calendar.getInstance();%0A        int currentYear = calendar.get(Calendar.YEAR);%0A        int currentMonth = calendar.get(Calendar.MONTH) + 1;%0A%0A        int nextMonth = month + 1;%0A        nextMonth = nextMonth %3E 12 ? nextMonth - 12 : nextMonth;%0A%0A        if (month %3C currentMonth %7C%7C nextMonth %3C month) return String.format(%22%25d-%2502d%22, currentYear + 1, nextMonth);%0A        return String.format(%22%25d-%2502d%22, currentYear, nextMonth);%0A    %7D%0A%0A
     
@@ -2505,19 +2505,20 @@
 thday) %3E
+=
  1
-5
+8
 ;%0A    %7D%0A
@@ -2608,9 +2608,9 @@
  %3C 1
-5
+8
 ;%0A  

refactor TimeUtil