package com.tax.sign.utils;

import com.simple.tool.encrypt.util.Base64Util;
import com.simple.tool.encrypt.util.MD5Util;
import com.yxcheng.token.utils.StringUtil;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;

public class EncryptCommonUtil {
	
	/**
	 * @param a
	 * @param b
	 * @param c
	 * @param a1
	 * @param b1
	 * @param c1
	 * @return
	 */
	public static String checkOneInv(String a, String b, String c, String d, String e) {
		String cc = EncryptCommonUtil.gen(EncryptCommonUtil.moveTo(MD5Util.MD5Encode(EncryptCommonUtil.getXX(EncryptCommonUtil.gen(a,b)))),EncryptCommonUtil.getYY(EncryptCommonUtil.moveTo(EncryptCommonUtil.getXX(e+(a+c+d))))+Base64Util.encode((MD5Util.MD5Encode(EncryptCommonUtil.getXX(Base64Util.encode((EncryptCommonUtil.getXX(c)+b+Base64Util.encode((d+e).getBytes())).getBytes())))).getBytes())).toUpperCase();
		return cc;
	}
	
	/**
	 * @param a
	 * @param b
	 * @return
	 */
	public static String ckCode(String a, String b) {
		String cc = MD5Util.MD5Encode(a + b);
		String d = MD5Util.MD5Encode(a) + Base64Util.encode(MD5Util.MD5Encode(b).getBytes());
		String g = gen(d, cc);
		return gen((g + gen(a, d) + cc) , d);
	}
	
	/**
	 * @param a
	 * @param b
	 * @return
	 *
	 */
	public static String checkTaxno(String a, String b, String e) {		
		String c2 = MD5Util.MD5Encode(EncryptCommonUtil.getXX(a)+b);
		c2 = MD5Util.MD5Encode(EncryptCommonUtil.gen(c2,EncryptCommonUtil.getYY(a))+EncryptCommonUtil.moveTo(b)+Base64Util.encode((EncryptCommonUtil.getYY(EncryptCommonUtil.gen(b,a))).getBytes())+EncryptCommonUtil.gen(Base64Util.encode(c2.getBytes()),EncryptCommonUtil.getXX(EncryptCommonUtil.moveTo(c2.length()+b + e))) + e).toUpperCase();
		return c2;
	}
	
	public static String checkInvConf(String a, String b, String c) {
		String c2 = EncryptCommonUtil.gen(EncryptCommonUtil.gen(a.toUpperCase(),EncryptCommonUtil.getYY(MD5Util.MD5Encode(MD5Util.MD5Encode(a+c.length())))),c.toUpperCase()+Base64Util.encode((a+MD5Util.MD5Encode(a+b)).getBytes())+EncryptCommonUtil.getXX(EncryptCommonUtil.moveTo(c))).toUpperCase();
		return c2;
	}
	
	
	/**
	 * @param invkind
	 * @param invnum
	 * @param preEncrpt
	 * @return
	 */
	public static String detailCode(String invkind, String invnum, String preEncrpt) {
		return Base64Util.encode((Base64Util.encode(preEncrpt.getBytes()) 
				+ Base64Util.encode(MD5Util.MD5Encode(invkind + invnum).getBytes())
				+ Base64Util.encode((invkind + invnum).getBytes())).getBytes());
	}
	
	public static String moveTo(String str){
		 return MD5Util.MD5Encode(EncryptCommonUtil.moveEncrypt(str));
	}
	
