package nlib.sample.web;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import egovframework.rte.fdl.cryptography.EgovEnvCryptoService;
import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;
import egovframework.rte.fdl.cryptography.impl.EgovARIACryptoServiceImpl;
import nlib.cmm.crypto.AriaCrypto;
import nlib.cmm.service.NlibProperty;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : SampleEncoderController.java
 * 
 * @Description : 비밀번호 등 인코딩값 조회/생성
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 7. 8.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 7. 8.
 * @version 1.0
 *
 */
@Controller
public class SampleEncoderController {
	
	private static final Logger log = LoggerFactory.getLogger(SampleEncoderController.class); 
	
	/** 암호화서비스 */
    @Resource(name = "egovEnvCryptoService")
    EgovEnvCryptoService cryptoService;
    
    @Resource(name = "egovEnvPasswordEncoderService")
    EgovPasswordEncoder egovPasswordEncoder;
    

    @Resource(name = "ariaCrypto")
    AriaCrypto ariaCrypto;
    
    @Value("#{properties['dataapi.server.ip']}")
    private String dataapiServerIp;
    
    @RequestMapping("/sample/password/getSampleEncoder.do")
    public String getSampleInfoForm(ModelMap model
    		, @RequestParam(required=false) String hash
    		, @RequestParam(required=false) String plainText
    		, @RequestParam(required=false) String encodedText
    		) throws Exception {
    	
    	String msg = null;
    	String decodedText = null;
    	
    	if(StringUtil.isEmpty(hash)) {
    		msg = "암호화 알고리즘을 선택하여 주시기 바랍니다.";
    	} else if(StringUtil.isNotEmpty(plainText)) {
    		// 암호처리 
    		log.debug("암호화 처리 시작 =" + plainText + "=");
    		
    		if("sha-256".equalsIgnoreCase(hash)) {
    			encodedText = egovPasswordEncoder.encryptPassword(plainText);
    		} else if("aria".equals(hash)) {
    			
    			encodedText = ariaCrypto.encode(plainText);
    			
    			log.debug(" Aria plainText=" + plainText);
    			log.debug(" Aria encodedText=" + encodedText);

    			decodedText = ariaCrypto.decode(encodedText);
    			log.debug(" Aria after decoding, decodedText=" + decodedText);
    			
    		} else {
    			msg = "암호화 알고리즘이 적합하지 않습니다.";
    		}
    	} else if("aria".equalsIgnoreCase(hash) && StringUtil.isNotEmpty(encodedText)) {
    		// 복호처리 
			decodedText = ariaCrypto.decode(encodedText);
			log.debug(" Aria after decoding, decodedText=" + decodedText);
			
    	} else {
    		msg = "암호화할 문자열을 입력하여 주시기 바랍니다.";
    	}


		model.addAttribute("msg", msg);
		model.addAttribute("hash", StringUtil.getString(hash, "aria"));
		model.addAttribute("plainText", plainText);
		model.addAttribute("encodedText", encodedText);
		model.addAttribute("decodedText", decodedText);
		model.addAttribute("key", NlibProperty.getProperty("crypto.aria.defaultKey"));
		
		return "nlib/sample/getSampleEncoder";
    }

}
