package kcg.imis.cmmn.service;

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

import javax.annotation.Resource;

import kcg.imis.cmmn.CommConstants;
import kcg.imis.cmmn.mapper.CodeMapper;
import kcg.imis.cmmn.vo.CodeVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
  * @FileName : CodeServiceImpl.java
  * @Project : 국제해양프로젝트
  * @Date : 2018. 3. 21. 
  * @작성자 : Moon
  * @변경이력 : 
  * @프로그램 설명 : 코드 서비스 객체
 */
@Service("codeService")
public class CodeServiceImpl implements CodeService {

	final String LIST_ALL_VAL = "";
	final String LIST_ALL_STR = "전체";
	
	/**
	 * CodeMapper.
	 */
	@Resource(name = "codeMapper")
	private CodeMapper codeMapper;


	public List<CodeVO> getCodeList(final String group) throws Exception {

		List<CodeVO> codeVOList = new ArrayList<CodeVO>();

		try {
			codeVOList = codeMapper.getCodeList(group);
		} catch (Exception e) {
			e.printStackTrace();
			codeVOList = null;
		}

		return codeVOList;
	}
	
	public List<CodeVO> getCodeList(final String group, boolean includeAll) throws Exception {

		List<CodeVO> codeVOList = new ArrayList<CodeVO>();

		try {
			codeVOList = codeMapper.getCodeList(group);
			if (includeAll) {
				codeVOList = addAll(codeVOList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			codeVOList = null;
		}

		return codeVOList;
	}
	
	public CodeVO getCode(final String group, final String code) throws Exception {

		CodeVO codeVOList = new CodeVO();

		try {
			codeVOList = codeMapper.getCode(group, code);
		} catch (Exception e) {
			e.printStackTrace();
			codeVOList = null;
		}

		return codeVOList;
	}

	public HashMap<String, String> getCodeMap(final String group) throws Exception {
		
		try {
			List<CodeVO> codeVOList = new ArrayList<CodeVO>();
			codeVOList = codeMapper.getCodeList(group);
			return convertMap(codeVOList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	/**
	 * code를 수정한다.
	 * 
	 * @param codeVO CodeVO 객체
	 * @return 성공 - 1, 실패 - 0
	 * @throws Exception
	 *             기본 예외 처리
	 */

	public int updateCode(final CodeVO codeVO) throws Exception {
		return codeMapper.updateCode(codeVO);
	}

	/**
	 * code를 추가한다.
	 * 
	 * @param codeVO  CodeVO 객체
	 * @return 성공 - 1, 실패 - 0
	 * @throws Exception
	 *             기본 예외 처리
	 */

	public int insertCode(final CodeVO codeVO) throws Exception {
		return codeMapper.insertCode(codeVO);
	}

	/**
	 * code를 삭제한다.
	 * 
	 * @param codeVO  CodeVO 객체
	 * @return 성공 - 1, 실패 - 0 `
	 * @throws Exception
	 *             기본 예외 처리
	 */
	@Transactional
	public int deleteCode(final CodeVO codeVO) throws Exception {
		return codeMapper.deleteCode(codeVO);
	}
	
	
	@Override
	public List<CodeVO> getPlace1SubList(final String place1Code) throws Exception {
		CodeVO code = new CodeVO();
		List<CodeVO> codes = new ArrayList<CodeVO>();
		try {
			String[] subCodes = PLACE_CATEGORY.get(place1Code);
 			if (subCodes != null) {
				for (int i = 0; i < subCodes.length; i++) {
					code = getCode(CommConstants.CODE_PLACE1, subCodes[i]);
					codes.add(code);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			codes = null;
		}
		
		return codes;
	}
	
	@Override
	public List<CodeVO> getPlace1SubListWithMain(final String place1Code) throws Exception {
		CodeVO code = new CodeVO();
		List<CodeVO> codes = new ArrayList<CodeVO>();
		try {
			String[] subCodes = PLACE_CATEGORY.get(place1Code);
 			if (subCodes != null) {
 				code = getCode(CommConstants.CODE_PLACE1, place1Code);
 				codes.add(code);
 				
 				// 하위 기관
				for (int i = 0; i < subCodes.length; i++) {
					code = getCode(CommConstants.CODE_PLACE1, subCodes[i]);
					codes.add(code);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			codes = null;
		}
		
		return codes;
	}
	
	@Override
	public String getPlace1HighCode(final String place1Code) throws Exception {
		for (Map.Entry<String, String[]> entry : PLACE_CATEGORY.entrySet()) {
		    String key = entry.getKey();
		    String[] values = entry.getValue();

		    for (String item : values) {
				if (item.equalsIgnoreCase(place1Code)) {
					return key;
				}
			}
		}
		return place1Code;
	}
	
	/**
	 * 맵으로 변환
	 * @param list 분류목록 
	 * @return ("코드명", "코드") 형식으로 리턴
	 */
	private HashMap<String, String> convertMap(final List<CodeVO> list) {
		HashMap<String, String> map = new HashMap<String, String>();
		for (CodeVO item : list) {
			map.put(item.getCode(), item.getName());
		}
		return map;
	}
	

	private List<CodeVO> addAll(final List<CodeVO> list) {
		if (list != null) {
			CodeVO allVO = new CodeVO();
			allVO.setCode(LIST_ALL_VAL);
			allVO.setGroup(LIST_ALL_VAL);
			allVO.setName(LIST_ALL_STR);
			allVO.setShortName(LIST_ALL_STR);
			list.add(0, allVO);
		}
		return list; 
	}
}
