package com.genlot.ninglottery.toolkit;

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


public class TimeUtils {
    private static final String DETAIL_FORMAT = "yyyy/MM/dd-HH:mm:ss";
    private static final String SHORT_FORMAT = "yyyy/MM/dd";

    //将秒转换成具体时间
    public static String getDetailTime(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat(DETAIL_FORMAT, Locale.CHINA);
        Date date = new Date(time * 1000);
        return sdr.format(date);
    }

    //将豪秒转换成具体时间
    public static String getDetailTimeMil(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat(DETAIL_FORMAT, Locale.CHINA);
        Date date = new Date(time);
        return sdr.format(date);
    }

    //根据秒数转化为时分秒   00:00:00
    public static String getTime(int second) {
        if (second <= 0) {
            return "00:00";
        }
        if (second < 10) {
            return "00:0" + second;
        }
        if (second < 60) {
            return "00:" + second;
        }
        if (second < 3600) {
            int minute = second / 60;
            second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "0" + minute + ":0" + second;
                }
                return "0" + minute + ":" + second;
            }
            if (second < 10) {
                return minute + ":0" + second;
            }
            return minute + ":" + second;
        }
        int hour = second / 3600;
        int minute = (second - hour * 3600) / 60;
        second = second - hour * 3600 - minute * 60;
        if (hour < 10) {
            if (minute < 10) {
                if (second < 10) {
                    return "0" + hour + ":0" + minute + ":0" + second;
                }
                return "0" + hour + ":0" + minute + ":" + second;
            }
            if (second < 10) {
                return "0" + hour + ":" + minute + ":0" + second;
            }
            return "0" + hour + ":" + minute + ":" + second;
        }
        if (minute < 10) {
            if (second < 10) {
                return hour + ":0" + minute + ":0" + second;
            }
            return hour + ":0" + minute + ":" + second;
        }
        if (second < 10) {
            return hour + ":" + minute + ":0" + second;
        }
        return hour + ":" + minute + ":" + second;
    }

    //20190812000000
    public static String getCashEndTime(long time) {
        if (String.valueOf(time).length() < 14) {
            return String.valueOf(time);
        }
        StringBuilder sb = new StringBuilder();
        String endTime = String.valueOf(time);
        sb.append(endTime.substring(0, 4)).append("/")
            .append(endTime.substring(4,6)).append("/")
            .append(endTime.substring(6,8)).append(" ")
            .append(endTime.substring(8,10)).append(":")
            .append(endTime.substring(10,12)).append(":")
            .append(endTime.substring(12,14));
        return sb.toString();
    }

    public static String getShortTime(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat(SHORT_FORMAT, Locale.CHINA);
        Date date = new Date(time * 1000);
        return sdr.format(date);
    }

    /**
     * 获得该日的开始时间
     * @param time 年月日
     * @return  秒
     */
    public static long getTimeStart(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
        Date date;
        long times = 0;
        try {
            date = sdr.parse(time);
            times = date.getTime() / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    /**
     * 获得该日的结束时间
     * @param time 年月日
     * @return  秒
     */
    public static long getTimeEnd(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
        Date date;
        long times = 0;
        try {
            date = sdr.parse(time);
            Calendar dayEnd = Calendar.getInstance();
            dayEnd.setTime(date);
            dayEnd.set(Calendar.HOUR_OF_DAY, 23);
            dayEnd.set(Calendar.MINUTE, 59);
            dayEnd.set(Calendar.SECOND, 59);
            dayEnd.set(Calendar.MILLISECOND, 999);
            times = dayEnd.getTime().getTime() / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    /**
     * 获取今天的最后时间 23:59:59 以秒为单位
     */
    public static long getLastTimeOfToday() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime() / 1000;
    }

    /**
     * 获取今天的开始时间 00:00:00 以秒为单位
     */
    public static long getStartTimeOfToday() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime() / 1000;
    }

    /**
     * 获取明天9点的时间
     * @return
     */
    public static long getNineOfTommory() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);

        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }

    //时间戳转字符串
    public static String getStrTime(String timeStamp) {
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        long l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }

    //判断当天
    public static boolean isCurrentDay(String timeStr) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        String time = String.valueOf(cal.get(Calendar.YEAR)) + "-" + String.format(Locale.getDefault(), "%02d",
                cal.get(Calendar.MONTH) + 1) + "-" + String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if (time.equals(timeStr)) {
            return true;
        } else {
            return false;
        }
    }

    /*获取星期几*/
    public static String getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            case 7:
                return "周六";
            default:
                return "";
        }
    }
}
