package com.eactive.eai.common.util;

import java.nio.ByteBuffer;

public class CodeUtil {

	// invalid bytes
	private static final byte[] invalidBytes = { (byte) 0x0E, (byte) 0x0F, (byte) 0xFF };

//	private static final byte SO = (byte) 0x0E;
//	private static final byte SI = (byte) 0x0F;
//	private static final byte FF = (byte) 0xFF;

	// 0x00 - 0x7F
	private static final byte ASC_S = (byte) 0x00;
	private static final byte ASC_E = (byte) 0x7F;

	// space
	private static final byte DEF_B = (byte) 0x20;

//	private static boolean debug = false;
	
	public static byte[] getHexIn0x(String line) {
		ByteBuffer bb = ByteBuffer.allocate(line.getBytes().length / 2);
		while (line.indexOf("0x") != -1) {
			bb.put(line.substring(0, line.indexOf("0x")).getBytes());
			String hex = line.substring(line.indexOf("0x"), line.indexOf("0x") + 4);
			bb.put((byte) Integer.parseInt(hex.substring(2), 16));
			line = line.substring(line.indexOf("0x") + 4);
		}
		bb.put(line.getBytes());
		byte[] dst = new byte[bb.limit()];
		bb.position(0);
		bb.get(dst);
		return dst;

	}

	public static byte[] getHex(String line) {
		ByteBuffer bb = ByteBuffer.allocate(line.getBytes().length / 2);
		while (line.length() > 0) {
			bb.put((byte) Integer.parseInt(line.substring(0, 2), 16));
			line = line.substring(2);
		}
		byte[] dst = new byte[bb.limit()];
		bb.position(0);
		bb.get(dst);
		return dst;
	}

	public static boolean checkInvalid(byte cbyte) {
		for (int i = 0; i < invalidBytes.length; i++) {
			if (invalidBytes[i] == cbyte)
				return true;
		}
		return false;
	}

	public static String scanBytes(byte[] source) {
		int index = 0;

//		if(debug) System.out.println("Source : [" + new String(source) +"]");

		byte[] convertedBytes = new byte[source.length];

		while (index < source.length) {
			if (checkInvalid(source[index])) {
//				if(debug) System.out.println("invalid : " + byte2Hex(source[index]) );
				convertedBytes[index] = DEF_B;
				index++;
			} else if (source[index] >= ASC_S && source[index] <= ASC_E) {
//				if(debug) System.out.println("Single : " + byte2Hex(source[index]) );
				convertedBytes[index] = source[index];
				index++;
			} else {
				if (index + 1 <= source.length - 1) {
					if (checkInvalid(source[index + 1])) {
//						if(debug) System.out.println("Multi, but broken");
						convertedBytes[index] = DEF_B;
						convertedBytes[index + 1] = DEF_B;
					} else if (source[index + 1] >= ASC_S && source[index + 1] <= ASC_E) {
						convertedBytes[index] = DEF_B;
						index++;
						continue;
					} else {

//						byte[] multiBytes = {source[index], source[index+1]};
//						if(debug) System.out.println("Multi  : " + bytes2Hex(multiBytes) );
						convertedBytes[index] = source[index];
						convertedBytes[index + 1] = source[index + 1];
					}
				} else {
					convertedBytes[index] = DEF_B;
					break;
				}
				index++;
				index++;
			}
		}

//		if(debug) System.out.println("Converted bytes2Hex  : "+convertedBytes.length+"[" + bytes2Hex(convertedBytes) +"]");
//		if(debug) System.out.println("Converted new String : "+new String(convertedBytes).getBytes().length+"[" + new String(convertedBytes) +"]");
		return new String(convertedBytes);
	}

	public static String byte2Hex(byte inByte) {
		String s = "";
		s = s + Integer.toHexString((inByte & 0xf0) >> 4) + Integer.toHexString(inByte & 0xf);

		return s.toUpperCase();
	}

	public static String bytes2Hex(byte inBytes[]) {
		String s = "";
		for (int i = 0; i < inBytes.length; i++)
			s = s + Integer.toHexString((inBytes[i] & 0xf0) >> 4) + Integer.toHexString(inBytes[i] & 0xf);

		return s.toUpperCase();
	}

	public static String str2Hex(String inStr) {
		if (inStr == null || inStr.equals("")) {
			return "";
		}
		String retStr = bytes2Hex(inStr.getBytes());
		return retStr;
	}

	public static String fullToHalf(String full) {
		String answer = null;
		int fullLength = full.length();
		char[] ch1 = new char[fullLength];
		full.getChars(0, fullLength, ch1, 0);
		for (int i = 0; i < fullLength; i++) {
			if (ch1[i] > 0xff00 && ch1[i] <= 0xff5e) {
				ch1[i] -= 0xfee0;
			} else if (ch1[i] == 0x3000)
				ch1[i] = 0x20;
		}
		answer = new String(ch1);

		// 뒷쪽 공백문자 trim
		char[] ch2 = new char[fullLength];
		full.getChars(0, fullLength, ch2, 0);
		for (int i = fullLength - 1; i >= 0; i--) {
			if (ch2[i] != 0x3000 && ch2[i] != 0x20) {
				answer = answer.substring(0, i + 1);
				break;
			}
			if (i == 0)
				answer = "";
		}
		return answer;
	}

	public static String halfToFull(String half) {

		String answer = null;

		int halfLength = half.length();

		char[] ch = new char[halfLength];
		half.getChars(0, halfLength, ch, 0);
		for (int i = 0; i < halfLength; i++) {
			if (ch[i] > 0x20 && ch[i] <= 0x7e) {
				ch[i] += 0xfee0;
			} else if (ch[i] == 0x20)
				ch[i] = 0x3000;
		}
		answer = new String(ch);

		return answer;
	}
	
//	   public static void main(String args[]) throws Exception {
//	   test();
//}

//private static void test() throws Exception {
//
//		String line2 ="";
//	    ArrayList<String> al = new ArrayList<String>();
//	    //al.add("0E20C8A83E2053204320C1FDB1DDB40F");
//	    //al.add("BFA9A1A1A3B1C0CEB4E7FFFFC3D6");
//	    //al.add("B8B6C1F6B8B720C0DFB8AEB4C2B0");
//	    al.add("C4BFC7C72FC3B5BEC8BDD6BFEBC12020");
//	    while (!al.isEmpty()){
//	    	line2 = al.remove(0);
//	    	byte [] src = getHex(line2); 
//	    	
//			System.out.println("byte   length="+ CommonLib.getDumpMessage(src)+"");
//			System.out.println("String length="+ CommonLib.getDumpMessage(new String(src).getBytes())+"");
//			System.out.println("scan   length="+ CommonLib.getDumpMessage( scanBytes(src).getBytes())+"");
//			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//	    }
//
//	}
}
