package com.lightinit.hsdataplatform.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by Mr.Yao on 2017/4/11.
 */
@Controller
@RequestMapping(value = "/captcha")
public class CaptchaController {

    @Autowired
    private Producer captchaProducer;

    @RequestMapping(value = "/image.action", method = RequestMethod.GET)
    public ModelAndView getCaptchaImage(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "timestamp", required = false) String timestamp) throws IOException {
        if (StringUtils.isEmpty(timestamp)) {
            model.addAttribute("timestamp", System.currentTimeMillis());
        } else {
            model.addAttribute("timestamp", timestamp);
        }

        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = captchaProducer.createText();

        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }
    @RequestMapping(value = "/qrcode.action", method = RequestMethod.GET)
    public ModelAndView getQRCodeImage(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "timestamp", required = false) String timestamp) throws IOException {
        if (StringUtils.isEmpty(timestamp)) {
            model.addAttribute("timestamp", System.currentTimeMillis());
        } else {
            model.addAttribute("timestamp", timestamp);
        }

        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/png");
        String qrCodeText = captchaProducer.createText();
        ServletOutputStream out = null;
        QRCodeWriter writer=new QRCodeWriter();
        int width=200;
        int height=200;
        BitMatrix m= null;
        try {
            out = response.getOutputStream();
            m = writer.encode(qrCodeText, BarcodeFormat.QR_CODE, height,width);
            MatrixToImageWriter.writeToStream(m, "png", out);
            out.flush();
        } catch (WriterException e) {
            e.printStackTrace();
        }finally {
            if (out != null)
                out.close();
        }
        return null;
    }
}
