package com.eactive.eai.common.sessioninfo;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.common.util.UUIDGenerator;
import com.eactive.eai.message.EncodingVar;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.manager.StandardMessageManager;
import com.eactive.eai.message.parser.ReaderType;
import com.eactive.eai.message.parser.StandardReader;
import com.eactive.eai.message.service.InterfaceMapper;

public class SessionHistoryDTO {
	private String historyId; 
	private String requestData; 
	private String historyDstcd;
	private String responseResultDvsn;
	private String uuid;
	private String remoteIp;
	private boolean isJson;
	
	public SessionHistoryDTO(String requestData, String historyDstcd, String responseResultDvsn, String uuid, String remoteIp, boolean isJson) {
		
		this.requestData = requestData;
		this.historyDstcd = historyDstcd;
		this.responseResultDvsn = responseResultDvsn;
		this.uuid = uuid;
		this.remoteIp = remoteIp;
		this.isJson = isJson;
		
	}

	public SessionHistoryVO toVo() {
		
		if (isJson) {
			StandardMessage standardMessage = null;
			StandardMessageManager standardManager = StandardMessageManager.getInstance();
			StandardReader reader = null;
			InterfaceMapper mapper = standardManager.getMapper();
			standardMessage = standardManager.getStandardMessage();
			reader = standardManager.getReader(ReaderType.JSON);
			String historyId = null;
			String guid = null;
			String userId = null;
			String body = null;
			try {
				reader.parse(standardMessage, requestData);
				// 표준전문 파싱 후 어댑터의 charset을 설정한다.
				standardMessage.setBizDataCharset(EncodingVar.jsonEncoding);

				historyId = "HISL" + UUIDGenerator.getUUID();
				guid = mapper.getGuid(standardMessage);
				userId = mapper.getUserId(standardMessage);

				String bizData = standardMessage.getBizData();
				JSONObject bodyObject = (JSONObject) JSONValue.parse(bizData);
				// TODO 추후 제거해야될 값 확인
				// bodyObject.remove(ExtMessageKeys.FIELD_USER_PASSWORD);
				body = bodyObject.toJSONString();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//
//	         JSONObject reqJsonObject = (JSONObject) JSONValue.parse(requestData);
//	         JSONObject headerObject =  (JSONObject) reqJsonObject.get(ExtMessageKeys.GROUP_HEADER);
//	         JSONObject systemHeader = (JSONObject) headerObject.get(ExtMessageKeys.GROUP_SYSTEM_HEADER);
//	         JSONObject commonHeader = (JSONObject) headerObject.get(ExtMessageKeys.GROUP_COMMON_HEADER);
//	         
//	         String historyId = "HISL" + UUIDGenerator.getUUID();
//	         String guid = (String) systemHeader.get(ExtMessageKeys.FIELD_GUID);
//	         String userId = (String) commonHeader.get(ExtMessageKeys.FIELD_USERID); 
//	         
//	         JSONObject bodyObject = (JSONObject)reqJsonObject.get(ExtMessageKeys.GROUP_BODY);
//	         bodyObject.remove(ExtMessageKeys.FIELD_USER_PASSWORD);
//	         
//	         String body = bodyObject.toJSONString();
//	         String tranTime = (String) systemHeader.get(ExtMessageKeys.FIELD_TLGR_DMND_DT);
			String tranTime = DatetimeUtil.getCurrentTimeMillis();

			return new SessionHistoryVO(historyId, guid, userId, uuid, remoteIp, historyDstcd, responseResultDvsn, body,
					tranTime);			
		} else {
			
			// requestData : 57 bytes
			// ICON(4) + REQ/RES(3) + 정상여부(1) + 사용자 ID(9) + UUID(40)
			String historyId = "HISL" + UUIDGenerator.getUUID();
			String guid = "";
			String userId = StringUtils.trim(requestData.substring(8, 8 + 9));
			String tranTime = DatetimeUtil.getCurrentTimeMillis();
			
			return new SessionHistoryVO(historyId, guid, userId, uuid, remoteIp, historyDstcd, responseResultDvsn, requestData, tranTime);
		}
		
	}

	@Override
	public String toString() {
		return "SessionHistoryDTO [requestData=" + requestData + ", historyDstcd=" + historyDstcd
				+ ", responseResultDvsn=" + responseResultDvsn + ", uuid=" + uuid + ", remoteIp=" + remoteIp
				+ ", isJson=" + isJson + "]";
	}

}
