/*
 * ********************************************************************************************************************
 *
 *                     :*$@@%$*:                         ;:                ;;    ;;
 *                   :@@%!  :!@@%:                       %!             ;%%@@%$ =@@@@@@@%;     @%@@@%%%%@@@@@
 *                  :@%;       :$=                       %%$$$%$$         ;$$  ;$@=   !@$
 *                  =@!                                  %!              @ $=;%   !@@@%:      !$$$$$$$$$$$$$$=
 *                  =@*                                  %!              @ $= % %@=   =%@!      %=
 *             *$%%! @@=        ;=$%%%$*:                %!              @ $= % =%%%%%%@$      *%:         =%
 *           %@@!:    !@@@%=$@@@@%!  :*@@$:              %!              @ $= % $*     ;@      @*          :%*
 *         ;@@!          ;!!!;:         ;@%:      =======@%========*     @ $$ % $%*****$@     :@$=*********=@$
 *         $@*   ;@@@%=!:                *@*
 *         =@$    ;;;!=%@@@@=!           =@!
 *          %@$:      =@%: :*@@@*       %@=                    Copyright (c) 2016-2018.  北京上格云技术有限公司
 *           ;%@@$=$@@%*       *@@@$=%@@%;
 *              ::;::             ::;::                                              All rights reservad.
 *
 * ********************************************************************************************************************
 */

package com.persagy.server.controller

import com.persagy.server.util.QrCodeUntils
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import java.io.IOException
import javax.imageio.ImageIO
import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletResponse

/**
 * 二维码接口类
 *
 * @author  PLX
 */
@Api(tags= ["010、二维码接口"])
@Controller
@RequestMapping("/service/qrcode/")
open class QrCodeController {
    companion object {
        private val logger = LoggerFactory.getLogger(QrCodeController::class.java)
    } // companion object

//    /**
//     * 设备资产
//     */
//    @ApiOperation(value="设备资产", notes="")
//    @GetMapping("fm")
//    fun fm(projectId: String, fmId: String, response: HttpServletResponse) {
//        logger.debug("------------------------$projectId")
//        outQrCode("http://service.sagacloud.cn/qrcode/$fmId", response)
//        return
//    } // Function fm()

    /**
     * 二维码生成接口
     */
    @ApiOperation(value="二维码生成接口", notes="")
    @GetMapping("zxing")
    fun point(Id: String, response: HttpServletResponse) {
        outQrCode("http://meos.sagacloud.cn/scan?key=$Id", response)
        return
    } // Function point()

    private fun outQrCode(contents: String, response: HttpServletResponse) {
        val buffImg = QrCodeUntils.createQrCode(contents, 400)

        // 禁止图像缓存
        response.setHeader("Pragma", "no-cache")
        response.setHeader("Cache-Control", "no-cache")
        response.setDateHeader("Expires", 0)
        response.contentType = "image/jpeg"

        // 将图像输出到Servlet输出流中
        val sos: ServletOutputStream
        try {
            sos = response.outputStream
            ImageIO.write(buffImg, "jpeg", sos)
            sos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return
    } // Function outQrCode()

} // Class QrCodeController