
package nlib.user.web;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;
import nlib.restful.service.DataApiReqVO;
import nlib.security.SecUserVO;
import nlib.user.service.MemberService;
import nlib.user.service.NlibLoginVO;
import nlib.user.service.UserInfoService;


/**
 * <pre>
 * @Class Name : userInfoController.java
 * 
 * @Description : 회원 정보 controller
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 7. 14.    JSYOO       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP JSYOO
 * @since 2021. 7. 14.
 * @version 1.0
 *
 */
@Controller
public class UserInfoController {
	
	@Resource(name="userInfoService")
	private UserInfoService userInfoService;
	
	@Resource(name = "egovEnvPasswordEncoderService")
    EgovPasswordEncoder egovPasswordEncoder;
	
	/**
	 * 내 정보 조회
	 * @exception Exception
	 */
	@RequestMapping(value="/userInfo/getMyInfo.do")
	public String getMyInfo(HttpServletResponse response,HttpServletRequest request) throws Exception{
		return "nlib/userInfo/getMyInfo";
	}
	
	/**
	 * 내 정보 수정 페이지
	 * @exception Exception
	 */
	@RequestMapping(value="/userInfo/putMyInfo.do")
	public String putMyInfo(NlibLoginVO vo,HttpServletResponse response,HttpServletRequest request,Authentication authentication,ModelMap model) throws Exception{
		NlibLoginVO loginVO =userInfoService.selectMyInfo(vo);
		
		String encodedText=null;
		encodedText = egovPasswordEncoder.encryptPassword(vo.getPassword());
		vo.setPassword(encodedText);
		
		//암호화된 패스워드 비교
		if(loginVO.getPassword().equals(vo.getPassword())) {
			model.addAttribute("loginVO",loginVO);
			return "nlib/userInfo/putMyInfo";
		}else {
			String message="비밀번호가 잘못되었습니다.";
			model.addAttribute("message",message);
			return "nlib/userInfo/pwCertMyInfo";
		}
	}
	
	/**
	 * 내 정보 수정 
	 * @return 
	 * @exception Exception
	 */
	@RequestMapping(value="/userInfo/updateMyInfo.ajax" , method=RequestMethod.POST)
	public @ResponseBody void updateMyInfo(NlibLoginVO vo,HttpServletResponse response,HttpServletRequest request) throws Exception{
		
		String encodedText=null;
		encodedText = egovPasswordEncoder.encryptPassword(vo.getPassword());
		vo.setPassword(encodedText);
		
		userInfoService.updateMyInfo(vo);
	}
	/**
	 * 내 정보 수정 전 password 인증
	 * @exception Exception
	 */
	@RequestMapping(value="/userInfo/pwCertMyInfo.do")
	public String pwCertMyInfo(HttpServletResponse response,HttpServletRequest request) throws Exception{
		
		return "nlib/userInfo/pwCertMyInfo";
	}
}