package com.dbs.mplus.controller.mobile.ext;

import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dbs.mplus.exception.mobile.MobileControllerException;
import com.dbs.mplus.framework.FrameworkConstants;
import com.dbs.mplus.framework.util.HttpRequestUtil;

// 가상 API. 기존 API 모양을 유지하기 위함 
@Controller  
@RequestMapping("/mobile/dev")
public class VirtualApiController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@RequestMapping(value = "/virtual.json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> execute(HttpServletRequest request, HttpServletResponse response) throws MobileControllerException {
		if(this.logger.isDebugEnabled()) { 
			StringBuilder builder = new StringBuilder(); 
			
			builder.append(FrameworkConstants.NEW_LINE); 
			builder.append("가상 API호출 ======== ").append(FrameworkConstants.NEW_LINE); 
			builder.append(HttpRequestUtil.getParameterStringForDebug(request)); 
			builder.append(FrameworkConstants.NEW_LINE); 
			
			this.logger.debug(builder.toString());
		} 	
		
		return HttpRequestUtil.getParameters(request); 
	}
	
	
	// 사용자의 결제 처리 후 요청되는 페이지 
	// 팝업이 열려서 결제가 됐으니까 팝업 닫는 명령을 뿌려주면 될듯 
	@SuppressWarnings("unused")
	@RequestMapping(value = "/receipt.ext", method = RequestMethod.POST)
	public ModelAndView processNicePayReceipt(HttpServletRequest request, /*HttpServletResponse response, */ ModelAndView model) throws MobileControllerException {
		if(this.logger.isDebugEnabled()) { 
			this.logger.debug("request : " + request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
		}
		
		// 모델명 변경 
		model.setViewName("mobile/pay/receipt");


		// 
		if(this.logger.isDebugEnabled()) { 
			StringBuilder builder = new StringBuilder(); 
			
			builder.append(FrameworkConstants.NEW_LINE); 
			builder.append("영수증 요청 수신 ===== ").append(FrameworkConstants.NEW_LINE); 
			builder.append("- viewName : ").append(model.getViewName()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- request.getContentType : ").append(request.getContentType()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- request.getCharacterEncoding : ").append(request.getCharacterEncoding()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- request.getParameter(CharSet) : ").append(request.getParameter("CharSet")).append(FrameworkConstants.NEW_LINE); 
			builder.append("- remote addr : ").append(request.getRemoteAddr()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- remote host : ").append(request.getRemoteHost()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- parameter ").append(HttpRequestUtil.getParameterStringForDebug(request)).append(FrameworkConstants.NEW_LINE); 

			
			// 
			this.logger.debug(builder.toString());
		} 		
		
		
		// 화면에 넘겨줄 키값들 정리 
		// 성공여부 : true, false 
		final String KEY_SUCCESS = "success"; 
		// 에러메시지 
		final String KEY_ERROR_MSG = "errorMsg"; 
		// 카드명 
		final String KEY_CARD_NAME = "cardName"; 
		// 카드번호 
		final String KEY_CARD_NUM = "cardNum"; 		
		// 할부개월 
		final String KEY_CARD_QUOTA = "cardQuota"; 
		// 상품명 
		final String KEY_GOODS_NAME = "goodsName"; 
		// 승인번호 
		final String KEY_AUTH_NUM = "authNum"; 
		// 승인날짜시간 
		final String KEY_AUTH_DT = "authDt"; 
		// 가격
		final String KEY_AMOUNT = "amount"; 
		// 구매자 이름 
		final String KEY_BUYER_NAME ="buyerName"; 
		// 구매자 메일 
		final String KEY_BUYER_EMAIL = "buyerEmail"; 
		// 상점예비필드 
		final String KEY_EXTRA = "extraInfo"; 
		// 성공메시지 
		final String KEY_SUCCESS_MSG = "successMsg"; 
		
		model.addObject(KEY_SUCCESS, "true"); 
		model.addObject(KEY_SUCCESS_MSG, "서대리, 영수증 파라미터 아직 안 만들었어요.. "); 
		
	
		
		return model; 
	}
		
	
	
	// 사용자가 승인을 받으면, M-Payment에서 승인정보를 알려주는 곳 
	// 여기에서 FAIL을 반환하면 망상취소가 될것임 
	@RequestMapping(value = "/confirmationOK.ext", method = RequestMethod.POST)
	@ResponseBody
	public String processConfirmationOK(HttpServletRequest request, HttpServletResponse response) throws MobileControllerException {
		if(this.logger.isDebugEnabled()) {
			StringBuilder builder = new StringBuilder(FrameworkConstants.NEW_LINE); 
			
			// 젠장! 여기는 CharSet 파라미터가 안 들어와... 
			builder.append("processConfirmationOK ===== ").append(FrameworkConstants.NEW_LINE); 
			builder.append("- request.getContentType : ").append(request.getContentType()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- request.getCharacterEncoding : ").append(request.getCharacterEncoding()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- request.getParameter(CharSet) : ").append(request.getParameter("CharSet")).append(FrameworkConstants.NEW_LINE); 
			builder.append("- remote addr : ").append(request.getRemoteAddr()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- remote host : ").append(request.getRemoteHost()).append(FrameworkConstants.NEW_LINE); 
			builder.append("- client IP : ").append(HttpRequestUtil.getClientIpAddr(request)).append(FrameworkConstants.NEW_LINE); 
			builder.append("- raw : ").append(HttpRequestUtil.getParameterStringForDebug(request)).append(FrameworkConstants.NEW_LINE); 
			
			
			this.logger.debug(builder.toString());
		}
		
		
		
		return "SUCCESS"; 
	}
}
