package nlib.sample.web;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import nlib.cmm.qrcode.QRCodeUtil;
import nlib.user.service.NlibLoginVO;
import nlib.util.StringUtil;

@Controller
public class BarcodeController {

	private static final Logger log = LoggerFactory.getLogger(BarcodeController.class);

	/** QRCodeService */
	@Resource(name = "qrCodeService")
	private QRCodeUtil qrCodeService;
	
    /**
     * 샘플 : 회원정보를 입력받을 수 있는 폼을 구성하여 표출한다.  
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/sample/barcode/getMemberQRCodeForm.do", produces=MediaType.IMAGE_JPEG_VALUE)
    @Secured("ROLE_ADMIN")
    public String getMemberQRCodeForm(@ModelAttribute("loginVO") NlibLoginVO loginVO, ModelMap model) throws Exception {
		
    	log.debug("getMemberQRCodeForm : loginVO=" + loginVO.toString());
    	
    	if(StringUtil.isEmpty(loginVO.getMbInfoId())) loginVO.setMbInfoId("USER0001-000001");
    	if(StringUtil.isEmpty(loginVO.getEmail()))  loginVO.setEmail("myid@digitalship.co.kr");
    	if(StringUtil.isEmpty(loginVO.getName()))   loginVO.setName("홍길동");
    	
    	model.addAttribute("loginVO", loginVO);
    	
        return "nlib/sample/getMemberInfoFromQRCode";
    }
    
    /**
     * 샘플 : 입력받은 회원정보를 가지고 QR 2D 바코드를 생성하는 예를 보여준다. 
     * 결과값은 JPG 바코드 이미지 이다. (Zxing Lib 사용) 
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/sample/barcode/getMemberQRCode.do", produces=MediaType.IMAGE_JPEG_VALUE)
    public @ResponseBody byte[] getMemberQRCode(@ModelAttribute("loginVO") NlibLoginVO loginVO, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
    	log.debug("getMemberQRCode : loginVO=" + loginVO.toString());
    	
    	BufferedImage bufferedImage = qrCodeService.generateMemberQrcodeImage(loginVO);
    	
    	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write( bufferedImage  , "jpg", byteArrayOutputStream);
        byte[] bs = byteArrayOutputStream.toByteArray();
        response.setContentLength(bs.length);
        
        return byteArrayOutputStream.toByteArray();
    }
    

    /**
     * 샘플 : 회원정보 QR 2D 바코드 내용을 입력받아 사용자VO 정보를 추출하는 예를 보여준다. 
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/sample/barcode/getMemberInfoFromQRCode.do")
    public String getMemberInfoFromQRCode(@RequestParam String barcodeText, ModelMap model) throws Exception {
		
    	log.debug("getMemberInfoFromQRCode : barcodeText=" + barcodeText);
    	
    	NlibLoginVO loginVO = qrCodeService.getMemberInfoFromQRCode(barcodeText);
    	log.debug("getMemberInfoFromQRCode : loginVO=" + loginVO.toString());
    	
    	model.addAttribute("loginVO", loginVO);
    	
        return "nlib/sample/getMemberInfoFromQRCode";
    }
}
