package com.sheep.gamegroup.util;

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

/**
 * Created by ljy on 2018/3/8.
 */
public class TimeUtil {

    public static String FORMAT = "yyyy/MM/dd HH:mm";
    public static String FORMAT_DD = "yyyy/MM/dd";
    public static String FORMAT_DD_LINE = "yyyy-MM-dd";

    private static TimeUtil timeUtil;

    public static TimeUtil newInstance() {
        if (timeUtil == null)
            timeUtil = new TimeUtil();
        return timeUtil;
    }

    public static long getCurrentSeconds() {
        long ls = System.currentTimeMillis() / 1000;
        return ls;
    }

    public static String TimeStamp2Date(long time, String formats) {
        time = time < 10_000_000_000L ? time * 1000 : time;//秒的时间戳为10位数,要x1000  10_000_000_000L对应时间为2286-11-21 01:46:40
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(time));
        return date;
    }

    public static String[] getCalendarShowTime(long paramLong) {
        String[] localObject;
        String str = new SimpleDateFormat("yyyy:MM:dd", Locale.CHINA).format(new Date(paramLong));
        try {
            String[] arrayOfString = str.split(":");
            localObject = arrayOfString;
            if ((localObject != null) && (localObject.length == 3)) ;
            return localObject;
        } catch (Exception localException) {
            while (true)
                localException.printStackTrace();
        }
    }

    public static String[] getCalendarShowTime(String paramString) {
        try {
            long l = Long.valueOf(paramString);
            Calendar localCalendar = Calendar.getInstance();
            localCalendar.setTimeInMillis(1000L * l);
            return getCalendarShowTime(localCalendar.getTimeInMillis());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDate(String format) {
        return new SimpleDateFormat(format, Locale.CHINA).format(new Date());
    }

    public static Calendar getCalendar(int year, int moth, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, moth);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar;
    }

    public static String getDate(String formate, long paramLong) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(paramLong * 1000);
        Date date = c.getTime();
        String str = new SimpleDateFormat(formate, Locale.CHINA).format(date);
        return str;
    }

    /**
     * 毫秒值
     *
     * @param time
     * @return 时:分:秒
     */
    public static String getHours(long time) {
        long second = time / 1000;
        long hour = second / 60 / 60;
        long minute = (second - hour * 60 * 60) / 60;
        long sec = (second - hour * 60 * 60) - minute * 60;

        String rHour;
        String rMin;
        String rSs;
        // 时
        if (hour < 10) {
            rHour = "0" + hour;
        } else {
            rHour = hour + "";
        }
        // 分
        if (minute < 10) {
            rMin = "0" + minute;
        } else {
            rMin = minute + "";
        }
        // 秒
        if (sec < 10) {
            rSs = "0" + sec;
        } else {
            rSs = sec + "";
        }

        // return hour + "小时" + minute + "分钟" + sec + "秒";
        return rHour + ":" + rMin + ":" + rSs;

    }

    /**
     * 日期时间字符串转换成毫秒
     *
     * @return
     */
    public static long string2Long(String dateStr, String dateFormat) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat(dateFormat).parse(dateStr));
            return calendar.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }

    /**
     * time在x天内 x为正，代表x天前，为负，代表-x天后
     *
     * @param time 判断的时间点
     * @param x    从今天24点算起，x天内，x可以为负
     * @return
     */
    public static boolean isTimeInXDay(long time, int x) {
        time = time < 10_000_000_000L ? time * 1000 : time;//秒的时间戳为10位数,要x1000  10_000_000_000L对应时间为2286-11-21 01:46:40
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTimeInMillis(time);
        //今日24点的时间
        Calendar cur24Calendar = Calendar.getInstance();
        applyDayEnd(cur24Calendar);
        if (x < 0) {
            boolean isBeforeCur24 = timeCalendar.before(cur24Calendar);
            if (TestUtil.isDev())
                LogUtil.println("isTimeInXDay", "isBeforeCur24", isBeforeCur24, cur24Calendar.getTimeInMillis());
            cur24Calendar.add(Calendar.DAY_OF_MONTH, x);
            boolean isAfterXDay = timeCalendar.after(cur24Calendar);
            if (TestUtil.isDev())
                LogUtil.println("isTimeInXDay", "isAfterXDay", isAfterXDay, cur24Calendar.getTimeInMillis());
            return isBeforeCur24 && isAfterXDay;
        } else if (x > 0) {
            boolean isAfterCur24 = timeCalendar.after(cur24Calendar);
            if (TestUtil.isDev())
                LogUtil.println("isTimeInXDay", "isAfterCur24", isAfterCur24, cur24Calendar.getTimeInMillis());
            cur24Calendar.add(Calendar.DAY_OF_MONTH, x);
            boolean isBeforeXDay = timeCalendar.before(cur24Calendar);
            if (TestUtil.isDev())
                LogUtil.println("isTimeInXDay", "isBeforeXDay", isBeforeXDay, cur24Calendar.getTimeInMillis());
            return isBeforeXDay && isAfterCur24;
        }
        return false;
    }

    /**
     * time在x天前或者-x天后 x为正，代表x天前，为负，代表-x天后
     *
     * @param time 判断的时间点
     * @param x    从今天24点算起，x天内，x可以为负
     * @return
     */
    public static boolean isTimeXDay(long time, int x) {
        time = time < 10_000_000_000L ? time * 1000 : time;//秒的时间戳为10位数,要x1000  10_000_000_000L对应时间为2286-11-21 01:46:40
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTimeInMillis(time);
        //今日24点的时间
        Calendar cur24Calendar = Calendar.getInstance();
        applyDayEnd(cur24Calendar);
        if (x > 0) {
            cur24Calendar.add(Calendar.DAY_OF_MONTH, x);
            boolean isAfterXDay = timeCalendar.after(cur24Calendar);
            if (TestUtil.isDev())
                LogUtil.println("isTimeXDay", "isAfterXDay", isAfterXDay, cur24Calendar.getTimeInMillis());
            return isAfterXDay;
        } else if (x < 0) {
            cur24Calendar.add(Calendar.DAY_OF_MONTH, x);
            boolean isBeforeXDay = timeCalendar.before(cur24Calendar);
            if (TestUtil.isDev())
                LogUtil.println("isTimeXDay", "isBeforeXDay", isBeforeXDay, cur24Calendar.getTimeInMillis());
            return isBeforeXDay;
        }
        return false;
    }

    //时间转换为一天的开始，也是前一天的结束
    public static Calendar applyDayBegin(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    //时间转换为一天的结束，也是后一天的开始
    public static Calendar applyDayEnd(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    //获取当月总天数
    public static int getTotalDay(int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 2:
                return 28;
            default:
                return 30;
        }
    }

    public static String getDurationText(long duration) {
        if(duration > 3600)
            return String.format(Locale.CHINA, "%d:%02d:%02d", duration / 3600, duration / 60 % 60, duration % 60);
        else if(duration > 60)
            return String.format(Locale.CHINA, "%d:%02d", duration / 60, duration % 60);
        else if(duration > 0)
            return String.format(Locale.CHINA, "%d:%02d", 0, duration % 60);
        else
            return "00:01";
    }

    /**
     * 是否大于今天
     */
    public boolean big2Today(String dateStr, String dateFormat) {
        if (getDayBegin() == string2Long(dateStr, dateFormat)) {
            return true;
        }
        return false;
    }

    //获取当天的开始时间
    public long getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTime()).getTime();
    }

    // 将Unix时间戳转变为日期，如调用TimeStampToDate("1252639886", "yyyy-MM-dd
    // HH:mm:ss")返回值：2009-11-09 11:31:26

    public String TimeStamp2Date(Long timestamp, String formats) {
        String date = "";
        try {
            date = new java.text.SimpleDateFormat(formats).format(new java.util.Date(timestamp));

        } catch (NumberFormatException e) {

        }
        return date;
    }
}
