package com.eactive.eai.util.jsonJson;

import java.util.HashMap;
import java.util.Map;

import com.eactive.eai.util.json.JsonPathsTransform;
import com.eactive.eai.util.json.transformer.CustomValueTransformer;
import com.eactive.eai.util.json.transformer.ValueTransformer;

public class PathsTransformTest {
	public static void main(String[] args) {
		String jsonString = "{ \"person\": { \"name\": \"John\", \"age\": 30, \"groups\": [ { \"id\": 1, \"name\": \"Group A\" }, { \"id\": 2, \"name\": \"Group B\" }, { \"id\": 3, \"name\": \"Group C\" } ] } }";

		System.out.println("Origin JSON): \n" + jsonString);
		
		// CustomValueTransformer 사용 예제
		CustomValueTransformer nameTransformer = new CustomValueTransformer();
		nameTransformer.setProperty("PREFIX", "Mr. ");
		nameTransformer.setProperty("SUFFIX", " Esq.");

		CustomValueTransformer groupTransformer = new CustomValueTransformer();
		groupTransformer.setProperty("PREFIX", "Team: ");
		groupTransformer.setProperty("SUFFIX", " Ltd.");
		
		// 타입에따라 각각 처리하도록 해야 한다.
		Map<String, ValueTransformer<String, String>> pathTransformerMap = new HashMap<>();
        pathTransformerMap.put("$.person.name", nameTransformer);                // 경로: person.name
        pathTransformerMap.put("$.person.groups[0].name", groupTransformer);     // 경로: groups[0].name
        pathTransformerMap.put("$.person.groups[1].name", groupTransformer);     // 경로: groups[1].name
        pathTransformerMap.put("$.person.groups[2].name", groupTransformer);     // 경로: groups[2].name

		String modifiedJsonString = JsonPathsTransform.modifyValuesAtPaths(jsonString, pathTransformerMap, true);
		System.out.println("Modified JSON (Pretty): \n" + modifiedJsonString);
		
		Map<String, ValueTransformer<Integer, Integer>> intTransformerMap = new HashMap<>();
		intTransformerMap.put("$.person.age", new ValueTransformer<Integer, Integer>() {
            @Override
            public Integer transform(Integer age) {
                return age + 10;
            }

			@Override
			public void setProperty(String propertyName, Object value) {
				// TODO Auto-generated method stub
			}
			@Override
			public String getProperty(String propertyName) {
				return null;
			}

        });
		
		modifiedJsonString = JsonPathsTransform.modifyValuesAtPaths(jsonString, intTransformerMap, true);
		System.out.println("Modified JSON (Pretty): \n" + modifiedJsonString);
		
	}
}
