package com.engine.salary.formlua.util;

import com.engine.salary.formlua.entity.parameter.DataType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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


public class DateUtil {
	protected static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
	private static String[] parsePatterns = {"yyyy-MM-dd","yyyy年MM月dd日",
			"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
			"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd","yy-MM","yy/MM"};
	private static String[] dateComponentKeyList={"DateComponent","DateInterval","TimeComponent"};
	public static void main(String args[]){
	}
	/**
	 * 构建日期对象
	 * @param dateChar
	 * @return
	 */
	public static Date buildDateByDateChar(String dateChar){

		Date resultDate=null;
		if(StringUtils.isEmpty(dateChar)){
			return resultDate;
		}
		String formatString=buildFormat(dateChar);
		if(StringUtils.isEmpty(formatString)){
			return resultDate;
		}
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(formatString);
		simpleDateFormat.setLenient(false);
		try {
			resultDate=simpleDateFormat.parse(dateChar);
		} catch (ParseException e) {
			logger.error("err",e);
		}
		return resultDate;
	}

	/**
	 * 判断是否是日期控件
	 * @param data
	 * @return
	 */
	public static boolean isDateComponent(Object data){
		if(data instanceof DataType){
			DataType dataType=(DataType)data;
			if(dataType.getComponentKey()!=null){
				for (String dateKey:dateComponentKeyList){
					if(dateKey.equalsIgnoreCase(dataType.getComponentKey())){
						return true;
					}
				}
			}
		}else if(data!=null && isValidDate(data+"")){
			return true;
		}

		return false;
	}


	public static boolean isValidDate(String str) {
		boolean convertSuccess=false;
		for(String s:parsePatterns){
			convertSuccess=parseDateString(str,s);
			if(convertSuccess){
				break;
			}
		}
		return convertSuccess;
	}

	public static boolean parseDateString(String dateString,String formatString){
		boolean convertSuccess=true;
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		try {
			format.setLenient(false);
			format.parse(dateString);
			logger.info(dateString);
		} catch (ParseException e) {
//			logger.error("err",e);
			convertSuccess=false;
		}
		return convertSuccess;
	}
	/**
	 * 判断日期时间的格式
	 * @param dateTimeStr
	 * @return
	 */
	public static String buildFormat(String dateTimeStr,String type){
		String fmateStr=null;
		int len=dateTimeStr.length();
		switch (type){
			case "Y":
				fmateStr="yyyy";
				break;
			case "M":
				fmateStr="yyyy-MM";
				break;
			case "D":
				fmateStr="yyyy-MM-dd";
				break;
			case "H":
				fmateStr="yyyy-MM-dd HH";
				break;
			case "I":
				fmateStr="yyyy-MM-dd HH:mm";
				break;
			case "S":
				fmateStr="yyyy-MM-dd HH:mm:ss";
				break;
		}
		return fmateStr;
	}

	public static String buildFormat(String dateTimeStr){
		String fmateStr=null;
		int len=dateTimeStr.length();
		switch (len){
			case 4:
				if(dateTimeStr.indexOf(":")<0){
					fmateStr="yyyy";
				}else if(dateTimeStr.indexOf("-")<0){
					fmateStr="H:mm";
				}
				break;
			case 5:
				if(dateTimeStr.indexOf(":")<0){
					fmateStr="yy-MM";
				}else if(dateTimeStr.indexOf("-")<0){
					fmateStr="HH:mm";
				}
				break;
			case 7:
				if(dateTimeStr.indexOf(":")<0){
					fmateStr="yyyy-MM";
				}else if(dateTimeStr.indexOf("-")<0){
					fmateStr="H:mm:ss";
				}
				break;
			case 8:
				fmateStr="HH:mm:ss";
				break;
			case 9:
				fmateStr="yyyy-MM-dd";
				break;
			case 10:
				fmateStr="yyyy-MM-dd";
				break;
			case 13:
				fmateStr="yyyy-MM-dd HH";
				break;
			case 16:
				fmateStr="yyyy-MM-dd HH:mm";
				break;
			case 19:
				fmateStr="yyyy-MM-dd HH:mm:ss";
				break;
			default:
				fmateStr="";
		}
		return fmateStr;
	}

	/**
	 * 判断参数类型，并且获取content属性的值，或者String类型直接返回
	 * @param obj
	 * @return
	 */
	public static String getContent(Object obj, SimpleDateFormat formatter){
		String date="";
		if(obj instanceof String || obj instanceof Character){
			date=obj.toString();
		}else if(obj instanceof Date){
			date=formatter.format(obj);
		}else{
			DataType dataType=(DataType)obj;
			Object content=dataType.getText();
			if(null!=content){
				date=content.toString();
			}else {
				date = dataType.getContent() == null ? null : dataType.getContent().toString();
			}
		}
		return date;
	}

	/**
	 * 计算日期间相隔天数
	 * @param start
	 * @param end
	 * @return
	 */
	public static int daysBetween(Date start, Date end) {
		if(start == null || end == null || start.getTime() >= end.getTime()) return 0;

		Date startDate = DateUtils.truncate(start, Calendar.DATE);
		return Days.daysBetween(new DateTime(startDate), new DateTime(end)).getDays();
	}
}
