package com.tofly.ghrq_route.common.util.date;

import android.annotation.SuppressLint;

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

@SuppressLint("SimpleDateFormat")
public class DateUtils {

    // 获取当前日期
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentDate() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(c.getTime());
    }

    public static String getToday() {
        String today = "未知";
        Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.DAY_OF_WEEK);
        switch (day) {
            case Calendar.SUNDAY:
                today = "星期天";
                break;
            case Calendar.MONDAY:
                today = "星期一";
                break;
            case Calendar.TUESDAY:
                today = "星期二";
                break;
            case Calendar.WEDNESDAY:
                today = "星期三";
                break;
            case Calendar.THURSDAY:
                today = "星期四";
                break;
            case Calendar.FRIDAY:
                today = "星期五";
                break;
            case Calendar.SATURDAY:
                today = "星期六";
                break;
        }
        return today;
    }

    // 获取当前日期
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentYearMonth() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(c.getTime());
    }

    public static int[] getYMDArray(String datetime, String splite) {
        int[] date = {0, 0, 0, 0, 0};
        if (datetime != null && datetime.length() > 0) {
            String[] dates = datetime.split(splite);
            int position = 0;
            for (String temp : dates) {
                date[position] = Integer.valueOf(temp);
                position++;
            }
        }
        return date;
    }

    /**
     * 将当前时间戳转化为标准时间函数
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTime(String time1) {

        int timestamp = Integer.parseInt(time1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = null;
        try {
            String str = sdf.format(new Timestamp(intToLong(timestamp)));
            time = str.substring(11, 16);
            String month = str.substring(5, 7);
            String day = str.substring(8, 10);
            time = getDate(month, day) + time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getLongTime(String time1) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = time1;
        long timestamp = 0;
        try {
            timestamp = Long.parseLong(time1);
            time = sdf.format(new Timestamp(timestamp));
        } catch (NumberFormatException e) {
        }
        return time;
    }

    @SuppressLint("SimpleDateFormat")
    public static Date getStrTimeDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
        }
        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public static long getStrTimeLong(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timestamp = 0;
        try {
            Date date = sdf.parse(time);
            if (date != null){
                timestamp = date.getTime();
            }
        } catch (ParseException e) {
        }
        return timestamp;
    }

    public static String formatMSTime(String time) {
        String dstTime = time;
        if (time != null && !time.isEmpty()) {
            SimpleDateFormat sdfOrign = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS");
            SimpleDateFormat sdfDst = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = sdfOrign.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
                return dstTime;
            }
            dstTime = sdfDst.format(date);
        }
        return dstTime;
    }

    //根据给定的原时间格式，转换成新的时间格式："yyyy-MM-dd HH:mm:ss"
    public static String formatToTime(String time, String format) {
        String dstTime = time;
        if (time != null && !time.isEmpty()) {
            String srcformat = "yyyy-MM-dd HH:mm:ss";
            String dstFormat = "yyyy-MM-dd HH:mm:ss";
            if (format != null && !format.isEmpty())
                srcformat = format;
            if (srcformat.equals(dstFormat))
                return dstTime;
            SimpleDateFormat sdfOrign = new SimpleDateFormat(srcformat);
            SimpleDateFormat sdfDst = new SimpleDateFormat(dstFormat);
            Date date = null;
            try {
                date = sdfOrign.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
                return dstTime;
            }
            dstTime = sdfDst.format(date);
        }
        return dstTime;
    }

    public static String formatTimeYMD(String time) {
        String dstTime = formatTime(time, null, "yyyy-MM-dd");
        return dstTime;
    }


    public static String formatTime(String time) {
        return formatTime(time, null, null);
    }

    public static String formatTime(String time, String inputformat) {
        return formatTime(time, inputformat, null);
    }

    public static String formatTime(String time, String inputformat, String outformat) {
        String dstTime = time;
        if (time != null && !time.isEmpty()) {
            String format = "yyyy-MM-dd HH:mm:ss";
            if (inputformat != null && !inputformat.isEmpty())
                format = inputformat;
            String dstFormat = "yyyy-MM-dd HH:mm:ss";
            if (outformat != null && !outformat.isEmpty())
                dstFormat = outformat;
            SimpleDateFormat sdfOrign = new SimpleDateFormat(format);
            SimpleDateFormat sdfDst = new SimpleDateFormat(dstFormat);
            Date date = null;
            try {
                date = sdfOrign.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
                return dstTime;
            }
            dstTime = sdfDst.format(date);
        }
        return dstTime;
    }

    public static String getTime(int timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = null;
        try {
            String str = sdf.format(new Timestamp(intToLong(timestamp)));
            time = str.substring(11, 16);

            String month = str.substring(5, 7);
            String day = str.substring(8, 10);
            time = getDate(month, day) + time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String getHMS(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String time = null;
        try {
            return sdf.format(new Date(timestamp));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将当前时间戳转化为标准时间函数
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getHMS(String time) {

        long timestamp = Long.parseLong(time);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        try {
            String str = sdf.format(new Timestamp(timestamp));
            return str;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    // java Timestamp构造函数需传入Long型
    public static long intToLong(int i) {
        long result = (long) i;
        result *= 1000;
        return result;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getDate(String month, String day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 24小时制
        Date d = new Date();
        ;
        String str = sdf.format(d);
        @SuppressWarnings("unused")
        String nowmonth = str.substring(5, 7);
        String nowday = str.substring(8, 10);
        String result = null;

        int temp = Integer.parseInt(nowday) - Integer.parseInt(day);
        switch (temp) {
            case 0:
                result = "今天";
                break;
            case 1:
                result = "昨天";
                break;
            case 2:
                result = "前天";
                break;
            default:
                StringBuilder sb = new StringBuilder();
                sb.append(Integer.parseInt(month) + "月");
                sb.append(Integer.parseInt(day) + "日");
                result = sb.toString();
                break;
        }
        return result;
    }

    /* 将字符串转为时间戳 */
    public static String getTimeToStamp(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒",
                Locale.CHINA);
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String tmptime = String.valueOf(date.getTime()).substring(0, 10);

        return tmptime;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getYMD() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(c.getTime());
    }

    @SuppressLint("SimpleDateFormat")
    public static String getChinaYMD() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(c.getTime());
    }

    @SuppressLint("SimpleDateFormat")
    public static String getYMD(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(timestamp));
    }

    @SuppressLint("SimpleDateFormat")
    public static long getTimeFromYMD(String ymd) {
        long time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(ymd);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            time = calendar.getTime().getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String getDate(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(new Date(timestamp));
    }

    public static String getDateMS() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.CHINA);
        return sdf.format(new Date());
    }

    public static String getDateMS(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.CHINA);
        return sdf.format(new Date(timestamp));
    }

    public static String getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(new Date());
    }

    public static String getTodayMinTime(){
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String ymd = sdf.format(c.getTime());
        return String.format("%s 00:00:00", ymd);
    }

    public static String getTodayMaxTime(){
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String ymd = sdf.format(c.getTime());
        return String.format("%s 23:59:59", ymd);
    }

    public static String getDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
        return sdf.format(new Date());
    }

    public static String getDateTimeMS() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS", Locale.CHINA);
        return sdf.format(new Date());
    }

    public static String getDateTimeMS(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS", Locale.CHINA);
        return sdf.format(new Date(timestamp));
    }

    public static String getTimestamp() {
        long time = System.currentTimeMillis() / 1000;
        return String.valueOf(time);
    }

    public static void formatTime(String orign, String[] result) {
        String date, time;
        date = time = orign;
        if (orign.contains("T")) {
            date = orign.split("T")[0];
            time = orign.split("T")[1];
        } else if (orign.contains(" ")) {
            date = orign.split(" ")[0];
            time = orign.split(" ")[1];
        }
        result[0] = date;
        result[1] = time;
    }

    public static long getUTCTime() {
        //取得本地时间
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        //取得时间偏移量
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        //取得夏令时差
        int dstOffset = cal.get(Calendar.DST_OFFSET);
        //从本地时间里扣除这些差量，即可以取得UTC时间
        cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        return cal.getTimeInMillis();
    }

    public static long getTimeOffset(String time1, String time2) {
        long offset = getTimeOffset(time1, time2, null);
        return offset;
    }

    public static long getTimeOffset(String time1, String time2, String timeformat) {
        long offset = 0;
        if (time1 != null && !time1.isEmpty()
                && time2 != null && !time2.isEmpty()) {
            String format = "yyyy-MM-dd HH:mm:ss";
            if (timeformat != null && !timeformat.isEmpty())
                format = timeformat;
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                Date date = sdf.parse(time1);
                Date date2 = sdf.parse(time2);
                offset = date2.getTime() - date.getTime();
                offset = (offset >= 0) ? offset : -offset;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return offset;
    }

    public static int getTimeCompare(String time1, String time2) {
        int compare = getTimeCompare(time1, time2, null);
        return compare;
    }

    public static int getTimeCompare(String time1, String time2, String timeformat) {
        int compare = 0;
        if (time1 != null && !time1.isEmpty()
                && time2 != null && !time2.isEmpty()) {
            String format = "yyyy-MM-dd HH:mm:ss";
            if (timeformat != null && !timeformat.isEmpty())
                format = timeformat;
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                Date date = sdf.parse(time1);
                Date date2 = sdf.parse(time2);
                compare = date.compareTo(date2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return compare;
    }

//多少天前的日期
    public static String getDaysAgoYMD(int day) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_YEAR, -day + 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(instance.getTime());
    }

    /**
     * Calendar转换为String
     */
    public static String gregorianCalendarToString(GregorianCalendar calendar){
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }

}