package nlib.restful.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;

import nlib.cmm.crypto.AriaCrypto;
import nlib.cmm.service.NlibProperty;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : DataApiReqVO.java
 * 
 * @Description : RESTful API 요청 VO
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 6. 22.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 6. 22.
 * @version 1.0
 *
 */
public class DataApiVO {
	
	private static final Logger log = LoggerFactory.getLogger(DataApiVO.class);

	/* 입출력 정보 중 공통 및 특정 항목들에 대한 처리를 위한 API 요소명 */
    public static final String XML_EL_NAME_ROOT_NAME        = "results";  // 결과값 최상위 요소명 
    public static final String XML_EL_NAME_PAGE_SIZE        = "rows";     // 페이지크기 : 한페이지에 보여줄 레코드수
    public static final String XML_EL_NAME_PAGE_INDEX       = "page";     // 페이지번호 
    public static final String XML_EL_NAME_PAGE_LAST_INDEX  = "totPages"; // 총페이지수
    public static final String XML_EL_NAME_RECORD_TOT_COUNT = "records";  // 총레코드수 
    public static final String XML_EL_NAME_MB_INFO_ID       = "mbInfoId"; // 시스템 내부 사용자ID
    public static final String XML_EL_NAME_RECORD           = "record";        // 다건 목록 자료인 레코드 요소명 
    public static final String XML_EL_NAME_RESULT_CODE      = "resultCode";    // 처리결과 코드 
    public static final String XML_EL_NAME_RESULT_MESSAGE   = "resultMessage"; // 처리결과 메시지 
    
    /* 페이지 처리 기본값 */
	public static final int DEFAULT_PAGE_SIZE = NlibProperty.getInt("list.paging.page.size", 10);
	public static final int DEFAULT_PAGE_INDEX = 1;
	
    String reqUrl = null;                   /* 요청업무ID에 대한 통합시스템의 응답 처리 프로그램 URI */
    HttpMethod reqMethod = HttpMethod.GET;  /* 요청메소드구분값 */
    
    String	resultCode;                     /* 결과코드 */
    String	resultMessage;                  /* 결과내용(오류내용) */
    
    HashMap<String, String> info = null;           /* 입출력 정보 맵 */
    HashMap<String, List<HashMap<String, String>>> listMap = null; /* 다건 레코드 리스트를 관리하는 맵 */

    public DataApiVO() {
    	super();
    }
    
	//--------------------------------------------------------------
    // 정보 항목 관련 IN/OUT 함수 
	//--------------------------------------------------------------
    public void putInfoItem(String key, String value) {
		if(StringUtil.isEmpty(key)) return;
		if(info == null) info = new HashMap<String, String>();
		
		info.put(key, value);
	}
	public String getInfoItem(String key) {
		return getInfoItem(key, null);
	}
    public String getInfoItem(String key, String defValue) {
		if(StringUtil.isEmpty(key)) return defValue;
		if(info == null) return defValue;
		
		String value = info.get(key);
		
		return StringUtil.isEmpty(value) ? defValue : value;
	}
	public void removeInfoItem(String key) {
		if(StringUtil.isEmpty(key)) return;
		if(info == null) return;
		info.remove(key);
	}
    public boolean hasInfo() {
    	return (info != null && info.size() > 0);
    }
    
    public boolean hasList() {
    	return (listMap != null && listMap.size() > 0); 
    }

    public boolean hasList(String listName) {
    	List list = getList(listName);
    	return (list != null && list.size() > 0); 
    }

	//--------------------------------------------------------------
    // 다건 레코드관련 함수  
	//--------------------------------------------------------------
    public void addListItem(String listName, HashMap<String, String> map) {
    	if(StringUtil.isEmpty(listName)) return;
		List<HashMap<String, String>> list = getList(listName);
		
		if(list == null) list = new ArrayList<HashMap<String, String>>();
		list.add(map);
		setList(listName, list);
		return;
	}
    
    public void removeListItem(String listName, int idx) {
    	if(StringUtil.isEmpty(listName)) return;
		if(idx < 0) return;
		
		List<HashMap<String, String>> list = getList(listName);
		if(list != null && list.size() > idx) list.remove(idx);
		return;
	}
    
    public void clearList(String listName) {
    	if(StringUtil.isEmpty(listName)) return;
    	
    	List<HashMap<String, String>> list = getList(listName);
    	if(list != null) list.clear();
		return;
	}
    
    public List<HashMap<String, String>> getList(String listName) {
    	if(StringUtil.isEmpty(listName)) return null;
    	if(listMap == null || listMap.size() < 1) return null;
    	
    	return listMap.get(listName);
	}
    
    public List<HashMap<String, String>> getRecordList() {
    	return getList(XML_EL_NAME_RECORD);
	}
    
