package com.eactive.eai.transformer.util;

public class JsonUtil {
	private JsonUtil() {
		
	}
	public static String escape(String value) throws Exception {
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<value.length(); i++) {
			char ch = value.charAt(i);
			int chx = (int)ch;
			
			if(ch == '\n') {
				sb.append("\\n");
			}
			else if(ch == '\t') {
				sb.append("\\t");
			}
			else if(ch == '\r') {
				sb.append("\\r");
			}
			else if(ch == '\\') {
				sb.append("\\\\");
			}
			else if(ch == '"') {
				sb.append("\\\"");
			}
			else if(ch == '\b') {
				sb.append("\\b");
			}
			else if(ch == '\f') {
				sb.append("\\f");
			}
			else if(chx >= 0x10000 ) {
				throw new RuntimeException("invalid character U16, over 2bytes code.");
			}
			else if(chx >= 127 ) {
				sb.append( String.format("\\u%04x", chx) );
			}
			else {
				sb.append(ch);
			}
		}
		return sb.toString();
	}
	
	public static String unescape(String value) {
		StringBuilder sb = new StringBuilder();
		
		int i = 0;
		while(i<value.length()) {
			char ch = value.charAt(i);
			i++;
			
			if(ch == '\\' && i < value.length()) {
				ch = value.charAt(i);
				i++;
				
				if(ch == '\\' || ch == '/' || ch == '\\' || ch == '"' || ch == '\'') {
					sb.append(ch);
				}
				else if(ch == 'n') sb.append('\n');
				else if(ch == 'r') sb.append('\r');
				else if(ch == 't') sb.append('\t');
				else if(ch == 'b') sb.append('\b');
				else if(ch == 'f') sb.append('\f');
				else if(ch == 'u') {
					StringBuilder hex = new StringBuilder();
					if(i+4 > value.length()) {
						throw new RuntimeException("Bad unicode size character, should 4 chars");
					}
					
					for(char xbit : value.substring(i, i + 4).toCharArray()) {
						if(!Character.isLetterOrDigit(xbit)) {
							throw new RuntimeException("Bad unicode character - "+xbit);
						}
						hex.append(Character.toLowerCase(xbit));
					}
					i += 4;
					
					int hexCode = Integer.parseInt(hex.toString(), 16);
					sb.append((char)hexCode);
				}
				else {
					throw new RuntimeException("Invalid escape sequence : \\"+ch);
				}
			} 
			else {
				sb.append(ch);
			}
		}
		return sb.toString();
	}
	
	public static String convertNull(String value) {
		return convertNull(value, true);
	}
	
	public static boolean checkNull(String value) {
		return (value == null || value.length() == 0);
	}
	
	public static boolean checkNotNull(String value) {
		return !checkNull(value);
	}
	
	public static String convertNull(String value, boolean nullable) {
		String valueExpression = "\"\"";
		if(value == null) {
			if(nullable) valueExpression = "null";
		}
		else {
			valueExpression = "\"" + value + "\"";
			// 데이터를 trim이 가능한 경우에 right trim 반영 (20191002)
//			valueExpression = "\"" + com.eactive.eai.common.util.StringUtil.rightTrim(value) + "\"";
		}
		return valueExpression;
	}
	
	public static String convertNumber(String value) {
		return convertNumber(value, true);
	}
	
	public static String convertNumber(String value, boolean nullable) {
		String valueExpression = "0";
		if(value == null) {
			if(nullable) valueExpression = "null";
		}
		else {
			int i = 0;
			try  {
				i = Integer.parseInt(value);
				valueExpression = "" + i;
			}
			catch(Exception e) {				
				valueExpression = "0";
			}
		}
		return valueExpression;
	}
//	   public static void main(String args[]) throws Exception {
//	   test();
//}

// private static void test() throws Exception {
//		byte[] bytes = {0x53, 0x22, 0x5C, 0x0D, 0x0A, 0x22, 0x45};
//		String valueStr =new String(bytes); 
//		
//		System.out.println("Orign : " +valueStr);
//		String escapeStr = JsonUtil.escape(valueStr);
//		System.out.println("Escape : " +escapeStr);
//		
//		String unescapeStr = JsonUtil.unescape(escapeStr);
//		System.out.println("Unescape : " +unescapeStr);
//	}

}
