package com.starcharge.base.controller;

import java.util.jar.Attributes;
import java.util.jar.Manifest;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.starcharge.base.redis.RedisClient;
import com.starcharge.base.redis.RedisKeyConstant;
import com.starcharge.base.util.CaptchaUtilsEx;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 首页
 * 
 * @author lilin
 * @version [版本号, 2018年11月28日]
 */
@RestController
public class IndexController {

    @Autowired
    private RedisClient redisClient;

    /**
     * 版本号
     */
    private static String version;
    
    static {
        
        try {
            version = "v" + new Manifest(IndexController.class.getResourceAsStream("/META-INF/MANIFEST.MF")).getMainAttributes()
                .get(Attributes.Name.IMPLEMENTATION_VERSION).toString();
        } catch (Exception e) {
            version = "unknow";
        }
        
        version = "<html><head></head><body>wios version: " + version + ".</body></html>";
    }
    
    /**
     * 首页展示页
     *
     * @return 首页展示页
     */
    @RequestMapping("/")
    public String index() {
        return version;
    }
    
    /**
     * 健康检查
     *
     * @return success
     */
    @RequestMapping("/healthCheck.do")
    public String healthCheck() {
        return "success";
    }

    /**
     * 验证码
     * @param deviceid  如果是前后端分离,页面也需要传(可以使用uuid,验证时使用相同的uuid)
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/jcaptcha.do")
    public void jcaptcha(String deviceid, HttpServletRequest request,
                         HttpServletResponse response) throws Exception {

        CaptchaUtilsEx captchaUtils = null;

        boolean isMobileCaptcha = StringUtils.isNotBlank(deviceid);

        if (isMobileCaptcha) {
            captchaUtils = new CaptchaUtilsEx(130, 58, 4);
        } else {
            captchaUtils = new CaptchaUtilsEx(98, 39, 4, true);
        }

        // 如果是手机登录,则保存缓存
        if (isMobileCaptcha) {
            redisClient.put(RedisKeyConstant.REDIS_IMAGE_CAPTCHA_PREFIX +deviceid, captchaUtils.getImageCode(), 600);
        } else {
            request.getSession().setAttribute("app.jcaptcha",captchaUtils.getImageCode());
        }

        // 输出图象到页面
        ImageIO.write(captchaUtils.getImage(), "JPEG", response.getOutputStream());
    }

    @RequestMapping("/generateQrcode.do")
    public void generateQrcode(String url, Integer width, Integer height, HttpServletRequest request, HttpServletResponse response) throws Exception {

        width = width == null ? 300 : width;
        height = height == null ? 300 : height;

        // 输出图象到页面
        ImageIO.write(QrCodeUtil.generate(url, width, height), "JPEG", response.getOutputStream());
    }
    
}