package net.northking.cctp.common.util;

import org.springframework.util.StringUtils;

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

/**
 * @Title:
 * @Description:
 * @Company: 京北方信息技术股份有限公司
 * @Author: 谢秀松
 * @CreateDate: 2018/3/21
 * @UpdateUser:
 * @Version: 1.0
 */
public class DateUtil
{
  protected static final String[] REGEXS = new String[]{
      // 无时分秒
      "^-?\\d{1,4}-\\d{1,2}-\\d{1,2}$",
      "^-?\\d{1,4}/\\d{1,2}/\\d{1,2}$",
      // 有时分秒
      "^-?\\d{1,4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}$",
      "^-?\\d{1,4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}$",
      // 有时分秒还有毫秒
      "^-?\\d{1,4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}\\s\\d{3}$",
      "^-?\\d{1,4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}\\s\\d{3}$",
  };

  protected static final String[] PATTERN = new String[]{
      "yyyy-MM-dd",
      "yyyy/MM/dd",
      "yyyy-MM-dd hh:mm:ss",
      "yyyy/MM/dd hh:mm:ss",
      "yyyy-MM-dd hh:mm:ss SSS",
      "yyyy/MM/dd hh:mm:ss SSS"
  };
  public static final String YYYY_MM_DD = "yyyy-MM-dd";
  public static final String YYYY_MM_DD_24H_MI_SS = "yyyy-MM-dd hh:mm:ss";
  public static final String YYYY_MM_DD_24H_MI_SS_MS = "yyyy-MM-dd hh:mm:ss SSS";

  private DateUtil()
  {
  }

  public static String toYearMonthDay(Date date)
  {
    if (date == null)
    {
      return "";
    }
    return new SimpleDateFormat(YYYY_MM_DD).format(date);
  }

  public static String toYearMonthDay24HourMinSecond(Date date)
  {
    if (date == null)
    {
      return "";
    }
    return new SimpleDateFormat(YYYY_MM_DD_24H_MI_SS).format(date);
  }

  public static String toYearMonthDay24HourMinSecondAndMicroSecond(Date date)
  {
    if (date == null)
    {
      return "";
    }
    return new SimpleDateFormat(YYYY_MM_DD_24H_MI_SS_MS).format(date);
  }

  public static Date fromYearMonthDay(String dateStr) throws ParseException
  {
    if (StringUtils.isEmpty(dateStr))
    {
      return null;
    }
    return new SimpleDateFormat(YYYY_MM_DD).parse(dateStr);
  }

  public static Date fromYearMonthDay24HourMinSecond(String dateStr) throws ParseException
  {
    if (StringUtils.isEmpty(dateStr))
    {
      return null;
    }
    return new SimpleDateFormat(YYYY_MM_DD_24H_MI_SS).parse(dateStr);
  }

  public static Date fromYearMonthDay24HourMinSecondAndMicroSecond(String dateStr) throws ParseException
  {
    if (StringUtils.isEmpty(dateStr))
    {
      return null;
    }
    return new SimpleDateFormat(YYYY_MM_DD_24H_MI_SS_MS).parse(dateStr);
  }

  public static Date parse(String dateStr, String pattern) throws ParseException
  {
    if (StringUtils.isEmpty(dateStr))
    {
      return null;
    }
    return new SimpleDateFormat(pattern).parse(dateStr);
  }

  public static Date parse(String date) throws ParseException
  {
    int idx = 0;
    for (String regex : DateUtil.REGEXS)
    {
      if (date.matches(regex))
      {
        return parse(date, PATTERN[idx]);
      }
      idx++;
    }
    throw new IllegalArgumentException("不可识别的日期格式");
  }

  /**
   * 创建今天的目录，例如： /2020/03/07/
   *
   * @return 目录
   */
  public static String createTodayDir()
  {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    String monthStr = "" + month;
    if (month < 10)
    {
      monthStr = "0" + monthStr;
    }
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String dayStr = "" + day;
    if (day < 10)
    {
      dayStr = "0" + dayStr;
    }
    return "/" + year + "/" + monthStr + "/" + dayStr + "/";
  }

}
