package com.jpsoft.shinestar.modules.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.jpsoft.shinestar.modules.base.entity.BusinessOut;
import com.jpsoft.shinestar.modules.business.entity.RequestForLeaveInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 字符串工具类，继承lang3字符串工具类
 * @author L.com
 */
public final class StringUtils extends org.apache.commons.lang3.StringUtils {

	public static String encode(String str){
		String encode=null;
		try {
			encode = URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encode;
	}

	/**
	 * 获取UUID，去掉`-`的
	 * @return {String}
	 *
	 */
	public static String generateStr () {
		return UUID.randomUUID().toString().replace("-", "");
	}


	/**
	 * 要求外部订单号必须唯一。
	 * @return {String}
	 */
	public synchronized static String getOutTradeNo() {
		SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
		Date date = new Date();
		String key = format.format(date);
		Integer ran = (int)((Math.random()*9+1)*100000);
		key = key +ran.toString();
	//	key = key.substring(0, 24);
		return key;
	}


	/**
	 * 审批编号生成。
	 * @return {String}
	 */
	public synchronized static String getApprovalNo() {
		SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
		Date date = new Date();
		String key = format.format(date);
		Integer ran = (int)((Math.random()*9+1)*100000);
		key = ran.toString() + key;
		//	key = key.substring(0, 24);
		return key;
	}

	/**
	 * 字符串格式化
	 *
	 * use: format("my name is {0}, and i like {1}!", "L.cm", "java")
	 *
	 * int long use {0,number,#}
	 *
	 * @param s
	 * @param args
	 * @return {String}转换后的字符串
	 */
	public static String format(String s, Object... args) {
		return MessageFormat.format(s, args);
	}

	/**
	 * 替换某个字符
	 * @param str
	 * @param regex
	 * @param args
	 * @return {String}
	 */
	public static String replace(String str,String regex,String... args){
		int length = args.length;
		for (int i = 0; i < length; i++) {
			str=str.replaceFirst(regex, args[i]);
		}
		return str;
	}

	/**
	 * 清理字符串，清理出某些不可见字符
	 * @param txt
	 * @return {String}
	 */
	public static String cleanChars(String txt) {
		return txt.replaceAll("[ 　	`·•�\\f\\t\\v]", "");
	}

	/**
	 *  随机字符串
	 */
	private static final String INT_TEMP = "0123456789";
	private static final String STR_TEMP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	private static final String ALL_TEMP = INT_TEMP + STR_TEMP;

	private static final Random RANDOM = new Random();

	/**
	 * 生成的随机数类型
	 */
	public enum RandomType {
		/**
		 * 整数
		 */
		INT,
		/**
		 * 字符串
		 */
		STRING,
		/**
		 * 所有类型
		 */
		ALL
	}

	/**
	 * 随机数生成
	 * @param count
	 * @return {String}
	 */
	public static String random(int count, RandomType randomType) {
		if (count == 0) {
			return "";
		}
		if (count < 0) {
			throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
		}
		char[] buffer = new char[count];
		for (int i = 0; i < count; i++) {
			if (randomType.equals(RandomType.INT)) {
				buffer[i] = INT_TEMP.charAt(RANDOM.nextInt(INT_TEMP.length()));
			} else if (randomType.equals(RandomType.STRING)) {
				buffer[i] = STR_TEMP.charAt(RANDOM.nextInt(STR_TEMP.length()));
			}else {
				buffer[i] = ALL_TEMP.charAt(RANDOM.nextInt(ALL_TEMP.length()));
			}
		}
		return new String(buffer);
	}

	public static String strValue(Object object,String defaultValue) {
		if (object!=null){
			return String.valueOf(object);
		}
		else{
			return defaultValue;
		}
	}

	public static String toUnicode(String str) {
		char[] chars = str.toCharArray();

		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < chars.length; i++) {
			sb.append(Integer.toString(chars[i], 16));
		}

		return sb.toString();
	}

	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Double days = Double.parseDouble("1");
		EvaluationContext ctx = new StandardEvaluationContext();
		ctx.setVariable("days", days);
		ctx.setVariable("leaveType", "2");

		//1：事假，2：病假，3：其它，4：调休，5:婚假，6：丧假

		//大于3天或请假类型是病假、婚假、丧假的判断条件
		Boolean value1 = (Boolean) parser.parseExpression("#days>=3 || (#leaveType=='2' || #leaveType=='5' || #leaveType=='6')").getValue(ctx);
		System.out.println(value1);

		//小于3天并且请假类型不是病假、婚假、丧假的判断条件
		Boolean value2 = (Boolean) parser.parseExpression("#days<3 && (#leaveType!='2' && #leaveType!='5' && #leaveType!='6')").getValue(ctx);
		System.out.println(value2);
	}
}