package com.eactive.eai.adapter.http.client.impl.filter;

import java.util.concurrent.ConcurrentHashMap;

import com.eactive.eai.common.util.Logger;

public class HttpClient5AdapterFilterFactory {
	static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
	static String customBasePackage = "com.eactive.eai.custom.adapter.http.client.impl.filter";
	static String basePackage = "com.eactive.eai.adapter.http.client.impl.filter";
	private static ConcurrentHashMap<String, HttpClientAdapterFilter> h = new ConcurrentHashMap<>();
	
	private HttpClient5AdapterFilterFactory() {
		throw new IllegalStateException("Utility class");
	}

	public static HttpClientAdapterFilter createFilter(String type) {
		if (h.containsKey(type)) {
			return h.get(type);
		}

		HttpClientAdapterFilter filter = null;
		switch (HttpClient5AdapterFilterType.getValue(type)) {
		case SIMPLEFRAMEWORK:
			filter = new SimpleFrameworkFilter();
			break;
		case SIMPLEFRAMEWORKBODY:
			filter = new SimpleframeworkBodyFilter();
			break;
		case JBOBP:
			filter = new JBOBPFilter();
			break;
		case KFTCFACE:
			filter = new KFTCFaceFilter();
			break;
		case KFTCP2P:
			filter = new KFTCP2PFilter();
			break;
		case KAKAOBANK:
			filter = new KAKAOBankFilter();
			break;
		case NAVERFIN:
			filter = new NAVERFinFilter();
			break;
		case NICECREDIT:
			filter = new NICECreditFilter();
			break;
		case TRACEBODY:
			filter = new TraceBodyFilter();
			break;
		case TOSSBANK:
			filter = new TOSSBankFilter();
			break;
		case ASYNCRESPONSE:
			filter = new AsyncReponseFilter();
			break;
		case ALLHEADER:
			filter = new AllHeaderFilter();
			break;
		default:
			filter = classForName(type);
			if (filter == null) {
				filter = classForName(customBasePackage + "." + type);
			}
			if (filter == null) {
				filter = classForName(basePackage + "." + type);
			}
			break;
		}

		if (filter != null) {
			h.put(type, filter);
			return filter;
		} else {
			return null;
		}
	}
	
	private static HttpClientAdapterFilter classForName(String type) {
		try {
			Class<?> cl = Class.forName(type);
			return (HttpClientAdapterFilter) cl.newInstance();
		} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
			logger.error("Cannot create a HttpClientAdapterFilter. - {}", type);
			return null;
		}
	}

	
}