package com.app.baselibrary.commonUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Desc:
 * Author: pl
 * Date: 2017-05-09 14:18
 */

public class DateTimeUtil {

    public static String[] weekdays = {"日", "一", "二", "三", "四", "五", "六"};

    // 返回当前timestamp所代表的天
    public static long getDate(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        long result = c.getTimeInMillis();
        return result;
    }

    // 返回当天最后的时间
    public static long getOneDayEnd(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 59);
        return c.getTimeInMillis();
    }

    public static String getMonthString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format(Locale.CHINA, "%d月", c.get(Calendar.MONTH) + 1);
    }

    public static String getDayOfMonthString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format(Locale.CHINA, "%d日", c.get(Calendar.DAY_OF_MONTH));
    }

    public static int getDayOfMonth(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    public static String getDayOfWeekString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return weekdays[c.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static String getTimeOfDayString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format(Locale.CHINA, "%02d:%02d", c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
    }

    /**
     * @param timestamp
     * @return "xx年xx月xx日 xx:xx"
     */
    public static String getFullTimeString(long timestamp) {
        Calendar c = Calendar.getInstance();
        String today = String.format("%s.%s.%s", c.get(Calendar.YEAR), (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH));
        c.setTimeInMillis(timestamp);
        String time = String.format("%s.%s.%s", c.get(Calendar.YEAR), (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH));
        if (today.equals(time)) {
            return "今天 " + String.format(Locale.CHINA, "%02d:%02d", c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
        } else {
            return String.format(Locale.CHINA, "%s.%s.%s  %02d:%02d", c.get(Calendar.YEAR), (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
        }
    }

    /**
     * @param timestamp
     * @return "xx年xx月xx日"
     */
    public static String getSimpleTimeString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format("%s年%s月%s日", c.get(Calendar.YEAR), (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * @param timestamp
     * @return "xx年xx月xx日"
     */
    public static String getBirthdayString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format("%s月%s日", (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * @param timestamp
     * @return "xxxx-xx-xx xx:xx:xx"
     */
    public static String getDigitalTimeString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format(Locale.CHINA, "%s.%s.%s  %02d:%02d:%02d",
                c.get(Calendar.YEAR),
                (c.get(Calendar.MONTH) + 1),
                c.get(Calendar.DAY_OF_MONTH),
                c.get(Calendar.HOUR_OF_DAY),
                c.get(Calendar.MINUTE),
                c.get(Calendar.SECOND));
    }

    /**
     * @param timestamp
     * @return "xx:xx"
     */
    public static String getHourMinutes(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format(Locale.CHINA, "%02d:%02d",
                c.get(Calendar.HOUR_OF_DAY),
                c.get(Calendar.MINUTE));

    }

    /**
     * @param timestamp
     * @return "YYYY-MM-DD"
     */
    public static String getSimpleDigitalTimeString(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        return String.format(Locale.CHINA, "%s.%s.%s", c.get(Calendar.YEAR), (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * 计算两个日期之间相差的天数 timeFrom < timeTo
     *
     * @param timeFrom
     * @param timeTo
     * @return
     */
    public static int getDaysBetween(long timeFrom, long timeTo) {

        long between_days = (timeTo - timeFrom) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    public static long getTodayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        return calendar.getTimeInMillis();
    }

    /**
     * 判断当天在0:00:00 ~ 24:00:00 之间
     *
     * @param time long
     * @return flag
     */
    public static boolean isToday(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return calendar.after(getTodayStart()) && calendar.before(getTodayEnd());
    }

    // 获得当天24点时间
    public static long getTodayEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    //FIXME: 应该实现为天减一，否则在闰秒时出错
    public static Calendar getYesterdayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(calendar.getTimeInMillis() - 24L * 60L * 60L * 1000L);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        return calendar;
    }

    //FIXME: 应该实现为天减一，否则在闰秒时出错
    public static Calendar getTomorrowStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(calendar.getTimeInMillis() + 24L * 60L * 60L * 1000L);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        return calendar;
    }

    public static Calendar getThisWeekStart() {
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        if (dayOfWeek == 1) {
            // 周日，需要特殊处理
            calendar.setTimeInMillis(calendar.getTimeInMillis() - 6L * 24L * 60L * 60L * 1000L);
        } else {
            calendar.setTimeInMillis(calendar.getTimeInMillis() - (dayOfWeek - 2) * 24L * 60L * 60L * 1000L);
        }
        // calendar.

        return calendar;
    }

    public static Calendar getThisMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        return calendar;
    }

    public static Calendar getThisQuarterStart() {
        Calendar calendar = Calendar.getInstance();
        int monthOfYear = calendar.get(Calendar.MONTH);
        monthOfYear /= 3;
        monthOfYear *= 3;
        calendar.set(Calendar.MONTH, monthOfYear);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        return calendar;
    }

    public static Calendar getLastQuarterStart() {
        Calendar calendar = getThisQuarterStart();
        calendar.add(Calendar.MONTH, -3);
        return calendar;
    }

    public static Calendar getLastQuarterEnd() {
        Calendar calendar = getThisQuarterStart();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar;
    }

    public static String formatDate(Calendar calendar) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
        return format.format(calendar.getTime());
    }

    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
        return format.format(date);
    }

    public static String getToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(getTodayStart());
        return formatDate(calendar);
    }

    /**
     * String日期转换为Long
     * * @param formatDate("MM/dd/yyyy HH:mm:ss")
     * * @param date("3/19/2015 00:00:00")
     * * @return
     * * @throws ParseException
     */
    public static long stringToLong()
            throws ParseException {
        Calendar calendar = Calendar.getInstance();
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        if (month.length() < 2) {
            month = "0" + month;
        }
        String today = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        if (today.length() < 2)
            today = "0" + today;
        String day = month + "/" + today + "/" + calendar.get(Calendar.YEAR);
        DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.CHINA);
        Date dt = sdf.parse(day + " 00:00:00");
        return dt.getTime();
    }

    public static String formatTime(long millis) {
        long s = millis / 1000;
        if (s < 60) {
            return s + "秒";
        } else {

            int minute = (int) (s / 60);
            int second = (int) (s % 60);

            return minute + "分" + second + "秒";
        }


    }

    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
    }

    //获取大约多少分钟  传入的是秒
    public static String formatTimeMinute(long millis) {
        if (millis < 60) {
            return millis + "秒";
        } else {

            int minute = (int) (millis / 60);
            int second = (int) (millis % 60);
            return minute + "分钟";
        }


    }

    public static long getCuttentTimeLong() {
        return System.currentTimeMillis();
    }
}
