/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2018.  北京力成恒通科技有限公司
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.persagy.server.util

import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.persagy.server.entities.User
import java.awt.image.BufferedImage
import java.util.*

/**
 * 二维码生成工具
 *
 * @author  Andy
 */
object QrCodeUntils {

    var bgColor = 0xFFFFFF
    var pixelColor = 0x0

    /**
     * 创建二维码图像
     *
     * @param   contents        二维码存储的内容
     * @param   size            二维码大小
     * @return  二维码图像
     */
    fun createQrCode(contents: String, size: Int, logo: BufferedImage? = null): BufferedImage? {
        try {
            val hints = Hashtable<EncodeHintType, Any>()
            hints[EncodeHintType.CHARACTER_SET] = "utf-8"
            hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L

            val matrix = QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, size, size, hints)
            var image = BufferedImage(size, size, BufferedImage.TYPE_INT_RGB)
            for (y in 0 until size) {
                for (x in 0 until size) {
                    if (matrix.get(x, y)) {
                        image.setRGB(x, y, pixelColor)
                    } else {
                        image.setRGB(x, y, bgColor)
                    }
                }
            }

            if (null != logo) {
                image = addLogo(image, logo)
            }

            return image
        } catch (e: WriterException) {
            e.printStackTrace()
        }
        return null
    } // Function createQrCode()

    /**
     * 为二维码图片增加Logo
     *
     * @param   src         二维码图片
     * @param   logo        Logol图片
     * @return  添加过Logo的二维码图片
     */
    private fun addLogo(src: BufferedImage, logo: BufferedImage): BufferedImage {
        val srcWidth = src.width
        val srcHeight = src.height
        val logoWidth = logo.width
        val logoHeight = logo.height


        val image = BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_4BYTE_ABGR)
        val gd = image.createGraphics()
        gd.drawImage(src,0,0, null)
        gd.drawImage(logo,srcWidth * 2 / 5, srcHeight * 2 / 5, srcWidth * 3 / 5, srcHeight / 5, null)

        return image
    } // Function addLogo()

    /**
     * 生成uuid
     *
     * @return              uuid
     */
    fun uuidCreate(): String {
        val uuid = UUID.randomUUID().toString()
        return uuid.replace("-", "")
    } // Function uuidCreate

    fun toLog(user: User, coming: String?): String{
        var str =""
        str = "手机号："+user.phone+" 项目ID "+user.projId+" 用户ID "+ user.userId + " 用户名："+user.userName
        if (!coming.isNullOrEmpty()){
            str += " 请求来源: "+coming!!
        }
        return str
    } // Function toLog

} // Object QrCodeUntils