	public static String moveEncrypt(String str) {
		String out;
		String sBase = "rhrewrchb";
		
		String wzwschallenge = MD5Util.MD5Encode(sBase);
		String wzwschallengex = Base64Util.encode(sBase.getBytes());
		String encoderchars = MD5Util.MD5Encode(wzwschallenge)+MD5Util.MD5Encode(wzwschallengex);
		
		int c1, c2, c3;
	    int len = str.length();
	    int i = 0;
	    out = "";
	    while (i < len) {
	        c1 =(int)str.charAt(i++) & 0xff;
	        if (i == len) {
	            out += encoderchars.charAt(c1 >> 2);
	            out += encoderchars.charAt((c1 & 0x3) << 4);
	            out += "==";
	            break;
	        }
	        c2 = (int)str.charAt(i++);
	        if (i == len) {
	            out += encoderchars.charAt(c1 >> 2);
	            out += encoderchars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));
	            out += encoderchars.charAt((c2 & 0xf) << 2);
	            out += "=";
	            break;
	        }
	        c3 = (int)str.charAt(i++);
	        out += encoderchars.charAt(c1 >> 2);
	        out += encoderchars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));
	        out += encoderchars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6));
	        out += encoderchars.charAt(c3 & 0x3f);
	    }
	    int hash = b( wzwschallenge, wzwschallengex);
	    return out+hash;
	}
	
	public static int b(String wzwschallenge,String wzwschallengex){
		String tmp = wzwschallenge + wzwschallengex;
		int hash = 0;
		int i = 0;
	    for (i = 0; i < tmp.length(); i++) {
	        hash += tmp.charAt(i);
	    }
	    hash *= 245;
	    hash += 963863;
	    return hash;
	}
	
	public static String gen(String e, String f) {
		String s = String.valueOf(e.trim().length());
		String b = MD5Util.MD5Encode(e);
		String c = MD5Util.MD5Encode(e) + f;
		String c1 = "402880bd5c76166f015c903ee811504e";
		return MD5Util.MD5Encode(b + c + c1 + s).toUpperCase();
	}
	
	/**
	 * @param oldStr
	 * @return
	 */
	public static String getXX(String oldStr) {
		if(oldStr.length() >= 12) {
			return oldStr.substring(0,10);
		} else {
			return oldStr.substring(0,2);
		}
	}
	
	public static String getYY(String oldStr) {
		return String.valueOf(oldStr.length() + (oldStr.length() * 5));
	}
	
	
	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
			"e", "f" };
	
	private static String getFormattedText(byte[] bytes) {  
        int len = bytes.length;  
        StringBuilder buf = new StringBuilder(len * 2);  
        for (int j = 0; j < len; j++) {  
            buf.append(hexDigits[(bytes[j] >> 4) & 0x0f]);  
            buf.append(hexDigits[bytes[j] & 0x0f]);  
        }  
        return buf.toString();  
    }
	
	public static String SHAEncode(String origin) {
		String resultString = null;
		String charsetname = "UTF-8";
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("SHA1");
			
			if (charsetname == null || "".equals(charsetname))
				resultString = getFormattedText(md.digest(resultString.getBytes()));
			else
				resultString = getFormattedText(md.digest(resultString.getBytes(charsetname)));
//			md.update(origin.getBytes());  
            return resultString;  
		} catch (Exception exception) {
		}
		return resultString;
	}
	
	public static String encode(String data) {
		return Base64Util.encode(data.getBytes());
	}

	public static String shaBcode(String str) {
		try {
			return EncryptCommonUtil.SHAEncode(Base64Util.encode(str.getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String getXcx(String oldStr) {
		if (oldStr.length() >= 20) {
			return oldStr.substring(0, 12);
		} else {
			if (oldStr.length() >= 8) {
				return oldStr.substring(0, 5);
			}
		}
		return oldStr;
	}

	public static String getLL(String oldStr) {
		return String.valueOf(oldStr.length() + (oldStr.length() * 3));
	}

	public static String getSlave(String oldStr) {
		return String.valueOf(oldStr.length() + (oldStr.length() * 6));
	}

	public static String getMoveAddDuJe(String je, double moveValue) {
		if (StringUtil.isFloatNumeric(je)) {
			BigDecimal bje = new BigDecimal(je);
			BigDecimal bmoveValue = new BigDecimal(moveValue);
			return String.valueOf(bje.add(bmoveValue).setScale(2, BigDecimal.ROUND_HALF_UP));
		}
		return je;
	}
}
