package sgc.portal.base.service;

import sgc.portal.SgcPortalAttributes;
import sgc.portal.base.model.ComData;
import sgc.portal.base.persistence.ComboSearchMapper;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.util.SessionUtil;
import sgc.portal.com.com.model.CommDtl;
import sgc.portal.com.sym.user.model.UserDetails;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 로그인 서비스 구현.
 *
 * @author WonSeok Song
 * @version 1.0.0
 * @since 2020. 7. 5.
 */
@Slf4j
@Service
public class ComboSearchService {

    /** 매퍼 선언. */
    @Autowired
    private ComboSearchMapper comboSearchMapper;

    /**
     * Select code list json object.
     *
     * @param param the param
     * @return the json object
     */
    public JSONObject selectCodeList(Map param) {

        JSONObject resObj = new JSONObject();

        Map commonCode;
        List <Map> cCode = null ;
        Map<String, String> commCode = null;
        String grpCd;
        String dataIdPrefix;
        String[] selectCodeList;
        try {

            commonCode = (Map) param.get("dma_Common_Code");
            grpCd = (String)commonCode.get("GRP_CD");
            dataIdPrefix = (String)commonCode.get("DATA_PREFIX");
            if(dataIdPrefix == null){
                dataIdPrefix = "dlt_CommonCode_";
            }
            selectCodeList = grpCd.split(",");
            commonCode.put("CODE", selectCodeList);
            try {
	            UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
	            userDetails.getLginId();
	            commonCode.put("CODE", selectCodeList);
	            commonCode.put("userId", userDetails.getUserId());
	            commonCode.put("cmpnCd", userDetails.getCmpnCd());
	            commonCode.put("bsuCd", userDetails.getBsuCd());
	            commonCode.put("deptCd", userDetails.getDeptCd());
            } catch (Exception ex) {
            	commonCode.put("cmpnCd", "S0001");
	        }
            List codeList;

            codeList = comboSearchMapper.selectCodeList(commonCode);

            //여러건의 code를 가져올 때 code별로 객체 생성
            if(selectCodeList.length > 1){
                int size = codeList.size();
                String preCode = "";
                List codeGrpList = new ArrayList();
                for (int i = 0; i < size; i++ ){
                    //Map codeMap = (Map)codeList.remove(0);
                    Map codeMap = (Map)codeList.get(i);
                    String gpCd = (String)codeMap.get("GRP_CD");
                    //if(StringUtils.isNotEmpty(user.getScrtNo()))
                    if ( !preCode.equals(gpCd)){
                        //System.out.println("11111111111111"+dataIdPrefix + preCode);
                        //System.out.println("11111111111111"+resObj);
                        resObj.put(dataIdPrefix + preCode, codeGrpList);
                        codeGrpList = new ArrayList();
                        preCode = gpCd;
                        codeGrpList.add(codeMap);
                    } else {
                        codeGrpList.add(codeMap);
                    }

                    if ( i ==  size-1 ){
                        resObj.put(dataIdPrefix + preCode, codeGrpList);
                    }
                }
            }else{
                resObj.put(dataIdPrefix + selectCodeList[0], codeList);
            }

            resObj.put("msg", "공통코드 조회가 완료되었습니다.");
            resObj.put("msgCode", "S");
        } catch (Exception ex) {
            resObj.put("msg", "공통코드 조회도중 오류가 발생하였습니다.");
            resObj.put("msgCode", "E");
            log.error("error",ex);
        }

        return resObj;
    }
    