    public<T> List<T> getRecordList(Class<T> clazz) {
    	List<HashMap<String, String>> list = getList(XML_EL_NAME_RECORD);
    	
    	if(list == null || list.size() < 1) return null;
    	
    	List<T> retList = new ArrayList<T>();
    	for(HashMap<String, String> map : list) {
    		T vo = (T)XmlConverter.convertMapToObject(map, clazz);
    		retList.add(vo);
    	}
    	
    	return retList;
	}
    
    public void setList(String listName, List<HashMap<String, String>> list) {
    	if(StringUtil.isEmpty(listName)) return;
    	if(listMap == null) initList();
    	
    	List<HashMap<String, String>> listOld = getList(listName);
    	if(listOld != null) listOld.clear();
    	
    	listMap.put(listName, list);
    	return;
	}
    
    private void initList() {
    	if(listMap == null) listMap = new HashMap<String, List<HashMap<String, String>>>();
    	return;
    }

	//--------------------------------------------------------------
    // 페이징 처리 관련 
	//--------------------------------------------------------------
    public int getPageIndex() {
    	int pageIndex = 0;
    	try { 
    		pageIndex = Integer.parseInt(getInfoItem(XML_EL_NAME_PAGE_INDEX, "0")); 
    	} catch(Exception e) {
    		log.error("getPageIndex > " + e.toString());
    		return 0;
    	}
        return pageIndex;
    }
    public void setPageIndex(int pageIndex) {
    	setPageIndex("" + pageIndex);
    }
    public void setPageIndex(String pageIndex) {
    	putInfoItem(XML_EL_NAME_PAGE_INDEX, pageIndex);
    }
    public int getPageLastIndex() {
    	int pageLastIndex = 0;
    	try {
    		pageLastIndex = Integer.parseInt(getInfoItem(XML_EL_NAME_PAGE_LAST_INDEX, "0")); 
    	} catch(Exception e) {
    		log.error("getPageLastIndex > " + e.toString());
    		return 0;
    	}
        return pageLastIndex;
    }
    public void setPageLastIndex(int pageLastIndex) {
    	setPageLastIndex("" + pageLastIndex);
    }
    public void setPageLastIndex(String pageLastIndex) {
    	putInfoItem("XML_EL_NAME_PAGE_LAST_INDEX", pageLastIndex);
    }
    public int getPageSize() {
    	int pageSize = 0;
    	try {
    		pageSize = Integer.parseInt(getInfoItem(XML_EL_NAME_PAGE_SIZE, "0")); 
    	} catch(Exception e) {
    		log.error("getPageSize > " + e.toString());
    		return 0;
    	}
        return pageSize;
    }
    public void setPageSize(int pageSize) {
    	setPageSize("" + pageSize);
    }
    public void setPageSize(String pageSize) {
    	putInfoItem(XML_EL_NAME_PAGE_SIZE, pageSize);
    }
    public int getRecordTotCount() {
    	int recordTotCount = 0;
    	try {
    		recordTotCount = Integer.parseInt(getInfoItem(XML_EL_NAME_RECORD_TOT_COUNT, "0")); 
    	} catch(Exception e) {
    		log.error("getRecordTotCount > " + e.toString());
    		return 0;
    	}
        return recordTotCount;
    }
    public void setRecordTotCount(int recordTotCount) {
    	setRecordTotCount("" + recordTotCount);
    }
    public void setRecordTotCount(String recordTotCount) {
    	putInfoItem(XML_EL_NAME_RECORD_TOT_COUNT, recordTotCount);
    }
    
	//--------------------------------------------------------------
    // 사용자ID 처리 관련 
	//--------------------------------------------------------------
	public String getMbInfoId() {
		return getInfoItem(XML_EL_NAME_MB_INFO_ID);
	}
	public String getEncMbInfoId() {
		return AriaCrypto.encode(getInfoItem(XML_EL_NAME_MB_INFO_ID));
	}
	public void setMbInfoId(String mbInfoId) {
		putInfoItem(XML_EL_NAME_MB_INFO_ID, mbInfoId);
	}
	
	//--------------------------------------------------------------
    // GET & SET 
	//--------------------------------------------------------------
	public String getReqUrl() {
		return reqUrl;
	}
	
	public void setReqUrl(String reqUrl) {
		this.reqUrl = reqUrl;
	}
	
	public HttpMethod getReqMethod() {
		return reqMethod;
	}
	
	public void setReqMethod(HttpMethod reqMethod) {
		this.reqMethod = reqMethod;
	}
	
    public String getResultCode() {
        return resultCode;
    }
    
    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }
    
    public String getResultMessage() {
        return resultMessage;
    }
    
    public void setResultMessage(String resultMessage) {
        this.resultMessage = resultMessage;
    }
    
	public HashMap<String, String> getInfo() {
		return info;
	}
	
	public void setInfo(HashMap<String, String> info) {
		this.info = info;
	}

	public HashMap<String, List<HashMap<String, String>>> getListMap() {
		return listMap;
	}

}
