package com.qhclh.ytzh.utils;

import android.text.TextUtils;

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

/**
 * Created by jiangxk on 2016/12/28.
 */

public class DateUtils {


    public static SimpleDateFormat getCommonSimpleDateFormat() {
        return new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
    }

    public static SimpleDateFormat getCurrectimeSimpleDateFormat() {
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    }
    public static SimpleDateFormat getSpritSimpleDateFormat() {
        return new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    }

    public static SimpleDateFormat getCommonMinuteDateFormat() {
        return new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
    }


    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static String getCurrentDate() {
        return getCommonSimpleDateFormat().format(new Date());
    }
    public static String getCurrentTimeDate() {
        return getCurrectimeSimpleDateFormat().format(new Date());
    }

    public static String getCurrentMinuteDate() {
        return getCommonMinuteDateFormat().format(new Date());
    }


    /**
     * ERP后台时间格式转换为时间戳
     *
     * @param date ERP后台时间
     * @return 时间戳
     */
    public static long getLongDate(String date) {
        try {
            if (!TextUtils.isEmpty(date)) {
                return Long.parseLong(date.substring(date.indexOf("(") + 1, date.indexOf("+")));
            } else {
                return 0;
            }
        } catch (Exception e) {
            return 0;
        }
    }


    /**
     * 讲时间戳转换为yyyy年MM月dd日格式
     *
     * @param realDate 时间戳
     * @return
     */
    public static String getRealDate(long realDate) {
        if (realDate == 0) {
            return "";
        }
        Date date = new Date(realDate);
        return getCommonSimpleDateFormat().format(date);
    }

    /**
     * 将ERP时间格式转换为yyyy年MM月dd日格式
     *
     * @return
     */
    public static String getRealDate(String date) {
        long dateLong = getLongDate(date);
        if (dateLong == 0) {
            return "";
        }
        return getCommonSimpleDateFormat().format(dateLong);
    }

    /**
     * 将ERP时间格式转换为yyyy/MM/dd格式
     *
     * @return
     */
    public static String getSpritRealDate(String date) {
        long dateLong = getLongDate(date);
        if (dateLong == 0) {
            return "";
        }
        return getSpritSimpleDateFormat().format(dateLong);
    }

    /**
     * 获取当前时间毫秒数
     *
     * @return 当前时间
     */
    public static long getCurrentMillisecondDate() {
        return new Date().getTime();
    }


    /**
     * 获取ERP后台时间格式
     *
     * @param string
     * @return
     */
    public static String getERPMillisecond(String string) {
        try {
            if (!TextUtils.isEmpty(string)) {
                SimpleDateFormat simpleDateFormat = getCommonSimpleDateFormat();
                Date date = simpleDateFormat.parse(string);
                return "/Date(" + date.getTime() + "+0800)/";
            } else {
                return string;
            }
        } catch (ParseException e) {
            return string;
        }
    }


    /**
     * 获取当前时间的 ERP后台时间格式
     *
     * @return
     */
    public static String getCurrentERPMillisecond() {
        Date date = new Date(new Date().getTime());
        return "/Date(" + date.getTime() + "+0800)/";
    }



    public static final String PATTERN_DEFAULT = "yyyy年MM月dd日";
    public static final String PATTERN_DEFAULT_TIME = "yyyy年MM月dd日 HH:mm";
    public static final String PATTERN_SPRIT = "yyyy/MM/dd";
    public static final String PATTERN_TRANSVERSE = "yyyy-MM-dd";
    public static final String PATTERN_TRANSVERSE_TIME = "yyyy-MM-dd HH:mm";




    private static SimpleDateFormat getDateFormat(String pattern) {
        return new SimpleDateFormat(pattern, Locale.getDefault());
    }


    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        return getDateFormat(pattern).format(date);
    }



    public static String erpTimeToDateStr(String erpTime, String pattern) {
        Date date = erpTimeToDate(erpTime);
        return format(date, pattern);
    }

    /**
     * ERP后台时间格式转换为时间戳
     *
     * @param erpTime ERP后台时间
     * @return Date
     */
    public static Date erpTimeToDate(String erpTime) {
        try {
            if (!TextUtils.isEmpty(erpTime)) {
                long dateLong = Long.parseLong(erpTime.substring(erpTime.indexOf("(") + 1, erpTime.indexOf("+")));
                return new Date(dateLong);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取ERP后台时间格式
     *
     * @param millisecond 毫秒值
     * @return
     */
    public static String dateToErpTime(long millisecond) {
        return "/Date(" + millisecond + "+0800)/";
    }

    /**
     * 获取ERP后台时间格式
     *
     * @param date date
     * @return
     */
    public static String dateToErpTime(Date date) {
        return dateToErpTime(date.getTime());
    }


    /**
     * 获取ERP后台时间格式
     *
     * @param dateStr     yyyy-MM-dd
     * @param pattern     "yyyy-MM-dd"
     * @return   /Date(1491753600000+0800)/
     */
    public static String dateStrToErpTime(String dateStr, String pattern) {
        SimpleDateFormat dateFormat = getDateFormat(pattern);
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            date = new Date();
        }
        return dateToErpTime(date);
    }


    public static int dayBetweenTwoDate(Date date1, Date date2) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date1);
//        int day1 = calendar.get(Calendar.DAY_OF_YEAR);
//        calendar.setTime(date2);
//        int day2 = calendar.get(Calendar.DAY_OF_YEAR);
        long dateTime1 = date1.getTime();
        long dateTime2 = date2.getTime();

        int betweenDay = (int) ((dateTime1 - dateTime2) / (1000 * 60 * 60 * 24));

        return Math.abs(betweenDay);
    }

}