    /**
     * Select code list json object.
     *
     * @param param the param
     * @return the json object
     */
    public JSONObject selectCodeListEn(Map param) {

        JSONObject resObj = new JSONObject();

        Map commonCode;
        List <Map> cCode = null ;
        Map<String, String> commCode = null;
        String grpCd;
        String dataIdPrefix;
        String[] selectCodeList;
        try {

            commonCode = (Map) param.get("dma_Common_Code");
            grpCd = (String)commonCode.get("GRP_CD");
            dataIdPrefix = (String)commonCode.get("DATA_PREFIX");
            if(dataIdPrefix == null){
                dataIdPrefix = "dlt_CommonCode_";
            }
            selectCodeList = grpCd.split(",");
            commonCode.put("CODE", selectCodeList);
            try {
	            UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
	            userDetails.getLginId();
	            commonCode.put("CODE", selectCodeList);
	            commonCode.put("userId", userDetails.getUserId());
	            commonCode.put("cmpnCd", userDetails.getCmpnCd());
	            commonCode.put("bsuCd", userDetails.getBsuCd());
	            commonCode.put("deptCd", userDetails.getDeptCd());
            } catch (Exception ex) {
            	commonCode.put("cmpnCd", "S0001");
	        }
            List codeList;

            codeList = comboSearchMapper.selectCodeListEn(commonCode);

            //여러건의 code를 가져올 때 code별로 객체 생성
            if(selectCodeList.length > 1){
                int size = codeList.size();
                String preCode = "";
                List codeGrpList = new ArrayList();
                for (int i = 0; i < size; i++ ){
                    //Map codeMap = (Map)codeList.remove(0);
                    Map codeMap = (Map)codeList.get(i);
                    String gpCd = (String)codeMap.get("GRP_CD");
                    //if(StringUtils.isNotEmpty(user.getScrtNo()))
                    if ( !preCode.equals(gpCd)){
                        //System.out.println("11111111111111"+dataIdPrefix + preCode);
                        //System.out.println("11111111111111"+resObj);
                        resObj.put(dataIdPrefix + preCode, codeGrpList);
                        codeGrpList = new ArrayList();
                        preCode = gpCd;
                        codeGrpList.add(codeMap);
                    } else {
                        codeGrpList.add(codeMap);
                    }

                    if ( i ==  size-1 ){
                        resObj.put(dataIdPrefix + preCode, codeGrpList);
                    }
                }
            }else{
                resObj.put(dataIdPrefix + selectCodeList[0], codeList);
            }

            resObj.put("msg", "공통코드 조회가 완료되었습니다.");
            resObj.put("msgCode", "S");
        } catch (Exception ex) {
            resObj.put("msg", "공통코드 조회도중 오류가 발생하였습니다.");
            resObj.put("msgCode", "E");
            log.error("error",ex);
        }

        return resObj;
    }
    
    /**
     * Select code list json object.
     *
     * @param param the param
     * @return the json object
     */
    public JSONObject selectCodeCmpnList(Map param) {

        JSONObject resObj = new JSONObject();

        Map commonCode;
        List <Map> cCode = null ;
        Map<String, String> commCode = null;
        String cmpn;
        String grpCd;
        String dataIdPrefix;
        String[] selectCodeList;
        try {

            commonCode = (Map) param.get("dma_Common_Code");
            cmpn = (String)commonCode.get("CMPN");
            grpCd = (String)commonCode.get("GRP_CD");
            dataIdPrefix = (String)commonCode.get("DATA_PREFIX");
            if(dataIdPrefix == null){
                dataIdPrefix = "dlt_CommonCode_";
            }
            selectCodeList = grpCd.split(",");

            UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
            userDetails.getLginId();

            commonCode.put("CODE", selectCodeList);
            commonCode.put("userId", userDetails.getUserId());
            commonCode.put("cmpnCd", cmpn);
            commonCode.put("bsuCd", userDetails.getBsuCd());
            commonCode.put("deptCd", userDetails.getDeptCd());

            List codeList;

            codeList = comboSearchMapper.selectCodeList(commonCode);

            //여러건의 code를 가져올 때 code별로 객체 생성
            if(selectCodeList.length > 1){
                int size = codeList.size();
                String preCode = "";
                List codeGrpList = new ArrayList();
                for (int i = 0; i < size; i++ ){
                    //Map codeMap = (Map)codeList.remove(0);
                    Map codeMap = (Map)codeList.get(i);
                    String gpCd = (String)codeMap.get("GRP_CD");
                    //if(StringUtils.isNotEmpty(user.getScrtNo()))
                    if ( !preCode.equals(gpCd)){
                        //System.out.println("11111111111111"+dataIdPrefix + preCode);
                        //System.out.println("11111111111111"+resObj);
                        resObj.put(dataIdPrefix + preCode, codeGrpList);
                        codeGrpList = new ArrayList();
                        preCode = gpCd;
                        codeGrpList.add(codeMap);
                    } else {
                        codeGrpList.add(codeMap);
                    }

                    if ( i ==  size-1 ){
                        resObj.put(dataIdPrefix + preCode, codeGrpList);
                    }
                }
            }else{
                resObj.put(dataIdPrefix + selectCodeList[0], codeList);
            }

            resObj.put("msg", "공통코드 조회가 완료되었습니다.");
            resObj.put("msgCode", "S");
        } catch (Exception ex) {
            resObj.put("msg", "공통코드 조회도중 오류가 발생하였습니다.");
            resObj.put("msgCode", "E");
            log.error("erwrwr");
        }

        return resObj;
    }

    /**
     * Select comm code list list.
     *
     * @param param the param
     * @return the list
     */
    public List<EasyHashMap<String, Object>> selectCommCodeList(CommDtl param) {
        return comboSearchMapper.selectCommCodeList(param);
    }

}