package com.eactive.eai.util;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonBeatiUtil {
	public static String SimpleBeautiful(Object input) {
		JSONObject json = null;
		String data = "";
		if (input instanceof JSONObject) {
			json = (JSONObject) input;
		} else if (input instanceof String) {
			json = (JSONObject) JSONValue.parse((String) input);
		} else if (input instanceof byte[]) {
			String s = new String((byte[]) input);
			json = (JSONObject) JSONValue.parse(s);
		}
		data = json.toJSONString();
		int tabCount = 0;
		StringBuilder inputBuilder = new StringBuilder();
		char[] inputChar = data.toCharArray();
		for (int i = 0; i < inputChar.length; i++) {
			String charI = String.valueOf(inputChar[i]);
			if (charI.equals("}") || charI.equals("]")) {
				tabCount--;
				if (!String.valueOf(inputChar[i - 1]).equals("[") && !String.valueOf(inputChar[i - 1]).equals("{")) {
					inputBuilder.append(simpleNewLine(tabCount));
				}
			}
			inputBuilder.append(charI);
			if (charI.equals("{") || charI.equals("[")) {
				tabCount++;
				if (String.valueOf(inputChar[i + 1]).equals("]") || String.valueOf(inputChar[i + 1]).equals("}")) {
					continue;
				}
				inputBuilder.append(simpleNewLine(tabCount));
			}
			if (charI.equals(",")) {
				inputBuilder.append(simpleNewLine(tabCount));
			}
		}
		return inputBuilder.toString();
	}

	private static String simpleNewLine(int tabCount) {
		StringBuilder builder = new StringBuilder();
		builder.append("\n");
		for (int j = 0; j < tabCount; j++) {
			builder.append("    ");
		}
		return builder.toString();
	}

	public static void main(String[] args) throws Exception {
		String data = "";
		data += "{                                                                      ";
		data += "    \"EAI_UJSON2DEPTHEXT_REQ1\":{                                      ";
		data += "        \"abc\":\"cba  \",                                             ";
		data += "        \"grid1\":[                                                    ";
		data += "            {                                                          ";
		data += "                \"long1\":1,                                           ";
		data += "                \"data2\":\"     \",                                   ";
		data += "                \"grid2\":[                                            ";
		data += "                    {                                                  ";
		data += "                        \"data3\":\"12345\",                           ";
		data += "                        \"big4\":14.00000                              ";
		data += "                    },                                                 ";
		data += "                    {                                                  ";
		data += "                        \"data3\":\"13   \",                           ";
		data += "                        \"big4\":14.00001                              ";
		data += "                    }                                                  ";
		data += "                ]                                                      ";
		data += "            },                                                         ";
		data += "            {                                                          ";
		data += "                \"long1\":10000,                                       ";
		data += "                \"data2\":\"     \",                                   ";
		data += "                \"grid2\":[                                            ";
		data += "                    {                                                  ";
		data += "                        \"data3\":\"23   \",                           ";
		data += "                        \"big4\":24.00000                              ";
		data += "                    }                                                  ";
		data += "                ]                                                      ";
		data += "            },                                                         ";
		data += "            {                                                          ";
		data += "                \"long1\":1000000000,                                  ";
		data += "                \"data2\":\"     \",                                   ";
		data += "                \"grid2\":[                                            ";
		data += "                    {                                                  ";
		data += "                        \"data3\":\"33   \",                           ";
		data += "                        \"big4\":1                                     ";
		data += "                    },                                                 ";
		data += "                    {                                                  ";
		data += "                        \"data3\":\"     \",                           ";
		data += "                        \"big4\":1234567890.00034                      ";
		data += "                    },                                                 ";
		data += "                    {                                                  ";
		data += "                        \"data3\":\"35   \",                           ";
		data += "                        \"big4\":0.00036                               ";
		data += "                    }                                                  ";
		data += "                ]                                                      ";
		data += "            }                                                          ";
		data += "        ],                                                             ";
		data += "        \"abcd\":\"123\",                                              ";
		data += "        \"grid3\":[                                                    ";
		data += "            {                                                          ";
		data += "                \"long1\":1,                                           ";
		data += "                \"data2\":\"     \",                                   ";
		data += "                \"grid4\":[                                            ";
		data += "                    {                                                  ";
		data += "                        \"data5\":\"00023\",                           ";
		data += "                        \"big6\":24                                    ";
		data += "                    }                                                  ";
		data += "                ]                                                      ";
		data += "            },                                                         ";
		data += "            {                                                          ";
		data += "                \"long1\":1000000000,                                  ";
		data += "                \"data2\":\"     \",                                   ";
		data += "                \"grid4\":[                                            ";
		data += "                    {                                                  ";
		data += "                        \"data5\":\"00033\",                           ";
		data += "                        \"big6\":0.0                                   ";
		data += "                    },                                                 ";
		data += "                    {                                                  ";
		data += "                        \"data5\":\"     \",                           ";
		data += "                        \"big6\":123456789012345                       ";
		data += "                    },                                                 ";
		data += "                    {                                                  ";
		data += "                        \"data5\":\"00035\",                           ";
		data += "                        \"big6\":900000003600011                       ";
		data += "                    }                                                  ";
		data += "                ]                                                      ";
		data += "            }                                                          ";
		data += "        ]                                                              ";
		data += "    }                                                                  ";
		data += "}                                                                      ";
		data = "{\"EAI_UJSON2DEPTHEXT_REQ1\":{\"abc\":\"cba  \",\"grid1\":[{\"long1\":1,\"data2\":\"     \",\"grid2\":[{\"data3\":\"12345\",\"big4\":\"14.00000\"},{\"data3\":\"13   \",\"big4\":\"14.00001\"}]},{\"long1\":10000,\"data2\":\"     \",\"grid2\":[{\"data3\":\"23   \",\"big4\":\"24.00000\"}]},{\"long1\":1000000000,\"data2\":\"     \",\"grid2\":[{\"data3\":\"33   \",\"big4\":\"0.00000\"},{\"data3\":\"     \",\"big4\":\"0.00034\"},{\"data3\":\"35   \",\"big4\":\"0.00036\"}]}],\"abcd\":\"123\",\"grid3\":[{\"long1\":1,\"data2\":\"     \",\"grid4\":[{\"data5\":\"00023\",\"big6\":\"24\"}]},{\"long1\":1000000000,\"data2\":\"     \",\"grid4\":[{\"data5\":\"00033\",\"big6\":\"0\"},{\"data5\":\"     \",\"big6\":\"34\"},{\"data5\":\"00035\",\"big6\":\"900000003600011\"}]}]}}";

		// System.out.println(JsonBeatiUtil.SimpleBeautiful(data));
	}

}
