package itn.let.uat.uia.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import egovframework.rte.fdl.security.userdetails.util.EgovUserDetailsHelper;
import itn.com.cmm.LoginVO;
import itn.com.cmm.util.DateUtils;
import itn.com.utl.fcc.service.EgovStringUtil;
import itn.let.mjo.apikey.service.ApiCallInfoMngService;
import itn.let.mjo.apikey.service.ApiKeyMngService;
import itn.let.mjo.apikey.service.ApiKeyVO;
import itn.let.utl.user.service.SecuKeyUtil;

/**
 * 일반 로그인, 인증서 로그인을 처리하는 컨트롤러 클래스
 * 
 * @author 공통서비스 개발팀 박지욱
 * @since 2009.03.06
 * @version 1.0
 * @see
 *
 *      <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일      수정자          수정내용
 *  -------    --------    ---------------------------
 *  2009.03.06  박지욱          최초 생성
 *  2011.08.31  JJY            경량환경 템플릿 커스터마이징버전 생성
 *
 *      </pre>
 */
@Controller
public class EgovMypageRESTAPIController{
	
	//api key 정보
	@Resource(name = "apiKeyMngService")
    private ApiKeyMngService apiKeyMngService;
	
	//api call info 정보
	@Resource(name = "apiCallInfoMngService")
    private ApiCallInfoMngService apiCallInfoMngService;
	
	/**
	 * 비밀번호 변경 페이지
	 */
	@RequestMapping(value="/web/api/sendRESTAPI_test_r1.do")
	public String sendRESTAPI_test(ModelMap model)
	throws Exception {
		/*LoginVO	loginVO = (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser();*/
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		model.addAttribute("pageTab", "passwordChange");
		return "web/api/sendRESTAPI_test_r1";
	}

	/**
	 * 비밀번호 변경 페이지
	 */
	@RequestMapping(value="/web/api/sendRESTAPI_test_r12.do")
	public String sendRESTAPI_test_r12(ModelMap model)
	throws Exception {
		/*LoginVO	loginVO = (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser();*/
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		model.addAttribute("pageTab", "passwordChange");
		return "web/api/sendRESTAPI_test_r12";
	}
	
	/**
	 * 비밀번호 변경 페이지
	 */
	@RequestMapping(value="/web/user/sendRESTAPI_test_r1_send.do")
	public String sendRESTAPI_test_r1_send(ModelMap model)
	throws Exception {
		/*LoginVO	loginVO = (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser();*/
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		model.addAttribute("pageTab", "passwordChange");
		return "web/api/sendRESTAPI_test_r1_send";
	}



	/** 
	 * @methodName	: sendRESTAPI_send_r2 
	 * @author		: 이호영
	 * @date		: 2023.05.23 
	 * @description	: [문자 발송] 같은 내용으로 여려명에게 보냄
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/web/api/sendRESTAPI_send_r2.do")
	public String sendRESTAPI_send_r2(ModelMap model)
			throws Exception {
		/*LoginVO	loginVO = (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser();*/
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		model.addAttribute("pageTab", "passwordChange");
		return "web/api/sendRESTAPI_send_r2";
	}
	
	
	/** 
	 * @methodName	: sendRESTAPI_send_r3 
	 * @author		: 이호영
	 * @date		: 2023.05.23 
	 * @description	: 수신자별 다른 내용으로 최대 500명에게 보냄
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/web/api/sendRESTAPI_send_r3.do")
	public String sendRESTAPI_send_r3(ModelMap model)
			throws Exception {
		/*LoginVO	loginVO = (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser();*/
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		model.addAttribute("pageTab", "passwordChange");
		return "web/api/sendRESTAPI_send_r3";
	}
	
	
	/**
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/web/api/intrdView.do")
	public String intrdView(ModelMap model)
			throws Exception {
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		return "web/api/intrdView";
	}
	
	/**
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/web/api/appMgmt.do")
	public String appMgmt(ModelMap model)
			throws Exception {
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);

		//사용자 신청/인증 정보
		ApiKeyVO apiKeyVO	=	new ApiKeyVO();
		apiKeyVO.setMberId(loginVO.getId());
		model.addAttribute("mberId", loginVO.getId());
		
		List<ApiKeyVO> resultList = new ArrayList<ApiKeyVO>();
		resultList = apiKeyMngService.selectApiKeyApplyStatus(apiKeyVO);

		if (resultList.size()>0) {
			if(StringUtils.isNotEmpty(resultList.get(0).getFrstRegistPnttm()))
			{
				resultList.get(0).setFrstRegistPnttm(DateUtils.getChangFormatS(resultList.get(0).getFrstRegistPnttm()));
				resultList.get(0).setLastUpdtPnttm(DateUtils.getChangFormatS(resultList.get(0).getLastUpdtPnttm()));
			}
			
			model.addAttribute("apiVO", resultList.get(0));
			
			
			apiKeyVO.setAccessNo(resultList.get(0).getAccessNo());
    		//api call info 정보
    		List<ApiKeyVO> resultList_call = new ArrayList<ApiKeyVO>();
    		resultList_call = apiCallInfoMngService.selectMberApiCallInfoList(apiKeyVO);
    		System.out.println("!!!!!!!!!!!!!!!!!");
    		resultList_call.forEach(t-> {
				try {
					t.setFrstRegistPnttm(DateUtils.getChangFormatS(t.getFrstRegistPnttm()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
    		
    		model.addAttribute("apiCallInfoVOList", resultList_call);
    		model.addAttribute("apiCallInfoVOListSize", resultList_call.size());
    		
		}else {
			model.addAttribute("apiVO", null);			
		}
		
		return "web/api/appMgmt";
	}
	/**
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/web/api/apiSpec.do")
	public String apiSpec(ModelMap model)
			throws Exception {
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		return "web/api/apiSpec";
	}
	
	@RequestMapping(value="/web/api/apiAtSpec.do")
	public String apiAtSpec(ModelMap model)
			throws Exception {
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		return "web/api/apiAtSpec";
	}
	@RequestMapping(value="/web/api/apiFtSpec.do")
	public String apiFtSpec(ModelMap model)
			throws Exception {
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		return "web/api/apiFtSpec";
	}
	
	/**
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/web/api/examDown.do")
	public String examDown(ModelMap model)
			throws Exception {
		
		LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
		
		if(loginVO == null) {
			return "redirect:/web/user/login/login.do";
		}
		model.addAttribute("loginVO", loginVO);
		
		return "web/api/examDown";
	}
	
	
	
}
